From d81fae0013d1c73033f322389b0405e690b3aec7 Mon Sep 17 00:00:00 2001 From: Alessio Date: Wed, 4 Aug 2021 02:00:58 -0700 Subject: [PATCH] Make UserID an alias for `int64` rather than `string` --- persistence/tweet_queries.go | 4 +--- persistence/user_queries.go | 4 +--- persistence/user_queries_test.go | 4 ++-- persistence/utils_test.go | 8 ++++---- scraper/api_request_utils.go | 4 ++-- scraper/api_types.go | 10 +++++----- scraper/api_types_test.go | 4 ++-- scraper/retweet.go | 2 +- scraper/retweet_test.go | 10 ++++++---- scraper/tweet.go | 2 +- scraper/tweet_test.go | 1 - scraper/user.go | 4 ++-- scraper/user_test.go | 5 +++-- 13 files changed, 30 insertions(+), 32 deletions(-) diff --git a/persistence/tweet_queries.go b/persistence/tweet_queries.go index 9539f45..886feb9 100644 --- a/persistence/tweet_queries.go +++ b/persistence/tweet_queries.go @@ -121,10 +121,9 @@ func (p Profile) GetTweetById(id scraper.TweetID) (scraper.Tweet, error) { var mentions string var hashtags string var tweet_id int64 - var user_id int64 row := stmt.QueryRow(id) - err = row.Scan(&tweet_id, &user_id, &t.Text, &postedAt, &t.NumLikes, &t.NumRetweets, &t.NumReplies, &t.NumQuoteTweets, &t.InReplyTo, &t.QuotedTweet, &mentions, &hashtags) + err = row.Scan(&tweet_id, &t.UserID, &t.Text, &postedAt, &t.NumLikes, &t.NumRetweets, &t.NumReplies, &t.NumQuoteTweets, &t.InReplyTo, &t.QuotedTweet, &mentions, &hashtags) if err != nil { return t, err } @@ -135,7 +134,6 @@ func (p Profile) GetTweetById(id scraper.TweetID) (scraper.Tweet, error) { } t.Hashtags = strings.Split(hashtags, ",") t.ID = scraper.TweetID(fmt.Sprint(tweet_id)) - t.UserID = scraper.UserID(fmt.Sprint(user_id)) imgs, err := p.GetImagesForTweet(t) if err != nil { diff --git a/persistence/user_queries.go b/persistence/user_queries.go index 5ab8003..d3f4708 100644 --- a/persistence/user_queries.go +++ b/persistence/user_queries.go @@ -81,15 +81,13 @@ func (p Profile) UserExists(handle scraper.UserHandle) bool { func parse_user_from_row(row *sql.Row) (scraper.User, error) { var u scraper.User var joinDate int64 - var user_id int64 var pinned_tweet_id int64 - err := row.Scan(&user_id, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.ProfileImageUrl, &u.BannerImageUrl, &pinned_tweet_id) + err := row.Scan(&u.ID, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.ProfileImageUrl, &u.BannerImageUrl, &pinned_tweet_id) if err != nil { return u, err } - u.ID = scraper.UserID(fmt.Sprint(user_id)) u.JoinDate = time.Unix(joinDate, 0) u.PinnedTweetID = scraper.TweetID(fmt.Sprint(pinned_tweet_id)) diff --git a/persistence/user_queries_test.go b/persistence/user_queries_test.go index 706e548..52e85cf 100644 --- a/persistence/user_queries_test.go +++ b/persistence/user_queries_test.go @@ -53,7 +53,7 @@ func TestUserExists(t *testing.T) { exists := profile.UserExists(user.Handle) if exists { - t.Errorf("It shouldn't exist, but it does: %s", user.ID) + t.Errorf("It shouldn't exist, but it does: %d", user.ID) } err := profile.SaveUser(user) if err != nil { @@ -61,6 +61,6 @@ func TestUserExists(t *testing.T) { } exists = profile.UserExists(user.Handle) if !exists { - t.Errorf("It should exist, but it doesn't: %s", user.ID) + t.Errorf("It should exist, but it doesn't: %d", user.ID) } } diff --git a/persistence/utils_test.go b/persistence/utils_test.go index 23d0175..f5eb188 100644 --- a/persistence/utils_test.go +++ b/persistence/utils_test.go @@ -41,7 +41,7 @@ func create_or_load_profile(profile_path string) persistence.Profile { */ func create_stable_user() scraper.User { return scraper.User{ - ID: scraper.UserID("-1"), + ID: scraper.UserID(-1), DisplayName: "stable display name", Handle: scraper.UserHandle("handle stable"), Bio: "stable bio", @@ -78,7 +78,7 @@ func create_stable_tweet() scraper.Tweet { tweet_id := scraper.TweetID("-1") return scraper.Tweet{ ID: tweet_id, - UserID: "-1", + UserID: -1, Text: "stable text", PostedAt: time.Unix(10000000, 0), NumLikes: 10, @@ -101,12 +101,12 @@ func create_stable_tweet() scraper.Tweet { */ func create_dummy_user() scraper.User { rand.Seed(time.Now().UnixNano()) - userID := fmt.Sprint(rand.Int()) + userID := rand.Int() return scraper.User{ ID: scraper.UserID(userID), DisplayName: "display name", - Handle: scraper.UserHandle("handle" + userID), + Handle: scraper.UserHandle(fmt.Sprintf("handle%d", userID)), Bio: "bio", FollowersCount: 0, FollowingCount: 1000, diff --git a/scraper/api_request_utils.go b/scraper/api_request_utils.go index 0b68c90..c91cd07 100644 --- a/scraper/api_request_utils.go +++ b/scraper/api_request_utils.go @@ -15,7 +15,7 @@ type API struct{} func (api API) GetFeedFor(user_id UserID, cursor string) (TweetResponse, error) { client := &http.Client{Timeout: 10 * time.Second} - req, err := http.NewRequest("GET", API_USER_TIMELINE_BASE_PATH + string(user_id) + ".json", nil) + req, err := http.NewRequest("GET", fmt.Sprintf("%s%d.json", API_USER_TIMELINE_BASE_PATH, user_id), nil) if err != nil { return TweetResponse{}, err } @@ -77,7 +77,7 @@ func (api API) GetMoreTweets(user_id UserID, response *TweetResponse, max_tweets func (api API) GetTweet(id TweetID, cursor string) (TweetResponse, error) { client := &http.Client{Timeout: 10 * time.Second} - req, err := http.NewRequest("GET", API_CONVERSATION_BASE_PATH + string(id) + ".json", nil) + req, err := http.NewRequest("GET", fmt.Sprintf("%s%d.json", API_CONVERSATION_BASE_PATH, id), nil) if err != nil { return TweetResponse{}, err } diff --git a/scraper/api_types.go b/scraper/api_types.go index cf906a0..977fd7b 100644 --- a/scraper/api_types.go +++ b/scraper/api_types.go @@ -38,7 +38,7 @@ type APITweet struct { } `json:"urls"` Mentions []struct { UserName string `json:"screen_name"` - UserID string `json:"id_str"` + UserID int64 `json:"id_str,string"` } `json:"user_mentions"` } `json:"entities"` ExtendedEntities struct { @@ -59,7 +59,7 @@ type APITweet struct { RetweetedStatusIDStr string `json:"retweeted_status_id_str"` QuotedStatusIDStr string `json:"quoted_status_id_str"` Time time.Time `json:"time"` - UserIDStr string `json:"user_id_str"` + UserID int64 `json:"user_id_str,string"` } func (t *APITweet) NormalizeContent() { @@ -107,7 +107,7 @@ type APIUser struct { FavouritesCount int `json:"favourites_count"` FollowersCount int `json:"followers_count"` FriendsCount int `json:"friends_count"` - IDStr string `json:"id_str"` + ID int64 `json:"id_str,string"` ListedCount int `json:"listed_count"` Name string `json:"name"` Location string `json:"location"` @@ -124,14 +124,14 @@ type APIUser struct { type UserResponse struct { Data struct { User struct { - ID string `json:"rest_id"` + ID int64 `json:"rest_id,string"` Legacy APIUser `json:"legacy"` } `json:"user"` } `json:"data"` } func (u UserResponse) ConvertToAPIUser() APIUser { ret := u.Data.User.Legacy - ret.IDStr = u.Data.User.ID + ret.ID = u.Data.User.ID return ret } diff --git a/scraper/api_types_test.go b/scraper/api_types_test.go index 4be68e1..1ed2120 100644 --- a/scraper/api_types_test.go +++ b/scraper/api_types_test.go @@ -52,8 +52,8 @@ func TestUserProfileToAPIUser(t *testing.T) { result := user_resp.ConvertToAPIUser() - if result.IDStr != "44067298" { - t.Errorf("Expected IDStr %q, got %q", "44067298", result.IDStr) + if result.ID != 44067298 { + t.Errorf("Expected ID %q, got %q", 44067298, result.ID) } if result.FollowersCount != user_resp.Data.User.Legacy.FollowersCount { t.Errorf("Expected user count %d, got %d", user_resp.Data.User.Legacy.FollowersCount, result.FollowersCount) diff --git a/scraper/retweet.go b/scraper/retweet.go index 2d639d8..0c1771d 100644 --- a/scraper/retweet.go +++ b/scraper/retweet.go @@ -16,7 +16,7 @@ type Retweet struct { func ParseSingleRetweet(apiTweet APITweet) (ret Retweet, err error) { ret.RetweetID = TweetID(apiTweet.ID) ret.TweetID = TweetID(apiTweet.RetweetedStatusIDStr) - ret.RetweetedByID = UserID(apiTweet.UserIDStr) + ret.RetweetedByID = UserID(apiTweet.UserID) ret.RetweetedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt) return } diff --git a/scraper/retweet_test.go b/scraper/retweet_test.go index 85de3a4..7744328 100644 --- a/scraper/retweet_test.go +++ b/scraper/retweet_test.go @@ -30,10 +30,12 @@ func TestParseSingleRetweet(t *testing.T) { if retweet.TweetID != "1404269989646028804" { t.Errorf("Expected %q, got %q", "1404269989646028804", retweet.TweetID) } - if retweet.RetweetedByID != "44067298" { - t.Errorf("Expected %q, got %q", "44067298", retweet.RetweetedBy) + expected_id = 44067298 + if retweet.RetweetedByID != scraper.UserID(expected_id) { + t.Errorf("Expected %d, got %d", expected_id, retweet.RetweetedByID) } - if retweet.RetweetedAt.Unix() != 1623639042 { - t.Errorf("Expected %d, got %d", 1623639042, retweet.RetweetedAt.Unix()) + expected_id = 1623639042 + if retweet.RetweetedAt.Unix() != int64(expected_id) { + t.Errorf("Expected %d, got %d", expected_id, retweet.RetweetedAt.Unix()) } } diff --git a/scraper/tweet.go b/scraper/tweet.go index ffe92e7..91ad2c4 100644 --- a/scraper/tweet.go +++ b/scraper/tweet.go @@ -75,7 +75,7 @@ func ParseSingleTweet(apiTweet APITweet) (ret Tweet, err error) { apiTweet.NormalizeContent() ret.ID = TweetID(apiTweet.ID) - ret.UserID = UserID(apiTweet.UserIDStr) + ret.UserID = UserID(apiTweet.UserID) ret.Text = apiTweet.FullText ret.PostedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt) diff --git a/scraper/tweet_test.go b/scraper/tweet_test.go index e306ad8..af9278a 100644 --- a/scraper/tweet_test.go +++ b/scraper/tweet_test.go @@ -1,7 +1,6 @@ package scraper_test import ( - // "fmt" "encoding/json" "io/ioutil" "testing" diff --git a/scraper/user.go b/scraper/user.go index a9fe51a..863a8ff 100644 --- a/scraper/user.go +++ b/scraper/user.go @@ -8,7 +8,7 @@ import ( "offline_twitter/terminal_utils" ) -type UserID string +type UserID int64 type UserHandle string func JoinArrayOfHandles(handles []UserHandle) string { @@ -73,7 +73,7 @@ Joined %s // Turn an APIUser, as returned from the scraper, into a properly structured User object func ParseSingleUser(apiUser APIUser) (ret User, err error) { - ret.ID = UserID(apiUser.IDStr) + ret.ID = UserID(apiUser.ID) ret.DisplayName = apiUser.Name ret.Handle = UserHandle(apiUser.ScreenName) ret.Bio = apiUser.Description diff --git a/scraper/user_test.go b/scraper/user_test.go index 720e5a9..00e4d12 100644 --- a/scraper/user_test.go +++ b/scraper/user_test.go @@ -25,8 +25,9 @@ func TestParseSingleUser(t *testing.T) { t.Errorf(err.Error()) } - if user.ID != "44067298" { - t.Errorf("Expected %q, got %q", "44067298", user.ID) + expected_id := 44067298 + if user.ID != scraper.UserID(expected_id) { + t.Errorf("Expected %q, got %q", expected_id, user.ID) } if user.DisplayName != "Michael Malice" { t.Errorf("Expected %q, got %q", "Michael Malice", user.DisplayName)