2015-08-12 08:53:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-08-12 09:25:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-08-12 08:53:53 +00:00
|
|
|
"log"
|
2015-08-13 07:29:39 +00:00
|
|
|
"net/url"
|
2015-08-12 08:53:53 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-12 11:29:45 +00:00
|
|
|
"sync"
|
2015-08-12 08:53:53 +00:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tabelogParams struct {
|
|
|
|
Page int
|
|
|
|
}
|
|
|
|
|
|
|
|
type tabelogReview struct {
|
2015-08-13 10:18:17 +00:00
|
|
|
Name string
|
|
|
|
Address string
|
|
|
|
Url string
|
|
|
|
|
2015-08-12 09:25:55 +00:00
|
|
|
Dishes float64
|
|
|
|
Service float64
|
|
|
|
Atmosphere float64
|
|
|
|
Cost float64
|
|
|
|
Drinks float64
|
2015-08-13 10:18:17 +00:00
|
|
|
|
|
|
|
Latitude float64
|
|
|
|
Longitude float64
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
func makeAbsUrl(base, ref string) (string, error) {
|
2015-08-13 07:29:39 +00:00
|
|
|
b, err := url.Parse(base)
|
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
return "", err
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := url.Parse(ref)
|
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
return "", err
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
2015-08-12 11:39:13 +00:00
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
return b.ResolveReference(r).String(), nil
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
func dumpReviews(filename string, in chan tabelogReview) error {
|
2015-08-12 11:29:45 +00:00
|
|
|
var reviews []tabelogReview
|
|
|
|
for {
|
2015-08-14 07:40:37 +00:00
|
|
|
if review, ok := <-in; ok {
|
2015-08-12 11:29:45 +00:00
|
|
|
reviews = append(reviews, review)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := json.MarshalIndent(reviews, "", " ")
|
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
return err
|
2015-08-12 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 07:29:39 +00:00
|
|
|
if err := ioutil.WriteFile(filename, js, 0644); err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
return err
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
2015-08-14 10:57:44 +00:00
|
|
|
|
|
|
|
return nil
|
2015-08-12 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 08:27:09 +00:00
|
|
|
func decodeReviews(in chan tabelogReview, out chan tabelogReview, gc *geoCache) {
|
2015-08-14 07:40:37 +00:00
|
|
|
for {
|
|
|
|
if review, ok := <-in; ok {
|
|
|
|
coord, err := gc.decode(review.Address)
|
2015-08-14 10:57:44 +00:00
|
|
|
if err == nil {
|
|
|
|
review.Latitude = coord.Latitude
|
|
|
|
review.Longitude = coord.Longitude
|
|
|
|
out <- review
|
|
|
|
} else {
|
|
|
|
log.Printf("failed to decode address for %s (%v)", review.Url, err)
|
2015-08-14 07:40:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
close(out)
|
2015-08-14 07:56:44 +00:00
|
|
|
return
|
2015-08-14 07:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 08:27:09 +00:00
|
|
|
func scrapeReview(url string, out chan tabelogReview, wc *webCache, wg *sync.WaitGroup) {
|
2015-08-12 11:29:45 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2015-08-13 10:18:17 +00:00
|
|
|
doc, err := wc.load(url)
|
2015-08-12 08:53:53 +00:00
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
log.Printf("failed to scrape review at %s (%v)", url, err)
|
|
|
|
return
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 11:29:45 +00:00
|
|
|
addresses := doc.Find("p.rd-detail-info__rst-address")
|
|
|
|
if addresses.Length() != 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var review tabelogReview
|
2015-08-12 08:53:53 +00:00
|
|
|
|
2015-08-12 11:29:45 +00:00
|
|
|
review.Url = url
|
2015-08-13 07:29:39 +00:00
|
|
|
review.Name = doc.Find("a.rd-header__rst-name-main").Text()
|
2015-08-12 11:29:45 +00:00
|
|
|
review.Address = strings.TrimSpace(addresses.First().Text())
|
2015-08-12 08:53:53 +00:00
|
|
|
|
2015-08-12 11:29:45 +00:00
|
|
|
if review.Dishes, err = strconv.ParseFloat(doc.Find("#js-rating-detail > dd:nth-child(2)").Text(), 8); err != nil {
|
2015-08-12 08:53:53 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-12 11:29:45 +00:00
|
|
|
if review.Service, err = strconv.ParseFloat(doc.Find("#js-rating-detail > dd:nth-child(4)").Text(), 8); err != nil {
|
2015-08-12 08:53:53 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-12 11:29:45 +00:00
|
|
|
if review.Atmosphere, err = strconv.ParseFloat(doc.Find("#js-rating-detail > dd:nth-child(6)").Text(), 8); err != nil {
|
2015-08-12 08:53:53 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-12 11:29:45 +00:00
|
|
|
if review.Cost, err = strconv.ParseFloat(doc.Find("#js-rating-detail > dd:nth-child(8)").Text(), 8); err != nil {
|
2015-08-12 08:53:53 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-12 11:29:45 +00:00
|
|
|
if review.Drinks, err = strconv.ParseFloat(doc.Find("#js-rating-detail > dd:nth-child(10)").Text(), 8); err != nil {
|
2015-08-12 08:53:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-14 07:40:37 +00:00
|
|
|
out <- review
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 08:27:09 +00:00
|
|
|
func scrapeIndex(url string, out chan tabelogReview, wc *webCache, wg *sync.WaitGroup) {
|
2015-08-13 10:18:17 +00:00
|
|
|
doc, err := wc.load(url)
|
2015-08-12 08:53:53 +00:00
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
log.Printf("failed to scrape index at %s (%v)", url, err)
|
|
|
|
return
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 11:29:45 +00:00
|
|
|
doc.Find("div.list-rst__header > p > a").Each(func(index int, sel *goquery.Selection) {
|
2015-08-12 08:53:53 +00:00
|
|
|
if href, ok := sel.Attr("href"); ok {
|
2015-08-12 11:29:45 +00:00
|
|
|
wg.Add(1)
|
2015-08-14 10:57:44 +00:00
|
|
|
|
|
|
|
absUrl, err := makeAbsUrl(url, href)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go scrapeReview(absUrl, out, wc, wg)
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-08-14 01:10:49 +00:00
|
|
|
if href, ok := doc.Find("a.c-pagination__target--next").Attr("href"); ok {
|
2015-08-14 10:57:44 +00:00
|
|
|
absUrl, err := makeAbsUrl(url, href)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-08-12 08:53:53 +00:00
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
scrapeIndex(absUrl, out, wc, wg)
|
2015-08-13 10:18:17 +00:00
|
|
|
}
|
2015-08-14 10:57:44 +00:00
|
|
|
}
|
2015-08-13 10:18:17 +00:00
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
func scrapeReviews(url string, out chan tabelogReview, wc *webCache) error {
|
2015-08-14 08:27:09 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
scrapeIndex(url, out, wc, &wg)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
close(out)
|
2015-08-14 10:57:44 +00:00
|
|
|
return nil
|
2015-08-14 08:27:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
func scrapeTabelog(url, resultFile, webCacheDir, geoCacheFile string) error {
|
|
|
|
wc, err := newWebCache(webCacheDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-13 10:18:17 +00:00
|
|
|
gc, err := newGeoCache(geoCacheFile)
|
2015-08-13 07:29:39 +00:00
|
|
|
if err != nil {
|
2015-08-14 10:57:44 +00:00
|
|
|
return err
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
2015-08-12 08:53:53 +00:00
|
|
|
|
2015-08-14 08:27:09 +00:00
|
|
|
scrapeChan := make(chan tabelogReview, 2000)
|
|
|
|
decodeChan := make(chan tabelogReview, 2000)
|
2015-08-14 07:40:37 +00:00
|
|
|
|
2015-08-14 08:27:09 +00:00
|
|
|
go decodeReviews(scrapeChan, decodeChan, gc)
|
2015-08-14 10:57:44 +00:00
|
|
|
scrapeReviews(url, scrapeChan, wc)
|
2015-08-14 08:27:09 +00:00
|
|
|
dumpReviews(resultFile, decodeChan)
|
2015-08-13 10:18:17 +00:00
|
|
|
|
2015-08-14 10:57:44 +00:00
|
|
|
return gc.save()
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|