From ac8c22aadd3a0819f87126ec8f2283d6337cf590 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 20 Sep 2015 11:38:35 +0900 Subject: [PATCH] WIP --- build/tabelog.go | 15 +++++++++++---- build/tripadvisor.go | 24 ++++++++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/build/tabelog.go b/build/tabelog.go index 928589c..cd83148 100644 --- a/build/tabelog.go +++ b/build/tabelog.go @@ -62,8 +62,10 @@ func (tabelog) index(doc *goquery.Document) (string, []string) { } func (tabelog) review(doc *goquery.Document) (name, address string, features map[string]float64, weight float64, err error) { - weight = 1.0 - name = doc.Find("a.rd-header__rst-name-main").Text() + if name = doc.Find("a.rd-header__rst-name-main").Text(); len(name) == 0 { + err = errors.New("invalid value for name") + return + } if addresses := doc.Find("p.rd-detail-info__rst-address"); addresses.Length() == 2 { address = strings.TrimSpace(addresses.First().Text()) @@ -73,9 +75,8 @@ func (tabelog) review(doc *goquery.Document) (name, address string, features map } features = make(map[string]float64) - for index, category := range []string{"dishes", "service", "atmosphere", "cost", "drinks"} { - valueText := doc.Find(fmt.Sprintf("#js-rating-detail > dd:nth-child(%d)", (index+1)*2)).Text() + valueText := doc.Find(fmt.Sprintf("dl#js-rating-detail > dd:nth-child(%d)", (index+1)*2)).Text() var value float64 if value, err = strconv.ParseFloat(valueText, 8); err != nil { @@ -86,5 +87,11 @@ func (tabelog) review(doc *goquery.Document) (name, address string, features map features[category] = value/2.5 - 1.0 } + weight, err = strconv.ParseFloat(doc.Find("a.rd-header__rst-reviews-target > b").Text(), 8) + if err != nil { + err = fmt.Errorf("invalid value for review count") + return + } + return } diff --git a/build/tripadvisor.go b/build/tripadvisor.go index 6f413c6..1daf529 100644 --- a/build/tripadvisor.go +++ b/build/tripadvisor.go @@ -61,9 +61,15 @@ func (tripadvisor) index(doc *goquery.Document) (string, []string) { } func (tripadvisor) review(doc *goquery.Document) (name, address string, features map[string]float64, weight float64, err error) { - weight = 1.0 - name = strings.TrimSpace(doc.Find("h1#HEADING").Text()) - address = strings.TrimSpace(doc.Find("address span.format_address").Text()) + if name = strings.TrimSpace(doc.Find("h1#HEADING").Text()); len(name) == 0 { + err = errors.New("invalid value for name name") + return + } + + if address = strings.TrimSpace(doc.Find("address span.format_address").Text()); len(address) == 0 { + err = errors.New("invalid value for address") + return + } ratings := doc.Find("ul.barChart div.ratingRow img.sprite-rating_s_fill") if ratings.Length() != 4 { @@ -72,7 +78,6 @@ func (tripadvisor) review(doc *goquery.Document) (name, address string, features } features = make(map[string]float64) - for index, category := range []string{"food", "service", "value", "atmosphere"} { altText, _ := ratings.Eq(index).Attr("alt") valueText := strings.Split(altText, " ")[0] @@ -86,5 +91,16 @@ func (tripadvisor) review(doc *goquery.Document) (name, address string, features features[category] = value/2.5 - 1.0 } + weightParts := strings.Split(doc.Find("h3.reviews_header").Text(), " ") + if len(weightParts) == 0 { + err = fmt.Errorf("missing review count") + return + } + + if weight, err = strconv.ParseFloat(weightParts[0], 8); err != nil { + err = fmt.Errorf("invalid value for review count") + return + } + return }