forked from g8rswimmer/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_search.go
271 lines (245 loc) · 7.34 KB
/
tweet_search.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package twitter
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
// TweetSearchSortOrder specifies the order the tweets are returned
type TweetSearchSortOrder string
const (
// TweetSearchSortOrderRecency will return the tweets in order of recency
TweetSearchSortOrderRecency TweetSearchSortOrder = "recency"
// TweetSearchSortOrderRelevancy will return the tweets in order of relevancy
TweetSearchSortOrderRelevancy TweetSearchSortOrder = "relevancy"
)
// TweetRecentSearchOpts are the optional parameters for the recent search API
type TweetRecentSearchOpts struct {
Expansions []Expansion
MediaFields []MediaField
PlaceFields []PlaceField
PollFields []PollField
TweetFields []TweetField
UserFields []UserField
StartTime time.Time
EndTime time.Time
SortOrder TweetSearchSortOrder
MaxResults int
NextToken string
SinceID string
UntilID string
}
func (t TweetRecentSearchOpts) addQuery(req *http.Request) {
q := req.URL.Query()
if len(t.Expansions) > 0 {
q.Add("expansions", strings.Join(expansionStringArray(t.Expansions), ","))
}
if len(t.MediaFields) > 0 {
q.Add("media.fields", strings.Join(mediaFieldStringArray(t.MediaFields), ","))
}
if len(t.PlaceFields) > 0 {
q.Add("place.fields", strings.Join(placeFieldStringArray(t.PlaceFields), ","))
}
if len(t.PollFields) > 0 {
q.Add("poll.fields", strings.Join(pollFieldStringArray(t.PollFields), ","))
}
if len(t.TweetFields) > 0 {
q.Add("tweet.fields", strings.Join(tweetFieldStringArray(t.TweetFields), ","))
}
if len(t.UserFields) > 0 {
q.Add("user.fields", strings.Join(userFieldStringArray(t.UserFields), ","))
}
if !t.StartTime.IsZero() {
q.Add("start_time", t.StartTime.Format(time.RFC3339))
}
if !t.EndTime.IsZero() {
q.Add("end_time", t.EndTime.Format(time.RFC3339))
}
if t.MaxResults > 0 {
q.Add("max_results", strconv.Itoa(t.MaxResults))
}
if len(t.NextToken) > 0 {
q.Add("next_token", t.NextToken)
}
if len(t.SinceID) > 0 {
q.Add("since_id", t.SinceID)
}
if len(t.UntilID) > 0 {
q.Add("until_id", t.UntilID)
}
if len(t.SortOrder) > 0 {
q.Add("sort_order", string(t.SortOrder))
}
if len(q) > 0 {
req.URL.RawQuery = q.Encode()
}
}
// TweetRecentSearchResponse contains all of the information from a tweet recent search
type TweetRecentSearchResponse struct {
Raw *TweetRaw
Meta *TweetRecentSearchMeta `json:"meta"`
RateLimit *RateLimit
}
// TweetRecentSearchMeta contains the recent search information
type TweetRecentSearchMeta struct {
NewestID string `json:"newest_id"`
OldestID string `json:"oldest_id"`
ResultCount int `json:"result_count"`
NextToken string `json:"next_token"`
}
// TweetSearchOpts are the tweet search options
type TweetSearchOpts struct {
Expansions []Expansion
MediaFields []MediaField
PlaceFields []PlaceField
PollFields []PollField
TweetFields []TweetField
UserFields []UserField
StartTime time.Time
EndTime time.Time
SortOrder TweetSearchSortOrder
MaxResults int
NextToken string
SinceID string
UntilID string
}
func (t TweetSearchOpts) addQuery(req *http.Request) {
q := req.URL.Query()
if len(t.Expansions) > 0 {
q.Add("expansions", strings.Join(expansionStringArray(t.Expansions), ","))
}
if len(t.MediaFields) > 0 {
q.Add("media.fields", strings.Join(mediaFieldStringArray(t.MediaFields), ","))
}
if len(t.PlaceFields) > 0 {
q.Add("place.fields", strings.Join(placeFieldStringArray(t.PlaceFields), ","))
}
if len(t.PollFields) > 0 {
q.Add("poll.fields", strings.Join(pollFieldStringArray(t.PollFields), ","))
}
if len(t.TweetFields) > 0 {
q.Add("tweet.fields", strings.Join(tweetFieldStringArray(t.TweetFields), ","))
}
if len(t.UserFields) > 0 {
q.Add("user.fields", strings.Join(userFieldStringArray(t.UserFields), ","))
}
if !t.StartTime.IsZero() {
q.Add("start_time", t.StartTime.Format(time.RFC3339))
}
if !t.EndTime.IsZero() {
q.Add("end_time", t.EndTime.Format(time.RFC3339))
}
if t.MaxResults > 0 {
q.Add("max_results", strconv.Itoa(t.MaxResults))
}
if len(t.NextToken) > 0 {
q.Add("next_token", t.NextToken)
}
if len(t.SinceID) > 0 {
q.Add("since_id", t.SinceID)
}
if len(t.UntilID) > 0 {
q.Add("until_id", t.UntilID)
}
if len(t.SortOrder) > 0 {
q.Add("sort_order", string(t.SortOrder))
}
if len(q) > 0 {
req.URL.RawQuery = q.Encode()
}
}
// TweetSearchResponse is the tweet search response
type TweetSearchResponse struct {
Raw *TweetRaw
Meta *TweetSearchMeta `json:"meta"`
RateLimit *RateLimit
}
// TweetSearchMeta is the tweet search meta data
type TweetSearchMeta struct {
NewestID string `json:"newest_id"`
OldestID string `json:"oldest_id"`
ResultCount int `json:"result_count"`
NextToken string `json:"next_token"`
}
// TweetSearchStreamRule is the search stream filter rule
type TweetSearchStreamRule struct {
Value string `json:"value"`
Tag string `json:"tag,omitempty"`
}
func (t TweetSearchStreamRule) validate() error {
if len(t.Value) == 0 {
return fmt.Errorf("tweet search stream rule value is required: %w", ErrParameter)
}
return nil
}
type tweetSearchStreamRules []TweetSearchStreamRule
func (t tweetSearchStreamRules) validate() error {
for _, rule := range t {
if err := rule.validate(); err != nil {
return err
}
}
return nil
}
// TweetSearchStreamRuleID is the filter rule id
type TweetSearchStreamRuleID string
func (t TweetSearchStreamRuleID) validate() error {
if len(t) == 0 {
return fmt.Errorf("tweet search rule id is required %w", ErrParameter)
}
return nil
}
type tweetSearchStreamRuleIDs []TweetSearchStreamRuleID
func (t tweetSearchStreamRuleIDs) validate() error {
for _, id := range t {
if err := id.validate(); err != nil {
return err
}
}
return nil
}
func (t tweetSearchStreamRuleIDs) toStringArray() []string {
ids := make([]string, len(t))
for i, id := range t {
ids[i] = string(id)
}
return ids
}
// TweetSearchStreamRuleEntity is the search filter rule entity
type TweetSearchStreamRuleEntity struct {
ID TweetSearchStreamRuleID `json:"id"`
TweetSearchStreamRule
}
// TweetSearchStreamRulesResponse is the response to getting the search rules
type TweetSearchStreamRulesResponse struct {
Rules []*TweetSearchStreamRuleEntity `json:"data"`
Meta *TweetSearchStreamRuleMeta `json:"meta"`
Errors []*ErrorObj `json:"errors,omitempty"`
RateLimit *RateLimit
}
// TweetSearchStreamAddRuleResponse is the response from adding rules
type TweetSearchStreamAddRuleResponse struct {
Rules []*TweetSearchStreamRuleEntity `json:"data"`
Meta *TweetSearchStreamRuleMeta `json:"meta"`
Errors []*ErrorObj `json:"errors,omitempty"`
RateLimit *RateLimit
}
// TweetSearchStreamDeleteRuleResponse is the response from deleting rules
type TweetSearchStreamDeleteRuleResponse struct {
Meta *TweetSearchStreamRuleMeta `json:"meta"`
Errors []*ErrorObj `json:"errors,omitempty"`
RateLimit *RateLimit
}
// TweetSearchStreamRuleMeta is the meta data object from the request
type TweetSearchStreamRuleMeta struct {
Sent time.Time `json:"sent"`
Summary TweetSearchStreamRuleSummary `json:"summary"`
}
// TweetSearchStreamRuleSummary is the summary of the search filters
type TweetSearchStreamRuleSummary struct {
Created int `json:"created"`
NotCreated int `json:"not_created"`
Deleted int `json:"deleted"`
NotDeleted int `json:"not_deleted"`
}