From 1ea97e669aaf7b649666b975b4e73f153ac58477 Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 6 Jan 2022 15:21:27 -0500 Subject: [PATCH] Update tiny profile image logic for users with no profile image (including banned users) --- scraper/user.go | 17 ++++++++++++++++- scraper/user_test.go | 8 ++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/scraper/user.go b/scraper/user.go index 72f708c..f6fe5f8 100644 --- a/scraper/user.go +++ b/scraper/user.go @@ -10,6 +10,8 @@ import ( "offline_twitter/terminal_utils" ) +const DEFAULT_PROFILE_IMAGE_URL = "https://abs.twimg.com/sticky/default_profile_images/default_profile.png" + type UserID int64 type UserHandle string @@ -173,14 +175,27 @@ func (u User) compute_banner_image_local_path() string { * Get the URL where we would expect to find a User's tiny profile image */ func (u User) GetTinyProfileImageUrl() string { - // Check that the format is as expected r := regexp.MustCompile(`(\.\w{2,4})$`) + + // If profile image is empty, then just use the default profile image + if u.ProfileImageUrl == "" { + return r.ReplaceAllString(DEFAULT_PROFILE_IMAGE_URL, "_normal$1") + } + + // Check that the format is as expected if !r.MatchString(u.ProfileImageUrl) { panic(fmt.Sprintf("Weird profile image url: %s", u.ProfileImageUrl)) } return r.ReplaceAllString(u.ProfileImageUrl, "_normal$1") } +/** + * If user has a profile image, return the local path for its tiny version. + * If user has a blank or default profile image, return a non-personalized default path. + */ func (u User) GetTinyProfileImageLocalPath() string { + if u.ProfileImageUrl == "" { + return path.Base(u.GetTinyProfileImageUrl()) + } return string(u.Handle) + "_profile_" + path.Base(u.GetTinyProfileImageUrl()) } diff --git a/scraper/user_test.go b/scraper/user_test.go index 6bafed7..e86172a 100644 --- a/scraper/user_test.go +++ b/scraper/user_test.go @@ -115,6 +115,14 @@ func TestParseBannedUser(t *testing.T) { if user.IsBanned != true { t.Errorf("Expected user to be banned") } + + // Test generation of profile images for banned user + if user.GetTinyProfileImageUrl() != "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png" { + t.Errorf("Incorrect tiny profile image URL for banned user: %q", user.GetTinyProfileImageUrl()) + } + if user.GetTinyProfileImageLocalPath() != "default_profile_normal.png" { + t.Errorf("Incorrect tiny profile image local path for banned user: %q", user.GetTinyProfileImageLocalPath()) + } } /**