-
Notifications
You must be signed in to change notification settings - Fork 0
/
what_bird.go
149 lines (136 loc) · 5.08 KB
/
what_bird.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package bird_data_guessing
import (
"fmt"
"strings"
"github.com/gbdubs/amass"
"github.com/gbdubs/attributions"
"github.com/gbdubs/bird"
)
type whatBirdResponse struct {
Overview amass.GetResponse
Identification amass.GetResponse
Behavior amass.GetResponse
}
const (
whatBirdSite = "what_bird"
whatBirdIdentificationPage = "identification"
whatBirdBehaviorPage = "behavior"
whatBirdOverviewPage = "overview"
whatBirdMaxConcurrentRequests = 2
)
func createWhatBirdRequests(birdName bird.BirdName) []*amass.GetRequest {
nameParam := strings.ToLower(strings.ReplaceAll(birdName.English, " ", "_"))
whatBirdId := whatBirdIdMap[nameParam]
if whatBirdId == 0 {
nameParam = strings.ReplaceAll(nameParam, "'", "")
whatBirdId = whatBirdIdMap[nameParam]
}
if whatBirdId == 0 {
recordMissing(whatBirdSite, birdName)
return []*amass.GetRequest{}
}
makeReq := func(page string) *amass.GetRequest {
url := fmt.Sprintf(
"https://identify.whatbird.com/obj/%d/%s/%s.aspx",
whatBirdId, page, nameParam)
req := &amass.GetRequest{
Site: whatBirdSite,
RequestKey: nameParam + "_" + page,
URL: url,
SiteMaxConcurrentRequests: whatBirdMaxConcurrentRequests,
Attribution: attributions.Attribution{
Author: "Mitch Waite Group",
AuthorUrl: "http://www.whatbird.com",
License: "Copyright 2002 - 2013, All Rights Reserved, Mitch Waite Group",
ScrapingMethodology: "github.com/gbdubs/bird_data_guessing/what_bird",
},
}
req.SetRoundTripData(birdName)
return req
}
return []*amass.GetRequest{
makeReq(whatBirdOverviewPage),
makeReq(whatBirdIdentificationPage),
makeReq(whatBirdBehaviorPage),
}
}
func reconstructWhatBirdsResponsesKeyedByEnglishName(responses []*amass.GetResponse) map[string]*whatBirdResponse {
m := make(map[string]map[string]*amass.GetResponse)
m[whatBirdOverviewPage] = make(map[string]*amass.GetResponse)
m[whatBirdIdentificationPage] = make(map[string]*amass.GetResponse)
m[whatBirdBehaviorPage] = make(map[string]*amass.GetResponse)
englishNames := make(map[string]bool)
for _, response := range responses {
if response.Site != whatBirdSite {
continue
}
page := ""
if strings.HasSuffix(response.RequestKey, whatBirdIdentificationPage) {
page = whatBirdIdentificationPage
} else if strings.HasSuffix(response.RequestKey, whatBirdOverviewPage) {
page = whatBirdOverviewPage
} else if strings.HasSuffix(response.RequestKey, whatBirdBehaviorPage) {
page = whatBirdBehaviorPage
} else {
panic(fmt.Errorf("Unrecongnized response request key %s for what bird.", response.RequestKey))
}
birdName := &bird.BirdName{}
response.GetRoundTripData(birdName)
englishName := birdName.English
englishNames[englishName] = true
m[page][englishName] = response
}
result := make(map[string]*whatBirdResponse)
for englishName, _ := range englishNames {
result[englishName] = &whatBirdResponse{
Identification: *m[whatBirdIdentificationPage][englishName],
Behavior: *m[whatBirdBehaviorPage][englishName],
Overview: *m[whatBirdOverviewPage][englishName],
}
}
return result
}
func whatBirdRequestForTesting(englishName string) *whatBirdResponse {
bn := bird.BirdName{English: englishName}
rs := createWhatBirdRequests(bn)
if len(rs) != 3 {
panic(fmt.Errorf("Expected 3 what bird request, was %d, for key %s.", len(rs), englishName))
}
resps, err := amass.AmasserForTests().GetAll(rs)
if err != nil {
panic(fmt.Errorf("GetAll request failed for %s: %v", englishName, err))
}
m := reconstructWhatBirdsResponsesKeyedByEnglishName(resps)
result, ok := m[englishName]
if !ok {
panic(fmt.Errorf("Expected key %s in map, but map was %+v.", englishName, m))
}
return result
}
func (r *whatBirdResponse) propertySearchers() *propertySearchers {
overview := r.Overview.AsDocument()
identification := r.Identification.AsDocument()
behavior := r.Behavior.AsDocument()
wingspanText := identification.Find("li:contains('Wingspan Range')").First().Text()
behaviorHeadingSearcher := func(s string) searcher {
return attributedSearch(
&r.Behavior.Attribution,
behavior.Find("h3:contains('"+s+"')").First().Next().Text())
}
behaviorText := behavior.Find("#behavior").Text()
behaviorSearcher := attributedSearch(&r.Behavior.Attribution, behaviorText)
behaviorOverviewText := behaviorText + overview.Find("#overview").Text()
behaviorOverviewSearcher := attributedSearch(&r.Overview.Attribution, behaviorOverviewText)
funFactText := overview.Find("h2:contains('INTERESTING FACTS')").First().Next().Text()
return &propertySearchers{
wingspan: attributedSearch(&r.Identification.Attribution, wingspanText),
clutchSize: behaviorSearcher,
eggColor: behaviorSearcher,
funFact: attributedSearch(&r.Overview.Attribution, funFactText),
food: behaviorHeadingSearcher("Forraging and Feeding"),
nestType: behaviorHeadingSearcher("Nest Location"),
habitat: behaviorHeadingSearcher("Range and Habitat"),
predator: behaviorOverviewSearcher,
flocking: behaviorOverviewSearcher,
}
}