forked from lisanmuaddib/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweets_test.go
469 lines (441 loc) · 15.2 KB
/
tweets_test.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package twitterscraper_test
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
twitterscraper "github.com/imperatrona/twitter-scraper"
)
var cmpOptions = cmp.Options{
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "Likes"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "Replies"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "Retweets"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "Views"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "IsSelfThread"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "Thread"),
cmpopts.IgnoreFields(twitterscraper.Tweet{}, "TimeParsed"),
}
func TestGetTweets(t *testing.T) {
count := 0
maxTweetsNbr := 100
dupcheck := make(map[string]bool)
for tweet := range testScraper.GetTweets(context.Background(), "x", maxTweetsNbr) {
if tweet.Error != nil {
t.Error(tweet.Error)
} else {
count++
if tweet.ID == "" {
t.Error("Expected tweet ID is empty")
} else {
if dupcheck[tweet.ID] {
t.Errorf("Detect duplicated tweet ID: %s", tweet.ID)
} else {
dupcheck[tweet.ID] = true
}
}
if tweet.UserID == "" {
t.Error("Expected tweet UserID is empty")
}
if tweet.Username == "" {
t.Error("Expected tweet Username is empty")
}
if tweet.PermanentURL == "" {
t.Error("Expected tweet PermanentURL is empty")
}
if tweet.Text == "" {
t.Error("Expected tweet Text is empty")
}
if tweet.TimeParsed.IsZero() {
t.Error("Expected tweet TimeParsed is zero")
}
if tweet.Timestamp == 0 {
t.Error("Expected tweet Timestamp is greater than zero")
}
for _, video := range tweet.Videos {
if video.ID == "" {
t.Error("Expected tweet video ID is empty")
}
if video.Preview == "" {
t.Error("Expected tweet video Preview is empty")
}
if video.URL == "" {
t.Error("Expected tweet video URL is empty")
}
}
}
}
if count != maxTweetsNbr {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
}
}
func assertGetTweet(t *testing.T, expectedTweet *twitterscraper.Tweet) {
// to get tweet as struct fmt.Printf("%#v", actualTweet)
actualTweet, err := testScraper.GetTweet(expectedTweet.ID)
if err != nil {
t.Error(err)
} else if diff := cmp.Diff(expectedTweet, actualTweet, cmpOptions...); diff != "" {
t.Error("Resulting tweet does not match the sample", diff)
}
}
func TestGetTweetWithVideo(t *testing.T) {
expectedTweet := twitterscraper.Tweet{
ConversationID: "1697304622749086011",
HTML: "on iOS & Android, you can now swipe to reply when you slide into their DMs <br><a href=\"https://t.co/evuWpMfBxQ\"><img src=\"https://pbs.twimg.com/amplify_video_thumb/1697304568550330368/img/BUlESpef6FmWV_j2.jpg\"/></a>",
ID: "1697304622749086011",
Name: "X",
PermanentURL: "https://twitter.com/X/status/1697304622749086011",
Photos: nil,
Text: "on iOS & Android, you can now swipe to reply when you slide into their DMs https://t.co/evuWpMfBxQ",
Timestamp: 1693503931,
UserID: "783214",
Username: "X",
Videos: []twitterscraper.Video{
{
ID: "1697304568550330368",
Preview: "https://pbs.twimg.com/amplify_video_thumb/1697304568550330368/img/BUlESpef6FmWV_j2.jpg",
URL: "https://video.twimg.com/amplify_video/1697304568550330368/vid/720x720/KyQlZA9zaf0kqY9Z.mp4?tag=14",
HLSURL: "https://video.twimg.com/amplify_video/1697304568550330368/pl/lhOwA_kBgWCnJX1l.m3u8?tag=14",
},
},
}
assertGetTweet(t, &expectedTweet)
}
func TestGetTweetWithMultiplePhotos(t *testing.T) {
expectedTweet := twitterscraper.Tweet{
ConversationID: "1577677328968204291",
HTML: "More ways to discover videos on Twitter are here!<br><br>Now on iOS, videos on your timeline will open in our full screen immersive video player, where you can swipe up to keep discovering more content. <br><a href=\"https://t.co/XI2vM8DKXA\"><img src=\"https://pbs.twimg.com/media/FeUJKdnXEAEFe2j.jpg\"/></a><br><img src=\"https://pbs.twimg.com/media/FeUJKuxXEAAa6t7.jpg\"/>",
ID: "1577677328968204291",
Name: "Support",
PermanentURL: "https://twitter.com/Support/status/1577677328968204291",
Photos: []twitterscraper.Photo{
{
ID: "1577677319816286209",
URL: "https://pbs.twimg.com/media/FeUJKdnXEAEFe2j.jpg",
},
{
ID: "1577677324421632000",
URL: "https://pbs.twimg.com/media/FeUJKuxXEAAa6t7.jpg",
},
},
Text: "More ways to discover videos on Twitter are here!\n\nNow on iOS, videos on your timeline will open in our full screen immersive video player, where you can swipe up to keep discovering more content. https://t.co/XI2vM8DKXA",
Timestamp: 1664982561,
UserID: "17874544",
Username: "Support",
}
assertGetTweet(t, &expectedTweet)
}
func TestGetTweetWithGIF(t *testing.T) {
expectedTweet := twitterscraper.Tweet{
ConversationID: "1517535384833605632",
GIFs: []twitterscraper.GIF{
{
ID: "1517535349890813952",
Preview: "https://pbs.twimg.com/tweet_video_thumb/FQ9eXEhXEAA-haj.jpg",
URL: "https://video.twimg.com/tweet_video/FQ9eXEhXEAA-haj.mp4",
},
},
HTML: "Video captions or no captions, it’s now easier to choose for some of you on iOS, and soon on Android.<br><br>On videos that have captions available, we’re testing the option to turn captions off/on with a new “CC” button. <br><a href=\"https://t.co/Q2Q2Wmr78U\"><img src=\"https://pbs.twimg.com/tweet_video_thumb/FQ9eXEhXEAA-haj.jpg\"/></a>",
ID: "1517535384833605632",
Name: "Support",
PermanentURL: "https://twitter.com/Support/status/1517535384833605632",
Text: "Video captions or no captions, it’s now easier to choose for some of you on iOS, and soon on Android.\n\nOn videos that have captions available, we’re testing the option to turn captions off/on with a new “CC” button. https://t.co/Q2Q2Wmr78U",
Timestamp: 1650643604,
UserID: "17874544",
Username: "Support",
}
assertGetTweet(t, &expectedTweet)
}
func TestGetTweetWithPhotoAndGIF(t *testing.T) {
expectedTweet := twitterscraper.Tweet{
ConversationID: "1583186305722507265",
GIFs: []twitterscraper.GIF{
{
ID: "1583186295588790290",
Preview: "https://pbs.twimg.com/tweet_video_thumb/FfibjDnWIBIt5fn.jpg",
URL: "https://video.twimg.com/tweet_video/FfibjDnWIBIt5fn.mp4",
},
},
HTML: "“we need to talk” <br><br>irl vs on Spaces <br><a href=\"https://t.co/hrflPpbpif\"><img src=\"https://pbs.twimg.com/tweet_video_thumb/FfibjDnWIBIt5fn.jpg\"/></a><br><img src=\"https://pbs.twimg.com/media/FfibjDwWIAwvbtJ.jpg\"/>",
ID: "1583186305722507265",
Name: "Spaces",
PermanentURL: "https://twitter.com/XSpaces/status/1583186305722507265",
Photos: []twitterscraper.Photo{{ID: "1583186295626539020", URL: "https://pbs.twimg.com/media/FfibjDwWIAwvbtJ.jpg"}},
Text: "“we need to talk” \n\nirl vs on Spaces https://t.co/hrflPpbpif",
Timestamp: 1666296004,
UserID: "1065249714214457345",
Username: "XSpaces",
}
assertGetTweet(t, &expectedTweet)
}
func TestTweetMentions(t *testing.T) {
sample := []twitterscraper.Mention{{
ID: "7018222",
Username: "davidmcraney",
Name: "David McRaney",
}}
tweet, err := testScraper.GetTweet("1554522888904101890")
if err != nil {
t.Error(err)
} else {
if diff := cmp.Diff(sample, tweet.Mentions, cmpOptions...); diff != "" {
t.Error("Resulting tweet does not match the sample", diff)
}
}
}
func TestQuotedAndReply(t *testing.T) {
sample := &twitterscraper.Tweet{
ConversationID: "1237110546383724547",
HTML: "The Easiest Problem Everyone Gets Wrong <br><br>[new video] --> <a href=\"https://youtu.be/ytfCdqWhmdg\">https://t.co/YdaeDYmPAU</a> <br><a href=\"https://t.co/iKu4Xs6o2V\"><img src=\"https://pbs.twimg.com/media/ESsZa9AXgAIAYnF.jpg\"/></a>",
ID: "1237110546383724547",
Likes: 485,
Name: "Vsauce2",
PermanentURL: "https://twitter.com/VsauceTwo/status/1237110546383724547",
Photos: []twitterscraper.Photo{{
ID: "1237110473486729218",
URL: "https://pbs.twimg.com/media/ESsZa9AXgAIAYnF.jpg",
}},
Replies: 12,
Retweets: 18,
Text: "The Easiest Problem Everyone Gets Wrong \n\n[new video] --> https://t.co/YdaeDYmPAU https://t.co/iKu4Xs6o2V",
Timestamp: 1583785113,
URLs: []string{"https://youtu.be/ytfCdqWhmdg"},
UserID: "978944851",
Username: "VsauceTwo",
}
tweet, err := testScraper.GetTweet("1237110897597976576")
if err != nil {
t.Error(err)
} else {
if !tweet.IsQuoted {
t.Error("IsQuoted must be True")
}
if diff := cmp.Diff(sample, tweet.QuotedStatus, cmpOptions...); diff != "" {
t.Error("Resulting quote does not match the sample", diff)
}
}
tweet, err = testScraper.GetTweet("1237111868445134850")
if err != nil {
t.Error(err)
} else {
if !tweet.IsReply {
t.Error("IsReply must be True")
}
if tweet.ConversationID != sample.ConversationID {
t.Error("Resulting reply does not match the required ConversationID")
}
}
}
func TestRetweet(t *testing.T) {
sample := &twitterscraper.Tweet{
ConversationID: "1758837061786779942",
HTML: "no ads, just bangers<br><br>aka your For You feed with Premium+<br><br>subscribe here → <a href=\"https://x.com/i/premium_sign_up\">https://t.co/APTO1t7kMk</a>",
ID: "1758837061786779942",
URLs: []string{"https://x.com/i/premium_sign_up"},
IsSelfThread: false,
Name: "Premium",
PermanentURL: "https://twitter.com/premium/status/1758837061786779942",
Text: "no ads, just bangers\n\naka your For You feed with Premium+\n\nsubscribe here → https://t.co/APTO1t7kMk",
Timestamp: 1708174407,
UserID: "1399766153053061121",
Username: "premium",
}
tweet, err := testScraper.GetTweet("1758837226379596068")
if err != nil {
t.Error(err)
} else {
if !tweet.IsRetweet {
t.Error("IsRetweet must be True")
}
if diff := cmp.Diff(sample, tweet.RetweetedStatus, cmpOptions...); diff != "" {
t.Error("Resulting retweet does not match the sample", diff)
}
}
}
func TestTweetViews(t *testing.T) {
sample := &twitterscraper.Tweet{
HTML: "Replies and likes don’t tell the whole story. We’re making it easier to tell *just* how many people have seen your Tweets with the addition of view counts, shown right next to likes. Now on iOS and Android, web coming soon.",
ID: "1606055187348688896",
Likes: 2839,
Name: "Support",
PermanentURL: "https://twitter.com/Support/status/1606055187348688896",
Replies: 3427,
Retweets: 783,
Text: "Replies and likes don’t tell the whole story. We’re making it easier to tell *just* how many people have seen your Tweets with the addition of view counts, shown right next to likes. Now on iOS and Android, web coming soon.",
Timestamp: 1612881838,
UserID: "17874544",
Username: "Support",
Views: 3189278,
}
tweet, err := testScraper.GetTweet("1606055187348688896")
if err != nil {
t.Error(err)
} else {
if tweet.Views < sample.Views {
t.Error("Views must be greater than or equal to the sample")
}
}
}
func TestTweetThread(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
tweet, err := testScraper.GetTweet("1665602315745673217")
if err != nil {
t.Fatal(err)
} else {
if !tweet.IsSelfThread {
t.Error("IsSelfThread must be True")
}
if len(tweet.Thread) != 7 {
t.Error("Thread length must be 7")
}
}
}
func TestFetchHomeTweets(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
tweets, _, err := testScraper.FetchHomeTweets(20, "")
if err != nil {
t.Fatal(err)
}
if len(tweets) < 1 {
t.Fatal("returned 0 tweets")
}
}
func TestGetHomeTweets(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxTweetsNbr := 150
dupcheck := make(map[string]bool)
for tweet := range testScraper.GetHomeTweets(context.Background(), maxTweetsNbr) {
if tweet.Error != nil {
t.Error(tweet.Error)
} else {
count++
if tweet.ID == "" {
t.Error("Expected tweet ID is empty")
} else {
if dupcheck[tweet.ID] {
t.Errorf("Detect duplicated tweet ID: %s", tweet.ID)
} else {
dupcheck[tweet.ID] = true
}
}
if tweet.UserID == "" {
t.Error("Expected tweet UserID is empty")
}
if tweet.Username == "" {
t.Error("Expected tweet Username is empty")
}
if tweet.PermanentURL == "" {
t.Error("Expected tweet PermanentURL is empty")
}
if tweet.Text == "" {
t.Error("Expected tweet Text is empty")
}
if tweet.TimeParsed.IsZero() {
t.Error("Expected tweet TimeParsed is zero")
}
if tweet.Timestamp == 0 {
t.Error("Expected tweet Timestamp is greater than zero")
}
for _, video := range tweet.Videos {
if video.ID == "" {
t.Error("Expected tweet video ID is empty")
}
if video.Preview == "" {
t.Error("Expected tweet video Preview is empty")
}
if video.URL == "" {
t.Error("Expected tweet video URL is empty")
}
}
}
}
if count != maxTweetsNbr {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
}
}
func TestFetchForYouTweets(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
tweets, _, err := testScraper.FetchForYouTweets(20, "")
if err != nil {
t.Fatal(err)
}
if len(tweets) < 1 {
t.Fatal("returned 0 tweets")
}
}
func TestGetForYouTweets(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxTweetsNbr := 150
dupcheck := make(map[string]bool)
for tweet := range testScraper.GetForYouTweets(context.Background(), maxTweetsNbr) {
if tweet.Error != nil {
t.Error(tweet.Error)
} else {
count++
if tweet.ID == "" {
t.Error("Expected tweet ID is empty")
} else {
if dupcheck[tweet.ID] {
t.Errorf("Detect duplicated tweet ID: %s", tweet.ID)
} else {
dupcheck[tweet.ID] = true
}
}
if tweet.UserID == "" {
t.Error("Expected tweet UserID is empty")
}
if tweet.Username == "" {
t.Error("Expected tweet Username is empty")
}
if tweet.PermanentURL == "" {
t.Error("Expected tweet PermanentURL is empty")
}
if tweet.Text == "" {
t.Error("Expected tweet Text is empty")
}
if tweet.TimeParsed.IsZero() {
t.Error("Expected tweet TimeParsed is zero")
}
if tweet.Timestamp == 0 {
t.Error("Expected tweet Timestamp is greater than zero")
}
for _, video := range tweet.Videos {
if video.ID == "" {
t.Error("Expected tweet video ID is empty")
}
if video.Preview == "" {
t.Error("Expected tweet video Preview is empty")
}
if video.URL == "" {
t.Error("Expected tweet video URL is empty")
}
}
}
}
if count != maxTweetsNbr {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
}
}
func TestGetTweetsAndReplies(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
tweets, _, err := testScraper.FetchTweetsAndRepliesByUserID("17874544", 20, "")
if err != nil {
t.Error(err)
}
if len(tweets) < 1 {
t.Errorf("Got %d tweets", len(tweets))
}
}