-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathvars.go
362 lines (303 loc) · 11.5 KB
/
vars.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gogtrends
import (
"sort"
"strconv"
"strings"
)
const (
gAPI = "https://trends.google.com/trends/api"
gDaily = "/dailytrends"
gRealtime = "/realtimetrends"
gSExplore = "/explore"
gSCategories = "/explore/pickers/category"
gSGeo = "/explore/pickers/geo"
gSRelated = "/widgetdata/relatedsearches"
gSIntOverTime = "/widgetdata/multiline"
gSIntOverReg = "/widgetdata/comparedgeo"
gSAutocomplete = "/autocomplete"
paramHl = "hl"
paramCat = "cat"
paramGeo = "geo"
paramReq = "req"
paramTZ = "tz"
paramToken = "token"
compareDataMode = "PERCENTAGES"
)
type WidgetType string
const (
IntOverTimeWidgetID WidgetType = "TIMESERIES"
IntOverRegionID WidgetType = "GEO_MAP"
RelatedQueriesID WidgetType = "RELATED_QUERIES"
RelatedTopicsID WidgetType = "RELATED_TOPICS"
)
var (
defaultParams = map[string]string{
paramTZ: "0",
paramCat: "all",
"fi": "0",
"fs": "0",
paramHl: "EN",
"ri": "300",
"rs": "20",
}
trendsCategories = map[string]string{
"all": "all",
"b": "business",
"h": "main news",
"m": "health",
"t": "science and technics",
"e": "entertainment",
"s": "sport",
}
)
type dailyOut struct {
Default *trendingSearchesDays `json:"default" bson:"default"`
}
type trendingSearchesDays struct {
Searches []*trendingSearchDays `json:"trendingSearchesDays" bson:"trending_search_days"`
}
type trendingSearchDays struct {
FormattedDate string `json:"formattedDate" bson:"formatted_date"`
Searches []*TrendingSearch `json:"trendingSearches" bson:"searches"`
}
// TrendingSearch is a representation trending search in period of 24 hours
type TrendingSearch struct {
Title *SearchTitle `json:"title" bson:"title"`
FormattedTraffic string `json:"formattedTraffic" bson:"formatted_traffic"`
Image *SearchImage `json:"image" bson:"image"`
Articles []*SearchArticle `json:"articles" bson:"articles"`
}
// SearchTitle is a user query string for daily trending search
type SearchTitle struct {
Query string `json:"query" bson:"query"`
}
// SearchImage is a picture of trending search
type SearchImage struct {
NewsURL string `json:"newsUrl" bson:"news_url"`
Source string `json:"source" bson:"source"`
ImageURL string `json:"imageUrl" bson:"image_url"`
}
// SearchArticle is a news relative to trending search
type SearchArticle struct {
Title string `json:"title" bson:"title"`
TimeAgo string `json:"timeAgo" bson:"time_ago"`
Source string `json:"source" bson:"source"`
Image *SearchImage `json:"image" bson:"image"`
URL string `json:"url" bson:"url"`
Snippet string `json:"snippet" bson:"snippet"`
}
type realtimeOut struct {
StorySummaries *storySummary `json:"storySummaries" bson:"story_summaries"`
}
type storySummary struct {
TrendingStories []*TrendingStory `json:"trendingStories" bson:"trending_stories"`
}
// TrendingStory is a representation of realtime trend
type TrendingStory struct {
Title string `json:"title" bson:"title"`
Image *SearchImage `json:"image" bson:"image"`
Articles []*TrendingArticle `json:"articles" bson:"articles"`
}
// TrendingArticle is an article relative to trending story
type TrendingArticle struct {
Title string `json:"articleTitle" bson:"title"`
URL string `json:"url" bson:"url"`
Source string `json:"source" bson:"source"`
Time string `json:"time" bson:"time"`
Snippet string `json:"snippet" bson:"snippet"`
}
// ExploreRequest it's an input which can contain multiple items (keywords) to discover
// category can be found in ExploreCategories output
type ExploreRequest struct {
ComparisonItems []*ComparisonItem `json:"comparisonItem" bson:"comparison_items"`
Category int `json:"category" bson:"category"`
Property string `json:"property" bson:"property"`
}
// ComparisonItem it's concrete search keyword
// with Geo (can be found with ExploreLocations method) locality and Time period
type ComparisonItem struct {
Keyword string `json:"keyword" bson:"keyword"`
Geo string `json:"geo,omitempty" bson:"geo"`
Time string `json:"time" bson:"time"`
GranularTimeResolution bool `json:"granularTimeResolution" bson:"granular_time_resolution"`
StartTime string `json:"startTime" bson:"start_time"`
EndTime string `json:"endTime" bson:"end_time"`
}
// ExploreCatTree - available categories list tree
type ExploreCatTree struct {
Name string `json:"name" bson:"name"`
ID int `json:"id" bson:"id"`
Children []*ExploreCatTree `json:"children" bson:"children"`
}
// ExploreLocTree - available locations list tree
type ExploreLocTree struct {
Name string `json:"name" bson:"name"`
ID string `json:"id" bson:"id"`
Children []*ExploreLocTree `json:"children" bson:"children"`
}
type exploreOut struct {
Widgets []*ExploreWidget `json:"widgets" bson:"widgets"`
}
// ExploreWidget - output of Explore method, required for InterestOverTime, InterestByLocation and Related methods.
// Globally it's a structure related to Google Trends UI and contains mostly system info
type ExploreWidget struct {
Token string `json:"token" bson:"token"`
Type string `json:"type" bson:"type"`
Title string `json:"title" bson:"title"`
ID string `json:"id" bson:"id"`
Request *WidgetResponse `json:"request" bson:"request"`
}
type ExploreResponse []*ExploreWidget
func (e ExploreResponse) Sort() {
sort.Sort(e)
}
func (e ExploreResponse) Len() int {
return len(e)
}
func (e ExploreResponse) Less(i, j int) bool {
numI := strings.LastIndex(e[i].ID, "_")
if numI < 0 {
return true
}
numJ := strings.LastIndex(e[j].ID, "_")
if numJ < 0 {
return false
}
valI, err := strconv.ParseInt(e[i].ID[numI+1:], 10, 32)
if err != nil {
return true
}
valJ, err := strconv.ParseInt(e[j].ID[numJ+1:], 10, 32)
if err != nil {
return false
}
return valI < valJ
}
func (e ExploreResponse) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
func (e ExploreResponse) GetWidgetsByOrder(i int) ExploreResponse {
out := make(ExploreResponse, 0)
for _, v := range e {
if v.ID == string(IntOverTimeWidgetID) || v.ID == string(IntOverRegionID) {
continue
}
ind := strings.LastIndex(v.ID, "_")
val, err := strconv.ParseInt(v.ID[ind+1:], 10, 32)
if err != nil {
return out
}
if int(val) == i {
out = append(out, v)
}
}
return out
}
func (e ExploreResponse) GetWidgetsByType(t WidgetType) ExploreResponse {
out := make(ExploreResponse, 0)
for _, v := range e {
if strings.Contains(v.ID, string(t)) {
out = append(out, v)
}
}
return out
}
// WidgetResponse - system info for every available trends search mode
type WidgetResponse struct {
Geo interface{} `json:"geo,omitempty" bson:"geo"`
Time string `json:"time,omitempty" bson:"time"`
Resolution string `json:"resolution,omitempty" bson:"resolution"`
Locale string `json:"locale,omitempty" bson:"locale"`
Restriction WidgetComparisonItem `json:"restriction" bson:"restriction"`
CompItem []*WidgetComparisonItem `json:"comparisonItem" bson:"comparison_item"`
RequestOpt RequestOptions `json:"requestOptions" bson:"request_option"`
KeywordType string `json:"keywordType" bson:"keyword_type"`
Metric []string `json:"metric" bson:"metric"`
Language string `json:"language" bson:"language"`
TrendinessSettings map[string]string `json:"trendinessSettings" bson:"trendiness_settings"`
DataMode string `json:"dataMode,omitempty" bson:"data_mode"`
UserConfig map[string]string `json:"userConfig,omitempty" bson:"user_config"`
UserCountryCode string `json:"userCountryCode,omitempty" bson:"user_country_code"`
}
// WidgetComparisonItem - system info for comparison item part of WidgetResponse
type WidgetComparisonItem struct {
Geo map[string]string `json:"geo,omitempty" bson:"geo"`
Time string `json:"time,omitempty" bson:"time"`
ComplexKeywordsRestriction KeywordsRestriction `json:"complexKeywordsRestriction,omitempty" bson:"complex_keywords_restriction"`
OriginalTimeRangeForExploreURL string `json:"originalTimeRangeForExploreUrl,omitempty" bson:"original_time_range_for_explore_url"`
}
// KeywordsRestriction - system info for keywords limitations, not used. part of WidgetResponse
type KeywordsRestriction struct {
Keyword []*KeywordRestriction `json:"keyword" bson:"keyword"`
}
// KeywordRestriction - specific keyword limitation. Part of KeywordsRestriction
type KeywordRestriction struct {
Type string `json:"type" bson:"type"`
Value string `json:"value" bson:"value"`
}
// RequestOptions - part of WidgetResponse
type RequestOptions struct {
Property string `json:"property" bson:"property"`
Backend string `json:"backend" bson:"backend"`
Category int `json:"category" bson:"category"`
}
type multilineOut struct {
Default multiline `json:"default" bson:"default"`
}
type multiline struct {
TimelineData []*Timeline `json:"timelineData" bson:"timeline_data"`
}
// Timeline - it's representation of interest to trend trough period timeline. Mostly used for charts
type Timeline struct {
Time string `json:"time" bson:"time"`
FormattedTime string `json:"formattedTime" bson:"formatted_time"`
FormattedAxisTime string `json:"formattedAxisTime" bson:"formatted_axis_time"`
Value []int `json:"value" bson:"value"`
HasData []bool `json:"hasData" bson:"has_data"`
FormattedValue []string `json:"formattedValue" bson:"formatted_value"`
}
type geoOut struct {
Default geo `json:"default" bson:"default"`
}
type geo struct {
GeoMapData []*GeoMap `json:"geoMapData" bson:"geomap_data"`
}
// GeoMap - it's representation of interest by location. Mostly used for maps
type GeoMap struct {
GeoCode string `json:"geoCode" bson:"geo_code"`
GeoName string `json:"geoName" bson:"geo_name"`
Value []int `json:"value" bson:"value"`
FormattedValue []string `json:"formattedValue" bson:"formatted_value"`
MaxValueIndex int `json:"maxValueIndex" bson:"max_value_index"`
HasData []bool `json:"hasData" bson:"has_data"`
}
type relatedOut struct {
Default relatedList `json:"default" bson:"default"`
}
type relatedList struct {
Ranked []*rankedList `json:"rankedList" bson:"ranked"`
}
type rankedList struct {
Keywords []*RankedKeyword `json:"rankedKeyword" bson:"keywords"`
}
type searchOut struct {
Default searchList `json:"default" bson:"default"`
}
type searchList struct {
Keywords []*KeywordTopic `json:"topics" bson:"keywords"`
}
// RankedKeyword - it's representation of related to search items
type RankedKeyword struct {
Query string `json:"query,omitempty" bson:"query"`
Topic KeywordTopic `json:"topic,omitempty" bson:"topic"`
Value int `json:"value" bson:"value"`
FormattedValue string `json:"formattedValue" bson:"formatted_value"`
HasData bool `json:"hasData" bson:"has_data"`
Link string `json:"link" bson:"link"`
}
// KeywordTopic - is a part of RankedKeyword
type KeywordTopic struct {
Mid string `json:"mid" bson:"mid"`
Title string `json:"title" bson:"title"`
Type string `json:"type" bson:"type"`
}