forked from n0madic/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trends.go
42 lines (35 loc) · 1.06 KB
/
trends.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
package twitterscraper
import "fmt"
// GetTrends return list of trends.
func (s *Scraper) GetTrends() ([]string, error) {
req, err := s.newRequest("GET", "https://api.twitter.com/2/guide.json")
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("count", "20")
q.Add("candidate_source", "trends")
q.Add("include_page_configuration", "false")
q.Add("entity_tokens", "false")
req.URL.RawQuery = q.Encode()
var jsn timelineV1
curBearerToken := s.bearerToken
if curBearerToken != bearerToken2 {
s.setBearerToken(bearerToken2)
}
err = s.RequestAPI(req, &jsn)
if curBearerToken != bearerToken2 {
s.setBearerToken(curBearerToken)
}
if err != nil {
return nil, err
}
if len(jsn.Timeline.Instructions[1].AddEntries.Entries) < 2 {
return nil, fmt.Errorf("no trend entries found")
}
var trends []string
for _, item := range jsn.Timeline.Instructions[1].AddEntries.Entries[1].Content.TimelineModule.Items {
trends = append(trends, item.Item.ClientEventInfo.Details.GuideDetails.TransparentGuideDetails.TrendMetadata.TrendName)
}
return trends, nil
}