forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline_v2.go
387 lines (358 loc) · 11.4 KB
/
timeline_v2.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
package twitterscraper
import (
"strconv"
"strings"
)
type tweet struct {
Core struct {
UserResults struct {
Result struct {
IsBlueVerified bool `json:"is_blue_verified"`
Legacy legacyUser `json:"legacy"`
} `json:"result"`
} `json:"user_results"`
} `json:"core"`
Views struct {
Count string `json:"count"`
} `json:"views"`
NoteTweet struct {
NoteTweetResults struct {
Result struct {
Text string `json:"text"`
} `json:"result"`
} `json:"note_tweet_results"`
} `json:"note_tweet"`
QuotedStatusResult struct {
Result *result `json:"result"`
} `json:"quoted_status_result"`
Legacy legacyTweet `json:"legacy"`
}
type result struct {
Typename string `json:"__typename"`
tweet
Tweet tweet `json:"tweet"`
}
func (result *result) parse() *Tweet {
if result.NoteTweet.NoteTweetResults.Result.Text != "" {
result.Legacy.FullText = result.NoteTweet.NoteTweetResults.Result.Text
}
var legacy *legacyTweet = &result.Legacy
var user *legacyUser = &result.Core.UserResults.Result.Legacy
if result.Typename == "TweetWithVisibilityResults" {
legacy = &result.Tweet.Legacy
user = &result.Tweet.Core.UserResults.Result.Legacy
}
tw := parseLegacyTweet(user, legacy)
if tw == nil {
return nil
}
if tw.Views == 0 && result.Views.Count != "" {
tw.Views, _ = strconv.Atoi(result.Views.Count)
}
if result.QuotedStatusResult.Result != nil {
tw.QuotedStatus = result.QuotedStatusResult.Result.parse()
}
return tw
}
type userResult struct {
Typename string `json:"__typename"`
ID string `json:"id"`
RestID string `json:"rest_id"`
AffiliatesHighlightedLabel struct{} `json:"affiliates_highlighted_label"`
HasGraduatedAccess bool `json:"has_graduated_access"`
IsBlueVerified bool `json:"is_blue_verified"`
ProfileImageShape string `json:"profile_image_shape"`
Legacy legacyUserV2 `json:"legacy"`
}
func (result *userResult) parse() Profile {
return parseProfileV2(*result)
}
type item struct {
EntryID string `json:"entryId"`
Item struct {
ItemContent struct {
ItemType string `json:"itemType"`
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
CursorType string `json:"cursorType"`
Value string `json:"value"`
} `json:"itemContent"`
} `json:"item"`
}
type entry struct {
Content struct {
CursorType string `json:"cursorType"`
Value string `json:"value"`
Items []item `json:"items"`
ItemContent struct {
ItemType string `json:"itemType"`
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
UserDisplayType string `json:"userDisplayType"`
UserResults struct {
Result userResult `json:"result"`
} `json:"user_results"`
CursorType string `json:"cursorType"`
Value string `json:"value"`
} `json:"itemContent"`
} `json:"content"`
}
// timeline v2 JSON object
type timelineV2 struct {
Data struct {
User struct {
Result struct {
TimelineV2 struct {
Timeline struct {
Instructions []struct {
ModuleItems []item `json:"moduleItems"`
Entries []entry `json:"entries"`
Entry entry `json:"entry"`
Type string `json:"type"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"timeline_v2"`
Timeline struct {
Timeline struct {
Instructions []struct {
Entries []entry `json:"entries"`
Entry entry `json:"entry"`
Type string `json:"type"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"timeline"`
} `json:"result"`
} `json:"user"`
} `json:"data"`
}
func (timeline *timelineV2) parseTweets() ([]*Tweet, string) {
var cursor string
var tweets []*Tweet
for _, instruction := range timeline.Data.User.Result.TimelineV2.Timeline.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.CursorType == "Bottom" {
cursor = entry.Content.Value
continue
}
if entry.Content.ItemContent.TweetResults.Result.Typename == "Tweet" || entry.Content.ItemContent.TweetResults.Result.Typename == "TweetWithVisibilityResults" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
tweets = append(tweets, tweet)
}
}
if len(entry.Content.Items) > 0 {
for _, item := range entry.Content.Items {
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
tweets = append(tweets, tweet)
}
}
}
}
if len(instruction.ModuleItems) > 0 {
for _, entry := range instruction.ModuleItems {
if entry.Item.ItemContent.TweetResults.Result.Typename == "Tweet" || entry.Item.ItemContent.TweetResults.Result.Typename == "TweetWithVisibilityResults" {
if tweet := entry.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
tweets = append(tweets, tweet)
}
}
}
}
}
return tweets, cursor
}
type bookmarksTimelineV2 struct {
Data struct {
Bookmarks struct {
Timeline struct {
Instructions []struct {
Entries []entry `json:"entries"`
Type string `json:"type"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"bookmark_timeline_v2"`
} `json:"data"`
}
func (timeline *bookmarksTimelineV2) parseTweets() ([]*Tweet, string) {
var cursor string
var tweets []*Tweet
for _, instruction := range timeline.Data.Bookmarks.Timeline.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.CursorType == "Bottom" {
cursor = entry.Content.Value
continue
}
if entry.Content.ItemContent.TweetResults.Result.Typename == "Tweet" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
tweets = append(tweets, tweet)
}
}
}
}
return tweets, cursor
}
type retweetersTimelineV2 struct {
Data struct {
RetweetersTimeline struct {
Timeline struct {
Instructions []struct {
Type string `json:"type"`
Entries []entry `json:"entries"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"retweeters_timeline"`
} `json:"data"`
}
func (timeline *retweetersTimelineV2) parseUsers() ([]*Profile, string) {
var cursor string
var users []*Profile
for _, instruction := range timeline.Data.RetweetersTimeline.Timeline.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.CursorType == "Bottom" {
cursor = entry.Content.Value
continue
}
if entry.Content.ItemContent.UserResults.Result.Typename == "User" {
user := entry.Content.ItemContent.UserResults.Result.parse()
users = append(users, &user)
}
}
}
return users, cursor
}
func (timeline *timelineV2) parseUsers() ([]*Profile, string) {
var cursor string
var users []*Profile
for _, instruction := range timeline.Data.User.Result.Timeline.Timeline.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.CursorType == "Bottom" {
cursor = entry.Content.Value
continue
}
if entry.Content.ItemContent.UserResults.Result.Typename == "User" {
user := entry.Content.ItemContent.UserResults.Result.parse()
users = append(users, &user)
}
}
}
return users, cursor
}
type threadedConversation struct {
Data struct {
ThreadedConversationWithInjectionsV2 struct {
Instructions []struct {
Type string `json:"type"`
Entry entry `json:"entry"`
Entries []entry `json:"entries"`
ModuleItems []item `json:"moduleItems"`
} `json:"instructions"`
} `json:"threaded_conversation_with_injections_v2"`
} `json:"data"`
}
func (conversation *threadedConversation) parse(focalTweetID string) ([]*Tweet, []*ThreadCursor) {
var tweets []*Tweet
var cursors []*ThreadCursor
for _, instruction := range conversation.Data.ThreadedConversationWithInjectionsV2.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.ItemContent.TweetResults.Result.Typename == "Tweet" || entry.Content.ItemContent.TweetResults.Result.Typename == "TweetWithVisibilityResults" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
if entry.Content.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
if entry.Content.ItemContent.CursorType != "" && entry.Content.ItemContent.Value != "" {
cursors = append(cursors, &ThreadCursor{
FocalTweetID: focalTweetID,
ThreadID: focalTweetID,
Cursor: entry.Content.ItemContent.Value,
CursorType: entry.Content.ItemContent.CursorType,
})
}
for _, item := range entry.Content.Items {
if item.Item.ItemContent.TweetResults.Result.Typename == "Tweet" || item.Item.ItemContent.TweetResults.Result.Typename == "TweetWithVisibilityResults" {
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
if item.Item.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
if item.Item.ItemContent.CursorType != "" && item.Item.ItemContent.Value != "" {
threadID := ""
entryId := strings.Split(item.EntryID, "-")
if len(entryId) > 1 && entryId[0] == "conversationthread" {
if i, _ := strconv.Atoi(entryId[1]); i != 0 {
threadID = entryId[1]
}
}
cursors = append(cursors, &ThreadCursor{
FocalTweetID: focalTweetID,
ThreadID: threadID,
Cursor: item.Item.ItemContent.Value,
CursorType: item.Item.ItemContent.CursorType,
})
}
}
}
for _, item := range instruction.ModuleItems {
if item.Item.ItemContent.TweetResults.Result.Typename == "Tweet" || item.Item.ItemContent.TweetResults.Result.Typename == "TweetWithVisibilityResults" {
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
if item.Item.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
if item.Item.ItemContent.CursorType != "" && item.Item.ItemContent.Value != "" {
threadID := ""
entryId := strings.Split(item.EntryID, "-")
if len(entryId) > 1 && entryId[0] == "conversationthread" {
if i, _ := strconv.Atoi(entryId[1]); i != 0 {
threadID = entryId[1]
}
}
cursors = append(cursors, &ThreadCursor{
FocalTweetID: focalTweetID,
ThreadID: threadID,
Cursor: item.Item.ItemContent.Value,
CursorType: item.Item.ItemContent.CursorType,
})
}
}
}
for _, tweet := range tweets {
if tweet.InReplyToStatusID != "" {
for _, parentTweet := range tweets {
if parentTweet.ID == tweet.InReplyToStatusID {
tweet.InReplyToStatus = parentTweet
break
}
}
}
if tweet.IsSelfThread && tweet.ConversationID == tweet.ID {
for _, childTweet := range tweets {
if childTweet.IsSelfThread && childTweet.ID != tweet.ID {
tweet.Thread = append(tweet.Thread, childTweet)
}
}
if len(tweet.Thread) == 0 {
tweet.IsSelfThread = false
}
}
}
return tweets, cursors
}
type tweetResult struct {
Data struct {
TweetResult struct {
Result result `json:"result"`
} `json:"tweetResult"`
} `json:"data"`
}
func (tweetResult *tweetResult) parse() *Tweet {
return tweetResult.Data.TweetResult.Result.parse()
}