1
This commit is contained in:
Alex Yatskov 2015-09-20 11:38:35 +09:00
parent 8212b071ec
commit ac8c22aadd
2 changed files with 31 additions and 8 deletions

View File

@ -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) { func (tabelog) review(doc *goquery.Document) (name, address string, features map[string]float64, weight float64, err error) {
weight = 1.0 if name = doc.Find("a.rd-header__rst-name-main").Text(); len(name) == 0 {
name = doc.Find("a.rd-header__rst-name-main").Text() err = errors.New("invalid value for name")
return
}
if addresses := doc.Find("p.rd-detail-info__rst-address"); addresses.Length() == 2 { if addresses := doc.Find("p.rd-detail-info__rst-address"); addresses.Length() == 2 {
address = strings.TrimSpace(addresses.First().Text()) 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) features = make(map[string]float64)
for index, category := range []string{"dishes", "service", "atmosphere", "cost", "drinks"} { 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 var value float64
if value, err = strconv.ParseFloat(valueText, 8); err != nil { 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 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 return
} }

View File

@ -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) { func (tripadvisor) review(doc *goquery.Document) (name, address string, features map[string]float64, weight float64, err error) {
weight = 1.0 if name = strings.TrimSpace(doc.Find("h1#HEADING").Text()); len(name) == 0 {
name = strings.TrimSpace(doc.Find("h1#HEADING").Text()) err = errors.New("invalid value for name name")
address = strings.TrimSpace(doc.Find("address span.format_address").Text()) 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") ratings := doc.Find("ul.barChart div.ratingRow img.sprite-rating_s_fill")
if ratings.Length() != 4 { if ratings.Length() != 4 {
@ -72,7 +78,6 @@ func (tripadvisor) review(doc *goquery.Document) (name, address string, features
} }
features = make(map[string]float64) features = make(map[string]float64)
for index, category := range []string{"food", "service", "value", "atmosphere"} { for index, category := range []string{"food", "service", "value", "atmosphere"} {
altText, _ := ratings.Eq(index).Attr("alt") altText, _ := ratings.Eq(index).Attr("alt")
valueText := strings.Split(altText, " ")[0] 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 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 return
} }