-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
91 lines (74 loc) · 1.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/thoas/go-funk"
"github.com/xbapps/xbvr-scrapers/scrapers"
)
func sceneWriter(wg *sync.WaitGroup, i *uint64, scenes <-chan scrapers.ScrapedScene) {
defer wg.Done()
for scene := range scenes {
atomic.AddUint64(i, 1)
fmt.Printf("\n\nScraped scene: %+v\n\n", scene)
}
}
func scrapeDir(dir string, scraperWG *sync.WaitGroup, collectedScenes chan scrapers.ScrapedScene) {
sfs, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
var scraperFiles []string
for _, sf := range sfs {
scraperFiles = append(scraperFiles, sf.Name())
}
fmt.Println(scraperFiles)
if funk.Contains(scraperFiles, "config.json") {
fmt.Printf("Scraping %s...\n", dir)
scraperWG.Add(1)
go scrapers.Scrape(scraperWG, dir, collectedScenes)
} else {
fmt.Printf("%s missing required files. Skipping.\n", dir)
}
}
func main() {
var scraper = flag.String("scraper", "", "process a single specified scraper")
flag.Parse()
scrapersDir := "scrapers"
files, err := ioutil.ReadDir(scrapersDir)
if err != nil {
log.Fatal(err)
}
t0 := time.Now()
var scraperWG, writerWG sync.WaitGroup
collectedScenes := make(chan scrapers.ScrapedScene, 250)
var sceneCount uint64
writerWG.Add(1)
go sceneWriter(&writerWG, &sceneCount, collectedScenes)
if *scraper != "" {
scraperDir := filepath.Join(scrapersDir, *scraper)
scrapeDir(scraperDir, &scraperWG, collectedScenes)
} else {
for _, f := range files {
scraperDir := filepath.Join(scrapersDir, f.Name())
if info, err := os.Stat(scraperDir); err == nil && !info.IsDir() {
continue
}
scrapeDir(scraperDir, &scraperWG, collectedScenes)
}
}
// Wait for scrapers to complete
scraperWG.Wait()
// Wait for sceneWriter threads to complete
close(collectedScenes)
writerWG.Wait()
fmt.Printf("Scraped %v scenes in %s\n",
sceneCount,
time.Now().Sub(t0).Round(time.Second))
}