-
Notifications
You must be signed in to change notification settings - Fork 4
/
googletrends.go
executable file
·68 lines (52 loc) · 1.31 KB
/
googletrends.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
package main
import (
"context"
"reflect"
"log"
"github.com/groovili/gogtrends"
"github.com/pkg/errors"
)
const (
locUS = "US"
catAll = "all"
langEn = "EN"
)
func (a *App) getGoogleTrends(keyword string) ([]*gogtrends.Timeline, error) {
//Enable debug to see request-response
//gogtrends.Debug(true)
ctx := context.Background()
// var sg = new(sync.WaitGroup)
log.Println("Explore trends:")
// get widgets for Golang keyword in programming category
explore, err := gogtrends.Explore(ctx, &gogtrends.ExploreRequest{
ComparisonItems: []*gogtrends.ComparisonItem{
{
Keyword: keyword,
Geo: locUS,
Time: "today 12-m",
},
},
Property: "",
}, langEn)
handleError(err, "Failed to explore widgets")
printItems(explore)
log.Println("Interest over time:")
overTime, err := gogtrends.InterestOverTime(ctx, explore[0], langEn)
handleError(err, "Failed in call interest over time")
printItems(overTime)
return overTime, err
}
func handleError(err error, errMsg string) {
if err != nil {
log.Fatal(errors.Wrap(err, errMsg))
}
}
func printItems(items interface{}) {
ref := reflect.ValueOf(items)
if ref.Kind() != reflect.Slice {
log.Fatalf("Failed to print %s. It's not a slice type.", ref.Kind())
}
for i := 0; i < ref.Len(); i++ {
log.Println(ref.Index(i).Interface())
}
}