forked from gnolang/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.go
69 lines (60 loc) · 1.37 KB
/
twitter.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
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func twitterFetchTips() string {
if opts.since != "" {
opts.twitterSearchTweetsUrl += "&start_time=" + opts.since
}
var bearer = "Bearer " + opts.twitterToken
req, err := http.NewRequest("GET", opts.twitterSearchTweetsUrl, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
return ""
}
req.Header.Add("Authorization", bearer)
resp, err := opts.httpClient.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
return ""
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
return ""
}
return string(body)
}
type TweetSearch struct {
Data []Tweet `json:"data"`
Meta Info `json:"meta"`
Includes Include `json:"includes"`
}
type Tweet struct {
Id string `json:"id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
AuthorId string `json:"author_id"`
}
type Info struct {
NewestId string `json:"newest_id"`
OldestId string `json:"oldest_id"`
ResultCount int `json:"result_count"`
}
type Include struct {
Users []User `json:"users"`
}
type User struct {
Id string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
}