1
This commit is contained in:
Alex Yatskov 2015-08-22 18:14:42 +09:00
parent 1b745426df
commit 6b010e7dfc

View File

@ -25,7 +25,6 @@ package main
import ( import (
"bufio" "bufio"
"errors" "errors"
"log"
"net/url" "net/url"
"os" "os"
) )
@ -38,7 +37,8 @@ func scrapeUrls(filename string, wc *webCache, gc *geoCache) ([]restaurant, erro
defer file.Close() defer file.Close()
var results []restaurant var results []restaurant
scanner := bufio.NewScanner(file) var scanner = bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
if line := scanner.Text(); len(line) > 0 { if line := scanner.Text(); len(line) > 0 {
parsed, err := url.Parse(line) parsed, err := url.Parse(line)
@ -63,32 +63,47 @@ func scrapeUrls(filename string, wc *webCache, gc *geoCache) ([]restaurant, erro
return results, nil return results, nil
} }
func main() { func scrapeData(urlsPath, geocachePath, webcachePath string) ([]restaurant, error) {
gc, err := newGeoCache("cache/geocache.json") gc, err := newGeoCache(geocachePath)
if err != nil { if err != nil {
panic(err) return nil, err
} }
defer gc.save() defer gc.save()
wc, err := newWebCache("cache/webcache") wc, err := newWebCache(webcachePath)
if err != nil { if err != nil {
panic(err) return nil, err
} }
sq, err := newStationQuery("data/stations.json") restaurants, err := scrapeUrls(urlsPath, wc, gc)
if err != nil { if err != nil {
panic(err) return nil, err
}
return restaurants, nil
}
func processData(restaurants []restaurant, stationsPath string) error {
sq, err := newStationQuery(stationsPath)
if err != nil {
return err
} }
restaurants, err := scrapeUrls("data/urls.txt", wc, gc)
for i, _ := range restaurants { for i, _ := range restaurants {
r := &restaurants[i] r := &restaurants[i]
r.closestStnName, r.closestStnDist = sq.closestStation(r.latitude, r.longitude) r.closestStnName, r.closestStnDist = sq.closestStation(r.latitude, r.longitude)
} }
if err == nil { return nil
log.Print(len(restaurants)) }
} else {
func main() {
restaraunts, err := scrapeData("data/urls.txt", "cache/geocache.json", "cache/webcache")
if err != nil {
panic(err)
}
if err := processData(restaraunts, "data/stations.json"); err != nil {
panic(err) panic(err)
} }
} }