-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter_api.go
138 lines (106 loc) · 2.62 KB
/
twitter_api.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
package twitter_api
import (
"github.com/NOX73/go-oauth"
"net/http"
"bufio"
"errors"
"encoding/json"
)
const (
NewRequestMethod = "POST"
NewRequestURL = "https://stream.twitter.com/1.1/statuses/filter.json"
TwitterStreamApiConnestionError = "Error: Response status code not 200."
)
type Credentials struct {
OauthConsumerKey string
OauthToken string
OauthConsumerSecret string
OauthTokenSecret string
}
type Message struct {
Error error
Response *http.Response
Tweet *Tweet
}
// >> Tweet
// stream api message
type TweetJSON struct {
Text string
User struct {
Id int
Screen_name string
Name string
Description string
Profile_image_url_https string
}
}
type Tweet struct {
Body string
JSON *TweetJSON
}
func (t *Tweet) Text() string {
if t.JSON == nil{ t.ParseJSON() }
return t.JSON.Text
}
func (t *Tweet) UserID() int {
if t.JSON == nil{ t.ParseJSON() }
return t.JSON.User.Id
}
func (t *Tweet) UserName() string {
if t.JSON == nil{ t.ParseJSON() }
return t.JSON.User.Screen_name
}
func (t *Tweet) ParseJSON() {
t.JSON = &TweetJSON{}
_ = json.Unmarshal([]byte(t.Body), t.JSON)
}
// << Tweet
func NewCredentials(consumer_key, token, consumer_secret, token_secret string) *Credentials {
return &Credentials{consumer_key, token, consumer_secret, token_secret}
}
func TwitterStream (ch chan Message, credentials *Credentials, params map[string]string){
var message Message
c := oauth.NewCredentials(credentials.OauthConsumerKey, credentials.OauthToken, credentials.OauthConsumerSecret, credentials.OauthTokenSecret)
r, _ := oauth.NewRequest(NewRequestMethod, NewRequestURL, params, c)
client := http.Client{}
resp, err := client.Do(r.HttpRequest())
if err == nil{
err = CheckError(resp)
}
if err != nil {
message = Message{
Error:err,
Response: resp,
}
ch <- message
return
}
body_reader := bufio.NewReader(resp.Body)
for {
var part []byte //Part of line
var prefix bool //Flag. Readln readed only part of line.
part, prefix, err := body_reader.ReadLine()
if err != nil { break }
if len(part) == 0 { continue }
buffer := append([]byte(nil), part...)
for prefix && err == nil {
part, prefix, err = body_reader.ReadLine()
buffer = append(buffer, part...)
}
if err != nil { break }
tweet := &Tweet{
Body: string(buffer),
}
message = Message{
Response: resp,
Tweet: tweet,
}
ch <- message
}
}
func CheckError(r *http.Response) error {
if r.StatusCode != 200{
return errors.New(TwitterStreamApiConnestionError)
}
return nil
}