From 80f6787aa07009f8e1543eb36ee220ef030be592 Mon Sep 17 00:00:00 2001 From: Alessio Date: Tue, 4 Jan 2022 13:15:47 -0500 Subject: [PATCH] Add getter for User tiny-profile-image URL --- scraper/user.go | 12 ++++++++++++ scraper/user_test.go | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/scraper/user.go b/scraper/user.go index a6def70..565c104 100644 --- a/scraper/user.go +++ b/scraper/user.go @@ -148,3 +148,15 @@ func (u User) compute_banner_image_local_path() string { } return string(u.Handle) + "_banner_" + base_name } + +/** + * 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 !r.MatchString(u.ProfileImageUrl) { + panic(fmt.Sprintf("Weird profile image url: %s", u.ProfileImageUrl)) + } + return r.ReplaceAllString(u.ProfileImageUrl, "_normal$1") +} diff --git a/scraper/user_test.go b/scraper/user_test.go index 13a1b50..0d7d135 100644 --- a/scraper/user_test.go +++ b/scraper/user_test.go @@ -64,6 +64,10 @@ func TestParseSingleUser(t *testing.T) { if user.ProfileImageUrl != expectedProfileImage { t.Errorf("Expected %q, got %q", expectedProfileImage, user.ProfileImageUrl) } + expected_tiny_profile_image := "https://pbs.twimg.com/profile_images/1064051934812913664/Lbwdb_C9_normal.jpg" + if user.GetTinyProfileImageUrl() != expected_tiny_profile_image { + t.Errorf("Expected %q, got %q", expected_tiny_profile_image, user.GetTinyProfileImageUrl()) + } expectedBannerImage := "https://pbs.twimg.com/profile_banners/44067298/1615134676" if user.BannerImageUrl != expectedBannerImage { t.Errorf("Expected %q, got %q", expectedBannerImage, user.BannerImageUrl)