Add LocalPath fields for BannerImage and ProfileImage on a User

This commit is contained in:
Alessio 2021-08-10 19:51:56 -07:00
parent 3de47f6bde
commit 655c968614
2 changed files with 60 additions and 13 deletions

View File

@ -4,6 +4,8 @@ import (
"time" "time"
"fmt" "fmt"
"strings" "strings"
"regexp"
"path"
"offline_twitter/terminal_utils" "offline_twitter/terminal_utils"
) )
@ -20,19 +22,22 @@ func JoinArrayOfHandles(handles []UserHandle) string {
} }
type User struct { type User struct {
ID UserID ID UserID
DisplayName string DisplayName string
Handle UserHandle Handle UserHandle
Bio string Bio string
FollowingCount int FollowingCount int
FollowersCount int FollowersCount int
Location string Location string
Website string Website string
JoinDate time.Time JoinDate time.Time
IsPrivate bool IsPrivate bool
IsVerified bool IsVerified bool
ProfileImageUrl string ProfileImageUrl string
BannerImageUrl string ProfileImageLocalPath string
BannerImageUrl string
BannerImageLocalPath string
PinnedTweetID TweetID PinnedTweetID TweetID
PinnedTweet *Tweet PinnedTweet *Tweet
@ -93,6 +98,10 @@ func ParseSingleUser(apiUser APIUser) (ret User, err error) {
ret.IsVerified = apiUser.Verified ret.IsVerified = apiUser.Verified
ret.ProfileImageUrl = apiUser.ProfileImageURLHTTPS ret.ProfileImageUrl = apiUser.ProfileImageURLHTTPS
ret.BannerImageUrl = apiUser.ProfileBannerURL 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 { if len(apiUser.PinnedTweetIdsStr) > 0 {
ret.PinnedTweetID = TweetID(idstr_to_int(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) 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
}

View File

@ -68,6 +68,14 @@ func TestParseSingleUser(t *testing.T) {
if user.BannerImageUrl != expectedBannerImage { if user.BannerImageUrl != expectedBannerImage {
t.Errorf("Expected %q, got %q", expectedBannerImage, user.BannerImageUrl) 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 expected_id = 1403835414373339136
if user.PinnedTweetID != scraper.TweetID(expected_id) { if user.PinnedTweetID != scraper.TweetID(expected_id) {
t.Errorf("Expected %q, got %q", expected_id, user.PinnedTweet) t.Errorf("Expected %q, got %q", expected_id, user.PinnedTweet)