1
This commit is contained in:
Alex Yatskov 2015-09-18 15:41:15 +09:00
parent 9e9bd99f0b
commit 227329c187
2 changed files with 57 additions and 17 deletions

View File

@ -24,8 +24,11 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"encoding/binary"
"errors" "errors"
"flag" "flag"
"hash/fnv"
"log" "log"
"net/url" "net/url"
"os" "os"
@ -47,6 +50,18 @@ func (s scrapeCtx) load(url string) (*goquery.Document, error) {
return s.wc.load(url) return s.wc.load(url)
} }
type restaurant struct {
reviews []review
accomodating float64
affordable float64
atmospheric float64
delicious float64
closestStnName string
closestStnDist float64
}
func scrapeData(urlsPath, geocachePath, webcachePath string) ([]review, error) { func scrapeData(urlsPath, geocachePath, webcachePath string) ([]review, error) {
gc, err := newGeoCache(geocachePath) gc, err := newGeoCache(geocachePath)
if err != nil { if err != nil {
@ -98,19 +113,45 @@ func scrapeData(urlsPath, geocachePath, webcachePath string) ([]review, error) {
return reviews, nil return reviews, nil
} }
// func computeStnData(reviews []restaurant, stationsPath string) error { func collateData(reviews []review) map[uint64]*restaurant {
// sq, err := newStationQuery(stationsPath) restaurants := make(map[uint64]*restaurant)
// if err != nil {
// return err
// }
// for i, _ := range reviews { for _, rev := range reviews {
// r := &reviews[i] var buff bytes.Buffer
// r.closestStnName, r.closestStnDist = sq.closestStation(r.latitude, r.longitude) binary.Write(&buff, binary.LittleEndian, rev.latitude)
// } binary.Write(&buff, binary.LittleEndian, rev.longitude)
binary.Write(&buff, binary.LittleEndian, rev.name)
// return nil hash := fnv.New64()
// } hash.Write(buff.Bytes())
var rest *restaurant
if rest, _ = restaurants[hash.Sum64()]; rest == nil {
rest = new(restaurant)
restaurants[hash.Sum64()] = rest
}
rest.reviews = append(rest.reviews, rev)
}
return restaurants
}
func computeStnData(restaurants map[uint64]*restaurant, stationsPath string) error {
sq, err := newStationQuery(stationsPath)
if err != nil {
return err
}
for _, rest := range restaurants {
if len(rest.reviews) > 0 {
rev := rest.reviews[0]
rest.closestStnName, rest.closestStnDist = sq.closestStation(rev.latitude, rev.longitude)
}
}
return nil
}
// func dumpData(dbPath string, restaraunts []restaurant) error { // func dumpData(dbPath string, restaraunts []restaurant) error {
// db, err := sql.Open("sqlite3", dbPath) // db, err := sql.Open("sqlite3", dbPath)
@ -229,9 +270,11 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
// if err := computeStnData(reviews, *stationsPath); err != nil { restaurants := collateData(reviews)
// log.Fatal(err)
// } if err := computeStnData(restaurants, *stationsPath); err != nil {
log.Fatal(err)
}
// if err := dumpData(*dbPath, reviews); err != nil { // if err := dumpData(*dbPath, reviews); err != nil {
// log.Fatal(err) // log.Fatal(err)

View File

@ -44,9 +44,6 @@ type review struct {
latitude float64 latitude float64
longitude float64 longitude float64
closestStnName string
closestStnDist float64
err error err error
} }