mirror of
https://github.com/terorie/od-database-crawler.git
synced 2025-12-13 15:19:03 +00:00
Add urfave/cli app
This commit is contained in:
64
main.go
64
main.go
@@ -2,27 +2,77 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/urfave/cli"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var app = cli.App {
|
||||
Name: "oddb-go",
|
||||
Usage: "OD-Database Go crawler",
|
||||
Version: "0.1",
|
||||
BashComplete: cli.DefaultAppComplete,
|
||||
Writer: os.Stdout,
|
||||
Compiled: buildDate,
|
||||
Commands: []cli.Command{
|
||||
{
|
||||
Name: "crawl",
|
||||
Usage: "Crawl a list of URLs",
|
||||
ArgsUsage: "[site, site, ...]",
|
||||
Action: cmdCrawler,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
prepareConfig()
|
||||
}
|
||||
|
||||
func main() {
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
||||
func cmdCrawler(clic *cli.Context) error {
|
||||
readConfig()
|
||||
|
||||
if clic.NArg() == 0 {
|
||||
cli.ShowCommandHelpAndExit(clic, "crawl", 1)
|
||||
}
|
||||
|
||||
args := clic.Args()
|
||||
remotes := make([]*OD, len(args))
|
||||
for i, arg := range args {
|
||||
// https://github.com/golang/go/issues/19779
|
||||
if !strings.Contains(arg, "://") {
|
||||
arg = "http://" + arg
|
||||
}
|
||||
u, err := url.Parse(arg)
|
||||
if !strings.HasSuffix(u.Path, "/") {
|
||||
u.Path += "/"
|
||||
}
|
||||
if err != nil { return err }
|
||||
remotes[i] = &OD{ BaseUri: *u }
|
||||
}
|
||||
|
||||
c := context.Background()
|
||||
|
||||
remotes := make(chan *OD)
|
||||
go Schedule(c, remotes)
|
||||
inRemotes := make(chan *OD)
|
||||
go Schedule(c, inRemotes)
|
||||
|
||||
u, _ := url.Parse("http://mine.terorie.com:420/")
|
||||
remote := NewRemoteDir(*u)
|
||||
|
||||
globalWait.Add(1)
|
||||
remotes <- remote
|
||||
for _, remote := range remotes {
|
||||
globalWait.Add(1)
|
||||
inRemotes <- remote
|
||||
}
|
||||
|
||||
// Wait for all jobs to finish
|
||||
globalWait.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var buildDate = time.Date(
|
||||
2018, 10, 28,
|
||||
17, 10, 0, 0,
|
||||
time.UTC)
|
||||
|
||||
Reference in New Issue
Block a user