Cleanup
This commit is contained in:
parent
a51d82d1ea
commit
dc38ce4a5f
@ -23,7 +23,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
@ -47,6 +46,8 @@ type review struct {
|
||||
|
||||
closestStnName string
|
||||
closestStnDist float64
|
||||
|
||||
err error
|
||||
}
|
||||
|
||||
type scraper interface {
|
||||
@ -71,78 +72,76 @@ func makeAbsUrl(ref, base string) (string, error) {
|
||||
}
|
||||
|
||||
func decodeReviews(in chan review, out chan review, scr scraper) {
|
||||
for {
|
||||
if res, ok := <-in; ok {
|
||||
var err error
|
||||
res.latitude, res.longitude, err = scr.decode(res.address)
|
||||
if err == nil {
|
||||
out <- res
|
||||
} else {
|
||||
log.Printf("failed to decode address for %s (%v)", res.url, err)
|
||||
for rev, ok := <-in; ok; {
|
||||
if rev.err == nil {
|
||||
rev.latitude, rev.longitude, rev.err = scr.decode(rev.address)
|
||||
}
|
||||
} else {
|
||||
|
||||
out <- rev
|
||||
}
|
||||
|
||||
close(out)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func scrapeReview(url string, out chan review, scr scraper, group *sync.WaitGroup) {
|
||||
defer group.Done()
|
||||
|
||||
doc, err := scr.load(url)
|
||||
if err != nil {
|
||||
log.Printf("failed to load review at %s (%v)", url, err)
|
||||
return
|
||||
var (
|
||||
doc *goquery.Document
|
||||
rev = review{url: url}
|
||||
)
|
||||
|
||||
if doc, rev.err = scr.load(rev.url); rev.err == nil {
|
||||
rev.name, rev.address, rev.features, rev.err = scr.review(doc)
|
||||
}
|
||||
|
||||
name, address, features, err := scr.review(doc)
|
||||
if err != nil {
|
||||
log.Printf("failed to scrape review at %s (%v)", url, err)
|
||||
return
|
||||
}
|
||||
|
||||
out <- review{
|
||||
name: name,
|
||||
address: address,
|
||||
features: features,
|
||||
url: url}
|
||||
out <- rev
|
||||
}
|
||||
|
||||
func scrapeIndex(indexUrl string, out chan review, scr scraper) {
|
||||
func scrapeIndex(indexUrl string, out chan review, scr scraper) error {
|
||||
var group sync.WaitGroup
|
||||
|
||||
defer func() {
|
||||
group.Wait()
|
||||
close(out)
|
||||
}()
|
||||
|
||||
for {
|
||||
doc, err := scr.load(indexUrl)
|
||||
if err != nil {
|
||||
log.Printf("failed to load index at %s (%v)", indexUrl, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
nextIndexUrl, reviewUrls := scr.index(doc)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var group sync.WaitGroup
|
||||
for _, reviewUrl := range reviewUrls {
|
||||
absUrl, err := makeAbsUrl(reviewUrl, indexUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
group.Add(1)
|
||||
go scrapeReview(absUrl, out, scr, &group)
|
||||
}
|
||||
group.Wait()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nextIndexUrl == "" {
|
||||
close(out)
|
||||
} else {
|
||||
absUrl, err := makeAbsUrl(nextIndexUrl, indexUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
break
|
||||
}
|
||||
|
||||
scrapeIndex(absUrl, out, scr)
|
||||
indexUrl, err = makeAbsUrl(nextIndexUrl, indexUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func scrape(url string, scr scraper) []review {
|
||||
@ -152,12 +151,10 @@ func scrape(url string, scr scraper) []review {
|
||||
go scrapeIndex(url, in, scr)
|
||||
go decodeReviews(in, out, scr)
|
||||
|
||||
var results []review
|
||||
for {
|
||||
if res, ok := <-out; ok {
|
||||
results = append(results, res)
|
||||
} else {
|
||||
return results
|
||||
}
|
||||
var reviews []review
|
||||
for rev, ok := <-out; ok; {
|
||||
reviews = append(reviews, rev)
|
||||
}
|
||||
|
||||
return reviews
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user