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-17 04:59:19 +00:00
|
|
|
"errors"
|
2015-08-17 06:47:20 +00:00
|
|
|
"fmt"
|
2015-08-12 08:53:53 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2015-08-16 10:02:59 +00:00
|
|
|
type tabelog struct {
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 09:19:39 +00:00
|
|
|
func (tabelog) define(keyword string) semantics {
|
|
|
|
return map[string]semantics{
|
2015-09-20 11:02:03 +00:00
|
|
|
"dishes": {Accomodating: 0.0, Affordable: 0.0, Atmospheric: 0.0, Delicious: 0.8},
|
|
|
|
"drinks": {Accomodating: 0.0, Affordable: 0.0, Atmospheric: 0.0, Delicious: 0.2},
|
|
|
|
"service": {Accomodating: 1.0, Affordable: 0.0, Atmospheric: 0.0, Delicious: 0.0},
|
|
|
|
"cost": {Accomodating: 0.0, Affordable: 1.0, Atmospheric: 0.0, Delicious: 0.0},
|
|
|
|
"atmosphere": {Accomodating: 0.0, Affordable: 0.0, Atmospheric: 1.0, Delicious: 0.0},
|
2015-09-18 09:19:39 +00:00
|
|
|
}[keyword]
|
|
|
|
}
|
|
|
|
|
2015-08-16 10:12:16 +00:00
|
|
|
func (tabelog) index(doc *goquery.Document) (string, []string) {
|
2015-08-16 10:02:59 +00:00
|
|
|
var reviewUrls []string
|
|
|
|
doc.Find("div.list-rst__header > p > a").Each(func(index int, sel *goquery.Selection) {
|
|
|
|
if href, ok := sel.Attr("href"); ok {
|
|
|
|
reviewUrls = append(reviewUrls, href)
|
2015-08-12 11:29:45 +00:00
|
|
|
}
|
2015-08-16 10:02:59 +00:00
|
|
|
})
|
2015-08-12 11:29:45 +00:00
|
|
|
|
2015-08-16 10:02:59 +00:00
|
|
|
var nextIndexUrl string
|
|
|
|
if href, ok := doc.Find("a.c-pagination__target--next").Attr("href"); ok {
|
|
|
|
nextIndexUrl = href
|
2015-08-13 07:29:39 +00:00
|
|
|
}
|
2015-08-14 10:57:44 +00:00
|
|
|
|
2015-08-16 10:02:59 +00:00
|
|
|
return nextIndexUrl, reviewUrls
|
2015-08-14 07:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 09:41:01 +00:00
|
|
|
func (tabelog) review(doc *goquery.Document) (name, address string, features map[string]float64, weight float64, err error) {
|
2015-09-20 02:38:35 +00:00
|
|
|
if name = doc.Find("a.rd-header__rst-name-main").Text(); len(name) == 0 {
|
2015-09-20 03:08:44 +00:00
|
|
|
err = errors.New("invalid name")
|
2015-09-20 02:38:35 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-12 11:29:45 +00:00
|
|
|
|
2015-08-16 10:02:59 +00:00
|
|
|
if addresses := doc.Find("p.rd-detail-info__rst-address"); addresses.Length() == 2 {
|
2015-08-17 04:59:19 +00:00
|
|
|
address = strings.TrimSpace(addresses.First().Text())
|
2015-08-16 10:02:59 +00:00
|
|
|
} else {
|
2015-09-20 03:08:44 +00:00
|
|
|
err = errors.New("invalid address")
|
2015-08-17 04:59:19 +00:00
|
|
|
return
|
2015-08-12 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 09:41:01 +00:00
|
|
|
features = make(map[string]float64)
|
2015-08-17 06:47:20 +00:00
|
|
|
for index, category := range []string{"dishes", "service", "atmosphere", "cost", "drinks"} {
|
2015-09-20 02:38:35 +00:00
|
|
|
valueText := doc.Find(fmt.Sprintf("dl#js-rating-detail > dd:nth-child(%d)", (index+1)*2)).Text()
|
2015-09-17 06:54:32 +00:00
|
|
|
|
|
|
|
var value float64
|
|
|
|
if value, err = strconv.ParseFloat(valueText, 8); err != nil {
|
2015-09-20 03:08:44 +00:00
|
|
|
err = fmt.Errorf("invalid rating for %s", category)
|
2015-08-17 09:42:26 +00:00
|
|
|
return
|
2015-08-17 06:47:20 +00:00
|
|
|
}
|
2015-08-12 08:53:53 +00:00
|
|
|
|
2015-09-18 09:41:01 +00:00
|
|
|
features[category] = value/2.5 - 1.0
|
2015-09-17 06:54:32 +00:00
|
|
|
}
|
2015-08-24 08:03:00 +00:00
|
|
|
|
2015-09-20 02:38:35 +00:00
|
|
|
weight, err = strconv.ParseFloat(doc.Find("a.rd-header__rst-reviews-target > b").Text(), 8)
|
|
|
|
if err != nil {
|
2015-09-20 03:08:44 +00:00
|
|
|
err = fmt.Errorf("invalid review count")
|
2015-09-20 02:38:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-17 04:59:19 +00:00
|
|
|
return
|
2015-08-12 08:53:53 +00:00
|
|
|
}
|