-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.go
53 lines (37 loc) · 835 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
func connectTwitterHttpStream(client http.Client) {
req , err := http.NewRequest("GET", "https://api.twitter.com/2/tweets/search/stream", nil)
if err != nil {
fmt.Printf("Error in start twitter stream: %s\n", err)
}
req.Header = http.Header{
"Authorization": {"Bearer " + os.Getenv("TWITTER_TOKEN")},
}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error in start twitter stream: %s\n", err)
}
defer resp.Body.Close()
type Tweet struct {
Id, Text string
}
type Message struct {
Tweet Tweet
}
tweet := new(Message);
decoder := json.NewDecoder(resp.Body)
for {
err := decoder.Decode(&tweet)
if err != nil {
fmt.Println(err)
}
fmt.Println(tweet.Tweet.Text)
addTrackToPlaylist(client, tweet.Tweet.Text)
}
}