forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.go
339 lines (285 loc) · 8.93 KB
/
tweet.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
package twitterscraper
import (
"bytes"
"encoding/json"
"errors"
"io"
"strconv"
)
type NewTweet struct {
Text string
Medias []*Media
}
type newTweet struct {
Data struct {
CreateTweet struct {
TweetResults struct {
Result tweet `json:"result"`
} `json:"tweet_results"`
} `json:"create_tweet"`
} `json:"data"`
}
func (newTweet *newTweet) parse() *Tweet {
var tweet = &newTweet.Data.CreateTweet.TweetResults.Result
if tweet.NoteTweet.NoteTweetResults.Result.Text != "" {
tweet.Legacy.FullText = tweet.NoteTweet.NoteTweetResults.Result.Text
}
var legacy *legacyTweet = &tweet.Legacy
var user *legacyUser = &tweet.Core.UserResults.Result.Legacy
tw := parseLegacyTweet(user, legacy)
if tw == nil {
return nil
}
if tw.Views == 0 && tweet.Views.Count != "" {
tw.Views, _ = strconv.Atoi(tweet.Views.Count)
}
if tweet.QuotedStatusResult.Result != nil {
tw.QuotedStatus = tweet.QuotedStatusResult.Result.parse()
}
return tw
}
func (s *Scraper) CreateTweet(tweet NewTweet) (*Tweet, error) {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/oB-5XsHNAbjvARJEc8CZFw/CreateTweet")
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/json")
media_entities := []map[string]interface{}{}
if len(tweet.Medias) > 0 {
for _, media := range tweet.Medias {
media_entities = append(media_entities, map[string]interface{}{
"media_id": strconv.Itoa(media.ID),
"tagged_users": []string{},
})
}
}
post_medias := map[string]interface{}{
"media_entities": media_entities,
"possibly_sensitive": false,
}
variables := map[string]interface{}{
"dark_request": false,
"media": post_medias,
"semantic_annotation_ids": []string{},
"tweet_text": tweet.Text,
}
features := map[string]interface{}{
"communities_web_enable_tweet_community_results_fetch": true,
"c9s_tweet_anatomy_moderator_badge_enabled": true,
"tweetypie_unmention_optimization_enabled": true,
"responsive_web_edit_tweet_api_enabled": true,
"graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
"view_counts_everywhere_api_enabled": true,
"longform_notetweets_consumption_enabled": true,
"responsive_web_twitter_article_tweet_consumption_enabled": true,
"tweet_awards_web_tipping_enabled": false,
"creator_subscriptions_quote_tweet_preview_enabled": false,
"longform_notetweets_rich_text_read_enabled": true,
"longform_notetweets_inline_media_enabled": true,
"articles_preview_enabled": true,
"rweb_video_timestamps_enabled": true,
"rweb_tipjar_consumption_enabled": true,
"responsive_web_graphql_exclude_directive_enabled": true,
"verified_phone_label_enabled": false,
"freedom_of_speech_not_reach_fetch_enabled": true,
"standardized_nudges_misinfo": true,
"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
"responsive_web_graphql_timeline_navigation_enabled": true,
"responsive_web_enhance_cards_enabled": false,
}
body := map[string]interface{}{
"features": features,
"variables": variables,
"queryId": "oB-5XsHNAbjvARJEc8CZFw",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response newTweet
err = s.RequestAPI(req, &response)
if err != nil {
return nil, err
}
if result := response.parse(); result != nil {
return result, nil
}
return nil, errors.New("tweet wasn't post")
}
func (s *Scraper) DeleteTweet(tweetId string) error {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/VaenaVgh5q5ih7kvyVjgtg/DeleteTweet")
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
variables := map[string]interface{}{
"dark_request": false,
"tweet_id": tweetId,
}
body := map[string]interface{}{
"variables": variables,
"queryId": "VaenaVgh5q5ih7kvyVjgtg",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response struct {
Data struct {
CreateTweet struct {
TweetResults struct {
} `json:"tweet_results"`
} `json:"delete_tweet"`
} `json:"data"`
}
err = s.RequestAPI(req, &response)
if err != nil {
return err
}
return nil
}
func (s *Scraper) CreateRetweet(tweetId string) (string, error) {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet")
if err != nil {
return "", err
}
req.Header.Set("content-type", "application/json")
variables := map[string]interface{}{
"dark_request": false,
"tweet_id": tweetId,
}
body := map[string]interface{}{
"variables": variables,
"queryId": "ojPdsZsimiJrUGLR1sjUtA",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response struct {
Data struct {
CreateRetweet struct {
RetweetResults struct {
Result struct {
RestID string `json:"rest_id"`
Legacy struct {
FullText string `json:"full_text"`
} `json:"legacy"`
} `json:"result"`
} `json:"retweet_results"`
} `json:"create_retweet"`
} `json:"data"`
}
err = s.RequestAPI(req, &response)
if err != nil {
return "", err
}
if response.Data.CreateRetweet.RetweetResults.Result.RestID != "" {
return response.Data.CreateRetweet.RetweetResults.Result.RestID, nil
}
return "", errors.New("tweet wasn't retweeted")
}
// Retweeted tweets has their own id, but to delete retweet twitter using id of source tweet
func (s *Scraper) DeleteRetweet(tweetId string) error {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/iQtK4dl5hBmXewYZuEOKVw/DeleteRetweet")
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
variables := map[string]interface{}{
"dark_request": false,
"source_tweet_id": tweetId,
}
body := map[string]interface{}{
"variables": variables,
"queryId": "iQtK4dl5hBmXewYZuEOKVw",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response struct {
Data struct {
Unretweet struct {
SourceTweetResults struct {
Result struct {
RestID string `json:"rest_id"`
Legacy struct {
FullText string `json:"full_text"`
} `json:"legacy"`
} `json:"result"`
} `json:"source_tweet_results"`
} `json:"unretweet"`
} `json:"data"`
}
err = s.RequestAPI(req, &response)
if err != nil {
return err
}
return nil
}
func (s *Scraper) LikeTweet(tweetId string) error {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet")
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
variables := map[string]interface{}{
"tweet_id": tweetId,
}
body := map[string]interface{}{
"variables": variables,
"queryId": "lI07N6Otwv1PhnEgXILM7A",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response struct {
Data struct {
FavoriteTweet string `json:"favorite_tweet"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
Code int `json:"code"`
} `json:"errors"`
}
err = s.RequestAPI(req, &response)
if err != nil {
return err
}
if len(response.Errors) > 0 && response.Errors[0].Code == 139 {
return errors.New("tweet already liked")
}
if response.Data.FavoriteTweet != "Done" {
return errors.New("unknown error")
}
return nil
}
func (s *Scraper) UnlikeTweet(tweetId string) error {
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/ZYKSe-w7KEslx3JhSIk5LA/UnfavoriteTweet")
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
variables := map[string]interface{}{
"tweet_id": tweetId,
}
body := map[string]interface{}{
"variables": variables,
"queryId": "ZYKSe-w7KEslx3JhSIk5LA",
}
b, _ := json.Marshal(body)
req.Body = io.NopCloser(bytes.NewReader(b))
var response struct {
Data struct {
UnfavoriteTweet string `json:"unfavorite_tweet"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
Code int `json:"code"`
} `json:"errors"`
}
err = s.RequestAPI(req, &response)
if err != nil {
return err
}
if len(response.Errors) > 0 && response.Errors[0].Code == 144 {
return errors.New("tweet already not liked")
}
if response.Data.UnfavoriteTweet != "Done" {
return errors.New("unknown error")
}
return nil
}