-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTL.go
428 lines (397 loc) · 13.4 KB
/
getTL.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"encoding/json"
"context"
"fmt"
"strconv"
"time"
"os"
//"errors"
g8rv2 "github.com/g8rswimmer/go-twitter/v2"
)
func twid2str(twid twidt) (string) {
return strconv.FormatInt(int64(twid), 10)
}
type twSearchApi struct {
client *g8rv2.Client
t tltype
tlopt g8rv2.UserTweetTimelineOpts
tmopt g8rv2.UserMentionTimelineOpts
lsopt g8rv2.ListTweetLookupOpts
sropt g8rv2.TweetRecentSearchOpts
saopt g8rv2.TweetSearchOpts
nextToken string
seq int
jsonp bool
}
func (ta *twSearchApi) getTL(userID string, maxresult int, max twidt, since twidt) (tweets []*g8rv2.TweetDictionary, count int, last bool, err error) {
count = 0
last = true
err = nil
client := ta.client
switch ta.t {
case tluser:
if ta.seq == 0 {
ta.tlopt = g8rv2.UserTweetTimelineOpts {
TweetFields: []g8rv2.TweetField{g8rv2.TweetFieldReferencedTweets, g8rv2.TweetFieldInReplyToUserID},
Expansions: []g8rv2.Expansion{g8rv2.ExpansionReferencedTweetsIDAuthorID},
}
if max > 0 {ta.tlopt.UntilID = twid2str(max)}
if since > 0 {ta.tlopt.SinceID = twid2str(since)}
} else {
if ta.nextToken == "" {
break
}
ta.tlopt.PaginationToken = ta.nextToken
}
if maxresult > 0 {ta.tlopt.MaxResults = maxresult}
ta.seq++
ta.nextToken = ""
var res *g8rv2.UserTweetTimelineResponse
res, err = client.UserTweetTimeline(context.Background(), userID, ta.tlopt)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if res == nil {
fmt.Fprintln(os.Stderr, "res == nil")
break
}
if ta.jsonp {
jsonraw, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(jsonraw))
}
if res.Raw != nil {
for _, e := range res.Raw.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", e.Title, e.Detail)
break
}
tweets = TweetDictionarySlice(res.Raw)
}
if res.Meta != nil {
count = res.Meta.ResultCount
if res.Meta.NextToken != "" {
ta.nextToken = res.Meta.NextToken
last = false
}
}
fmt.Fprintf(os.Stderr, "%s get len: %d\n", time.Now().Format("15:04:05"), count)
return tweets, count, last, err
case tlhome:
case tlmention:
if ta.seq == 0 {
ta.tmopt = g8rv2.UserMentionTimelineOpts {
TweetFields: []g8rv2.TweetField{g8rv2.TweetFieldReferencedTweets, g8rv2.TweetFieldInReplyToUserID},
Expansions: []g8rv2.Expansion{g8rv2.ExpansionReferencedTweetsIDAuthorID},
}
if max > 0 {ta.tmopt.UntilID = twid2str(max)}
if since > 0 {ta.tmopt.SinceID = twid2str(since)}
} else {
if ta.nextToken == "" {
break
}
ta.tmopt.PaginationToken = ta.nextToken
}
if maxresult > 0 {ta.tmopt.MaxResults = maxresult}
ta.seq++
ta.nextToken = ""
var res *g8rv2.UserMentionTimelineResponse
res, err = client.UserMentionTimeline(context.Background(), userID, ta.tmopt)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if res == nil {
fmt.Fprintln(os.Stderr, "res == nil")
break
}
if ta.jsonp {
jsonraw, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(jsonraw))
}
if res.Raw != nil {
for _, e := range res.Raw.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", e.Title, e.Detail)
break
}
tweets = TweetDictionarySlice(res.Raw)
}
if res.Meta != nil {
count = res.Meta.ResultCount
if res.Meta.NextToken != "" {
ta.nextToken = res.Meta.NextToken
last = false
}
}
fmt.Fprintf(os.Stderr, "%s get len: %d\n", time.Now().Format("15:04:05"), count)
return tweets, count, last, err
case tlrtofme:
case tllist:
if ta.seq == 0 {
ta.lsopt = g8rv2.ListTweetLookupOpts {
TweetFields: []g8rv2.TweetField{g8rv2.TweetFieldReferencedTweets, g8rv2.TweetFieldInReplyToUserID},
Expansions: []g8rv2.Expansion{g8rv2.ExpansionReferencedTweetsIDAuthorID},
}
//if max > 0 {ta.lsopt.UntilID = twid2str(max)}
//if since > 0 {ta.lsopt.SinceID = twid2str(since)}
} else {
if ta.nextToken == "" {
break
}
ta.lsopt.PaginationToken = ta.nextToken
}
if maxresult > 0 {ta.lsopt.MaxResults = maxresult}
ta.seq++
ta.nextToken = ""
var res *g8rv2.ListTweetLookupResponse
res, err = client.ListTweetLookup(context.Background(), userID, ta.lsopt)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if res == nil {
fmt.Fprintln(os.Stderr, "res == nil")
break
}
if ta.jsonp {
jsonraw, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(jsonraw))
}
if res.Raw != nil {
for _, e := range res.Raw.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", e.Title, e.Detail)
break
}
tweets = TweetDictionarySlice(res.Raw)
}
if res.Meta != nil {
count = res.Meta.ResultCount
if res.Meta.NextToken != "" {
ta.nextToken = res.Meta.NextToken
last = false
}
}
fmt.Fprintf(os.Stderr, "%s get len: %d\n", time.Now().Format("15:04:05"), count)
return tweets, count, last, err
case tlsearch:
if ta.seq == 0 {
ta.sropt = g8rv2.TweetRecentSearchOpts {
TweetFields: []g8rv2.TweetField{g8rv2.TweetFieldReferencedTweets, g8rv2.TweetFieldInReplyToUserID},
Expansions: []g8rv2.Expansion{g8rv2.ExpansionReferencedTweetsIDAuthorID},
}
if max > 0 {ta.sropt.UntilID = twid2str(max)}
if since > 0 {ta.sropt.SinceID = twid2str(since)}
} else {
if ta.nextToken == "" {
break
}
ta.sropt.NextToken = ta.nextToken
}
if maxresult > 0 {ta.sropt.MaxResults = maxresult}
ta.seq++
ta.nextToken = ""
var res *g8rv2.TweetRecentSearchResponse
res, err = client.TweetRecentSearch(context.Background(), userID, ta.sropt)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if res == nil {
fmt.Fprintln(os.Stderr, "res == nil")
break
}
if ta.jsonp {
jsonraw, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(jsonraw))
}
if res.Raw != nil {
for _, e := range res.Raw.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", e.Title, e.Detail)
break
}
tweets = TweetDictionarySlice(res.Raw)
}
if res.Meta != nil {
count = res.Meta.ResultCount
if res.Meta.NextToken != "" {
ta.nextToken = res.Meta.NextToken
last = false
}
}
fmt.Fprintf(os.Stderr, "%s get len: %d\n", time.Now().Format("15:04:05"), count)
return tweets, count, last, err
case tlsearcha:
if ta.seq == 0 {
ta.saopt = g8rv2.TweetSearchOpts {
TweetFields: []g8rv2.TweetField{g8rv2.TweetFieldReferencedTweets, g8rv2.TweetFieldInReplyToUserID},
Expansions: []g8rv2.Expansion{g8rv2.ExpansionReferencedTweetsIDAuthorID},
}
if max > 0 {ta.saopt.UntilID = twid2str(max)}
if since > 0 {ta.saopt.SinceID = twid2str(since)}
} else {
if ta.nextToken == "" {
break
}
ta.saopt.NextToken = ta.nextToken
}
if maxresult > 0 {ta.saopt.MaxResults = maxresult}
ta.seq++
ta.nextToken = ""
var res *g8rv2.TweetSearchResponse
res, err = client.TweetSearch(context.Background(), userID, ta.saopt)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if res == nil {
fmt.Fprintln(os.Stderr, "res == nil")
break
}
if ta.jsonp {
jsonraw, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(jsonraw))
}
if res.Raw != nil {
for _, e := range res.Raw.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", e.Title, e.Detail)
break
}
tweets = TweetDictionarySlice(res.Raw)
}
if res.Meta != nil {
count = res.Meta.ResultCount
if res.Meta.NextToken != "" {
ta.nextToken = res.Meta.NextToken
last = false
}
}
fmt.Fprintf(os.Stderr, "%s get len: %d\n", time.Now().Format("15:04:05"), count)
return tweets, count, last, err
}
return nil, 0, true, err
}
func (ta *twSearchApi) rewindQuery() {
ta.seq = 0
}
func TweetDictionarySlice(t *g8rv2.TweetRaw) (s []*g8rv2.TweetDictionary) {
s = []*g8rv2.TweetDictionary{}
for _, tweet := range t.Tweets {
s = append(s, g8rv2.CreateTweetDictionary(*tweet, t.Includes))
}
return s
}
// // UserTweetTimelineOpts are the options for the user tweet timeline request
// type UserTweetTimelineOpts struct {
// Expansions []Expansion
// MediaFields []MediaField
// PlaceFields []PlaceField
// PollFields []PollField
// TweetFields []TweetField
// UserFields []UserField
// Excludes []Exclude
// StartTime time.Time
// EndTime time.Time
// MaxResults int
// PaginationToken string
// SinceID string
// UntilID string
// }
// // UserTweetTimeline will return the user tweet timeline
// func (c *Client) UserTweetTimeline(ctx context.Context, userID string, opts UserTweetTimelineOpts) (*UserTweetTimelineResponse, error) {
// // UserTweetTimelineResponse contains the information from the user tweet timeline callout
// type UserTweetTimelineResponse struct {
// Raw *TweetRaw
// Meta *UserTimelineMeta `json:"meta"`
// RateLimit *RateLimit
// }
// TweetRaw is the raw response from the tweet lookup endpoint
// type TweetRaw struct {
// Tweets []*TweetObj `json:"data"`
// Includes *TweetRawIncludes `json:"includes,omitempty"`
// Errors []*ErrorObj `json:"errors,omitempty"`
// dictionaries map[string]*TweetDictionary
// }
// // UserTimelineMeta contains the meta data from the timeline callout
// type UserTimelineMeta struct {
// ResultCount int `json:"result_count"`
// NewestID string `json:"newest_id"`
// OldestID string `json:"oldest_id"`
// NextToken string `json:"next_token"`
// PreviousToken string `json:"previous_token"`
// }
// // TweetDictionaries create a map of tweet dictionaries from the raw tweet response
// func (t *TweetRaw) TweetDictionaries() map[string]*TweetDictionary {
// if t.dictionaries != nil {
// return t.dictionaries
// }
//
// t.dictionaries = map[string]*TweetDictionary{}
// for _, tweet := range t.Tweets {
// t.dictionaries[tweet.ID] = CreateTweetDictionary(*tweet, t.Includes)
// }
// return t.dictionaries
// }
// // TweetDictionary is a struct of a tweet and all of the reference objects
// type TweetDictionary struct {
// Tweet TweetObj
// Author *UserObj
// InReplyUser *UserObj
// Place *PlaceObj
// AttachmentPolls []*PollObj
// AttachmentMedia []*MediaObj
// Mentions []*TweetMention
// ReferencedTweets []*TweetReference
// }
// // TweetObj is the primary object on the tweets endpoints
// type TweetObj struct {
// ID string `json:"id"`
// Text string `json:"text"`
// Attachments *TweetAttachmentsObj `json:"attachments,omitempty"`
// AuthorID string `json:"author_id,omitempty"`
// ContextAnnotations []*TweetContextAnnotationObj `json:"context_annotations,omitempty"`
// ConversationID string `json:"conversation_id,omitempty"`
// CreatedAt string `json:"created_at,omitempty"`
// Entities *EntitiesObj `json:"entities,omitempty"`
// Geo *TweetGeoObj `json:"geo,omitempty"`
// InReplyToUserID string `json:"in_reply_to_user_id,omitempty"`
// Language string `json:"lang,omitempty"`
// NonPublicMetrics *TweetMetricsObj `json:"non_public_metrics,omitempty"`
// OrganicMetrics *TweetMetricsObj `json:"organic_metrics,omitempty"`
// PossiblySensitive bool `json:"possibly_sensitive,omitempty"`
// PromotedMetrics *TweetMetricsObj `json:"promoted_metrics,omitempty"`
// PublicMetrics *TweetMetricsObj `json:"public_metrics,omitempty"`
// ReferencedTweets []*TweetReferencedTweetObj `json:"referenced_tweets,omitempty"`
// Source string `json:"source,omitempty"`
// WithHeld *WithHeldObj `json:"withheld,omitempty"`
// }
// // UserObj contains Twitter user account metadata describing the referenced user
// type UserObj struct {
// ID string `json:"id"`
// Name string `json:"name"`
// UserName string `json:"username"`
// CreatedAt string `json:"created_at,omitempty"`
// Description string `json:"description,omitempty"`
// Entities *EntitiesObj `json:"entities,omitempty"`
// Location string `json:"location,omitempty"`
// PinnedTweetID string `json:"pinned_tweet_id,omitempty"`
// ProfileImageURL string `json:"profile_image_url,omitempty"`
// Protected bool `json:"protected,omitempty"`
// PublicMetrics *UserMetricsObj `json:"public_metrics,omitempty"`
// URL string `json:"url,omitempty"`
// Verified bool `json:"verified,omitempty"`
// WithHeld *WithHeldObj `json:"withheld,omitempty"`
// }
// // TweetReference is the tweet referenced and it's dictionary
// type TweetReference struct {
// Reference *TweetReferencedTweetObj
// TweetDictionary *TweetDictionary
// }
// // TweetReferencedTweetObj is a Tweet this Tweet refers to
// type TweetReferencedTweetObj struct {
// Type string `json:"type"`
// ID string `json:"id"`
// }
// // ErrorObj is part of the partial errors in the response
// type ErrorObj struct {
// Title string `json:"title"`
// Detail string `json:"detail"`
// Type string `json:"type"`
// ResourceType string `json:"resource_type"`
// Parameter string `json:"parameter"`
// Value interface{} `json:"value"`
// }