This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
timeline_v2.go
175 lines (167 loc) · 4.73 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
package twitterscraper
import (
"strconv"
)
type result struct {
Typename string `json:"__typename"`
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"`
}
func (result *result) parse() *Tweet {
if result.NoteTweet.NoteTweetResults.Result.Text != "" {
result.Legacy.FullText = result.NoteTweet.NoteTweetResults.Result.Text
}
tw := parseLegacyTweet(&result.Core.UserResults.Result.Legacy, &result.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 entry struct {
Content struct {
CursorType string `json:"cursorType"`
Value string `json:"value"`
Items []struct {
Item struct {
ItemContent struct {
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
} `json:"itemContent"`
} `json:"item"`
} `json:"items"`
ItemContent struct {
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
UserDisplayType string `json:"userDisplayType"`
UserResults struct {
Result struct {
RestID string `json:"rest_id"`
Legacy legacyUser `json:"legacy"`
} `json:"result"`
} `json:"user_results"`
} `json:"itemContent"`
} `json:"content"`
}
// timeline v2 JSON object
type timelineV2 struct {
Data struct {
User struct {
Result struct {
TimelineV2 struct {
Timeline struct {
Instructions []struct {
Entries []entry `json:"entries"`
Entry entry `json:"entry"`
Type string `json:"type"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"timeline_v2"`
} `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" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
tweets = append(tweets, tweet)
}
}
}
}
return tweets, cursor
}
type threadedConversation struct {
Data struct {
ThreadedConversationWithInjectionsV2 struct {
Instructions []struct {
Type string `json:"type"`
Entries []entry `json:"entries"`
Entry entry `json:"entry"`
} `json:"instructions"`
} `json:"threaded_conversation_with_injections_v2"`
} `json:"data"`
}
func (conversation *threadedConversation) parse() []*Tweet {
var tweets []*Tweet
for _, instruction := range conversation.Data.ThreadedConversationWithInjectionsV2.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.ItemContent.TweetResults.Result.Typename == "Tweet" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
if entry.Content.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
for _, item := range entry.Content.Items {
if item.Item.ItemContent.TweetResults.Result.Typename == "Tweet" {
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
if item.Item.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
}
}
}
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
}