twitter-scrapper/profile_test.go

171 lines
5.1 KiB
Go
Raw Normal View History

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"
"time"
2019-09-21 11:56:06 +03:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2024-01-28 23:06:45 +03:00
twitterscraper "github.com/imperatrona/twitter-scraper"
2019-09-21 11:56:06 +03:00
)
func TestGetProfile(t *testing.T) {
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",
// Birthday: "March 21",
IsPrivate: false,
2021-07-02 14:55:49 +03:00
IsVerified: false,
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()
err := scraper.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
2022-05-04 11:55:12 +03:00
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
}
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
}
}
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{
2023-01-10 13:02:35 +02:00
Avatar: "https://pbs.twimg.com/profile_images/1612213936082030594/_HEsjv7Q_normal.jpg",
Banner: "https://pbs.twimg.com/profile_banners/1221221876849995777/1673110776",
Biography: "t h e h e r m i t",
// Birthday: "March 21",
IsPrivate: true,
IsVerified: false,
Joined: &joined,
2023-01-10 13:02:35 +02:00
Location: "sometimes",
2021-01-28 10:54:43 +02:00
Name: "private account",
PinnedTweetIDs: []string{},
2021-01-28 10:54:43 +02:00
URL: "https://twitter.com/tomdumont",
UserID: "1221221876849995777",
Username: "tomdumont",
Website: "",
}
2022-05-04 11:55:12 +03:00
scraper := twitterscraper.New()
err := scraper.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
// some random private profile (found via google)
2022-05-04 11:55:12 +03:00
profile, err := scraper.GetProfile("tomdumont")
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"),
}
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.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
_, err = scraper.GetProfile("123")
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)
}
}
}
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.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
_, err = scraper.GetProfile(neUser)
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-28 11:12:20 +02:00
func TestGetUserIDByScreenName(t *testing.T) {
2021-12-07 10:18:01 +02:00
scraper := twitterscraper.New()
err := scraper.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
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")
}
}