2021-12-07 10:18:01 +02:00
package twitterscraper_test
2019-09-21 11:56:06 +03:00
import (
2021-03-10 09:02:02 +02:00
"fmt"
2019-09-21 11:56:06 +03:00
"testing"
2020-12-11 20:58:49 +02:00
"time"
2019-09-21 11:56:06 +03:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2021-12-07 10:18:01 +02:00
twitterscraper "github.com/n0madic/twitter-scraper"
2019-09-21 11:56:06 +03:00
)
func TestGetProfile ( t * testing . T ) {
2020-12-11 20:58:49 +02:00
loc := time . FixedZone ( "UTC" , 0 )
2021-07-02 14:55:49 +03:00
joined := time . Date ( 2010 , 01 , 18 , 8 , 49 , 30 , 0 , loc )
2021-12-07 10:18:01 +02:00
sample := twitterscraper . Profile {
2021-07-02 14:55:49 +03:00
Avatar : "https://pbs.twimg.com/profile_images/436075027193004032/XlDa2oaz_normal.jpeg" ,
Banner : "https://pbs.twimg.com/profile_banners/106037940/1541084318" ,
Biography : "nothing" ,
2020-12-11 20:58:49 +02:00
// Birthday: "March 21",
IsPrivate : false ,
2021-07-02 14:55:49 +03:00
IsVerified : false ,
2020-12-11 20:58:49 +02:00
Joined : & joined ,
2021-07-02 14:55:49 +03:00
Location : "Ukraine" ,
Name : "Nomadic" ,
2020-12-23 10:37:08 +02:00
PinnedTweetIDs : [ ] string { } ,
2021-07-02 14:55:49 +03:00
URL : "https://twitter.com/nomadic_ua" ,
UserID : "106037940" ,
Username : "nomadic_ua" ,
Website : "https://nomadic.name" ,
2019-09-21 11:56:06 +03:00
}
2022-05-04 11:55:12 +03:00
scraper := twitterscraper . New ( )
profile , err := scraper . GetProfile ( "nomadic_ua" )
2019-09-21 11:56:06 +03:00
if err != nil {
t . Error ( err )
}
2020-09-22 10:17:51 +03:00
cmpOptions := cmp . Options {
2021-12-07 10:18:01 +02:00
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FollowersCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FollowingCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FriendsCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "LikesCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "ListedCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "TweetsCount" ) ,
2019-09-21 11:56:06 +03:00
}
if diff := cmp . Diff ( sample , profile , cmpOptions ... ) ; diff != "" {
t . Error ( "Resulting profile does not match the sample" , diff )
}
if profile . FollowersCount == 0 {
2019-09-21 12:06:42 +03:00
t . Error ( "Expected FollowersCount is greater than zero" )
2019-09-21 11:56:06 +03:00
}
if profile . FollowingCount == 0 {
2019-09-21 12:06:42 +03:00
t . Error ( "Expected FollowingCount is greater than zero" )
2019-09-21 11:56:06 +03:00
}
2020-12-11 20:58:49 +02:00
if profile . LikesCount == 0 {
t . Error ( "Expected LikesCount is greater than zero" )
}
2019-09-21 11:56:06 +03:00
if profile . TweetsCount == 0 {
2019-09-21 12:06:42 +03:00
t . Error ( "Expected TweetsCount is greater than zero" )
2019-09-21 11:56:06 +03:00
}
}
2021-01-25 10:31:41 +07:00
func TestGetProfilePrivate ( t * testing . T ) {
loc := time . FixedZone ( "UTC" , 0 )
2021-01-28 10:54:43 +02:00
joined := time . Date ( 2020 , 1 , 26 , 0 , 3 , 5 , 0 , loc )
2021-12-07 10:18:01 +02:00
sample := twitterscraper . Profile {
2021-01-28 10:54:43 +02:00
Avatar : "https://pbs.twimg.com/profile_images/1222218816484020224/ik9P1QZt_normal.jpg" ,
Banner : "" ,
2022-06-16 11:22:30 +03:00
Biography : ` "Beware that, when fighting monsters, you yourself do not become a monster... for when you gaze long into the abyss. The abyss gazes also into you." -Nietzsche ` ,
2021-01-25 10:31:41 +07:00
// Birthday: "March 21",
IsPrivate : true ,
IsVerified : false ,
Joined : & joined ,
Location : "" ,
2021-01-28 10:54:43 +02:00
Name : "private account" ,
2021-01-25 10:31:41 +07:00
PinnedTweetIDs : [ ] string { } ,
2021-01-28 10:54:43 +02:00
URL : "https://twitter.com/tomdumont" ,
UserID : "1221221876849995777" ,
Username : "tomdumont" ,
Website : "" ,
2021-01-25 10:31:41 +07:00
}
2022-05-04 11:55:12 +03:00
scraper := twitterscraper . New ( )
2021-01-25 10:31:41 +07:00
// some random private profile (found via google)
2022-05-04 11:55:12 +03:00
profile , err := scraper . GetProfile ( "tomdumont" )
2021-01-25 10:31:41 +07:00
if err != nil {
t . Error ( err )
}
cmpOptions := cmp . Options {
2021-12-07 10:18:01 +02:00
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FollowersCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FollowingCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "FriendsCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "LikesCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "ListedCount" ) ,
cmpopts . IgnoreFields ( twitterscraper . Profile { } , "TweetsCount" ) ,
2021-01-25 10:31:41 +07:00
}
if diff := cmp . Diff ( sample , profile , cmpOptions ... ) ; diff != "" {
t . Error ( "Resulting profile does not match the sample" , diff )
}
if profile . FollowingCount == 0 {
t . Error ( "Expected FollowingCount is greater than zero" )
}
if profile . LikesCount == 0 {
t . Error ( "Expected LikesCount is greater than zero" )
}
if profile . TweetsCount == 0 {
t . Error ( "Expected TweetsCount is greater than zero" )
}
}
func TestGetProfileErrorSuspended ( t * testing . T ) {
2022-05-04 11:55:12 +03:00
scraper := twitterscraper . New ( )
_ , err := scraper . GetProfile ( "123" )
2021-01-25 10:31:41 +07:00
if err == nil {
t . Error ( "Expected Error, got success" )
} else {
2021-10-18 23:33:57 +03:00
if err . Error ( ) != "Authorization: User has been suspended. (63)" {
t . Errorf ( "Expected error 'Authorization: User has been suspended. (63)', got '%s'" , err )
2021-01-25 10:31:41 +07:00
}
}
}
func TestGetProfileErrorNotFound ( t * testing . T ) {
2021-03-10 09:02:02 +02:00
neUser := "sample3123131"
expectedError := fmt . Sprintf ( "User '%s' not found" , neUser )
2022-05-04 11:55:12 +03:00
scraper := twitterscraper . New ( )
_ , err := scraper . GetProfile ( neUser )
2021-01-25 10:31:41 +07:00
if err == nil {
t . Error ( "Expected Error, got success" )
} else {
2021-03-10 09:02:02 +02:00
if err . Error ( ) != expectedError {
t . Errorf ( "Expected error '%s', got '%s'" , expectedError , err )
2021-01-25 10:31:41 +07:00
}
}
}
2021-01-28 11:12:20 +02:00
func TestGetUserIDByScreenName ( t * testing . T ) {
2021-12-07 10:18:01 +02:00
scraper := twitterscraper . New ( )
2021-01-28 11:12:20 +02:00
userID , err := scraper . GetUserIDByScreenName ( "Twitter" )
if err != nil {
t . Errorf ( "getUserByScreenName() error = %v" , err )
}
if userID == "" {
t . Error ( "Expected non-empty user ID" )
}
}