diff --git a/scraper/user.go b/scraper/user.go index 206507d..bcbcd8c 100644 --- a/scraper/user.go +++ b/scraper/user.go @@ -4,6 +4,8 @@ import ( "time" "fmt" "strings" + "regexp" + "path" "offline_twitter/terminal_utils" ) @@ -20,19 +22,22 @@ func JoinArrayOfHandles(handles []UserHandle) string { } type User struct { - ID UserID - DisplayName string - Handle UserHandle - Bio string - FollowingCount int - FollowersCount int - Location string - Website string - JoinDate time.Time - IsPrivate bool - IsVerified bool - ProfileImageUrl string - BannerImageUrl string + ID UserID + DisplayName string + Handle UserHandle + Bio string + FollowingCount int + FollowersCount int + Location string + Website string + JoinDate time.Time + IsPrivate bool + IsVerified bool + ProfileImageUrl string + ProfileImageLocalPath string + BannerImageUrl string + BannerImageLocalPath string + PinnedTweetID TweetID PinnedTweet *Tweet @@ -93,6 +98,10 @@ func ParseSingleUser(apiUser APIUser) (ret User, err error) { ret.IsVerified = apiUser.Verified ret.ProfileImageUrl = apiUser.ProfileImageURLHTTPS ret.BannerImageUrl = apiUser.ProfileBannerURL + + ret.ProfileImageLocalPath = ret.compute_profile_image_local_path() + ret.BannerImageLocalPath = ret.compute_banner_image_local_path() + if len(apiUser.PinnedTweetIdsStr) > 0 { ret.PinnedTweetID = TweetID(idstr_to_int(apiUser.PinnedTweetIdsStr[0])) } @@ -108,3 +117,33 @@ func GetUser(handle UserHandle) (User, error) { } return ParseSingleUser(apiUser) } + +/** + * Make a filename for the profile image, that hopefully won't clobber other ones + */ +func (u User) compute_profile_image_local_path() string { + return string(u.Handle) + "_profile_" + path.Base(u.ProfileImageUrl) +} + +/** + * Make a filename for the banner image, that hopefully won't clobber other ones. + * Add a file extension if necessary (seems to be necessary). + * If there is no banner image, just return nothing. + */ +func (u User) compute_banner_image_local_path() string { + if u.BannerImageUrl == "" { + return "" + } + base_name := path.Base(u.BannerImageUrl) + + // Check if it has an extension (e.g., ".png" or ".jpeg") + match, err := regexp.MatchString("\\.\\w{2,4}$", base_name) + if err != nil { + panic(err) + } + // If it doesn't have an extension, add one + if !match { + base_name += ".jpg" + } + return string(u.Handle) + "_banner_" + base_name +} diff --git a/scraper/user_test.go b/scraper/user_test.go index 3c137c1..d208c5b 100644 --- a/scraper/user_test.go +++ b/scraper/user_test.go @@ -68,6 +68,14 @@ func TestParseSingleUser(t *testing.T) { if user.BannerImageUrl != expectedBannerImage { t.Errorf("Expected %q, got %q", expectedBannerImage, user.BannerImageUrl) } + expected_profile_image_local := "michaelmalice_profile_Lbwdb_C9_normal.jpg" + if user.ProfileImageLocalPath != expected_profile_image_local { + t.Errorf("Expected %q, got %q", expected_profile_image_local, user.ProfileImageLocalPath) + } + expected_banner_image_local := "michaelmalice_banner_1615134676.jpg" + if user.BannerImageLocalPath != expected_banner_image_local { + t.Errorf("Expected %q, got %q", expected_banner_image_local, user.BannerImageLocalPath) + } expected_id = 1403835414373339136 if user.PinnedTweetID != scraper.TweetID(expected_id) { t.Errorf("Expected %q, got %q", expected_id, user.PinnedTweet)