Make UserID an alias for int64
rather than string
This commit is contained in:
parent
5e32f0605d
commit
d81fae0013
@ -121,10 +121,9 @@ func (p Profile) GetTweetById(id scraper.TweetID) (scraper.Tweet, error) {
|
|||||||
var mentions string
|
var mentions string
|
||||||
var hashtags string
|
var hashtags string
|
||||||
var tweet_id int64
|
var tweet_id int64
|
||||||
var user_id int64
|
|
||||||
|
|
||||||
row := stmt.QueryRow(id)
|
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 {
|
if err != nil {
|
||||||
return t, err
|
return t, err
|
||||||
}
|
}
|
||||||
@ -135,7 +134,6 @@ func (p Profile) GetTweetById(id scraper.TweetID) (scraper.Tweet, error) {
|
|||||||
}
|
}
|
||||||
t.Hashtags = strings.Split(hashtags, ",")
|
t.Hashtags = strings.Split(hashtags, ",")
|
||||||
t.ID = scraper.TweetID(fmt.Sprint(tweet_id))
|
t.ID = scraper.TweetID(fmt.Sprint(tweet_id))
|
||||||
t.UserID = scraper.UserID(fmt.Sprint(user_id))
|
|
||||||
|
|
||||||
imgs, err := p.GetImagesForTweet(t)
|
imgs, err := p.GetImagesForTweet(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -81,15 +81,13 @@ func (p Profile) UserExists(handle scraper.UserHandle) bool {
|
|||||||
func parse_user_from_row(row *sql.Row) (scraper.User, error) {
|
func parse_user_from_row(row *sql.Row) (scraper.User, error) {
|
||||||
var u scraper.User
|
var u scraper.User
|
||||||
var joinDate int64
|
var joinDate int64
|
||||||
var user_id int64
|
|
||||||
var pinned_tweet_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 {
|
if err != nil {
|
||||||
return u, err
|
return u, err
|
||||||
}
|
}
|
||||||
|
|
||||||
u.ID = scraper.UserID(fmt.Sprint(user_id))
|
|
||||||
u.JoinDate = time.Unix(joinDate, 0)
|
u.JoinDate = time.Unix(joinDate, 0)
|
||||||
u.PinnedTweetID = scraper.TweetID(fmt.Sprint(pinned_tweet_id))
|
u.PinnedTweetID = scraper.TweetID(fmt.Sprint(pinned_tweet_id))
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ func TestUserExists(t *testing.T) {
|
|||||||
|
|
||||||
exists := profile.UserExists(user.Handle)
|
exists := profile.UserExists(user.Handle)
|
||||||
if exists {
|
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)
|
err := profile.SaveUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,6 +61,6 @@ func TestUserExists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
exists = profile.UserExists(user.Handle)
|
exists = profile.UserExists(user.Handle)
|
||||||
if !exists {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ func create_or_load_profile(profile_path string) persistence.Profile {
|
|||||||
*/
|
*/
|
||||||
func create_stable_user() scraper.User {
|
func create_stable_user() scraper.User {
|
||||||
return scraper.User{
|
return scraper.User{
|
||||||
ID: scraper.UserID("-1"),
|
ID: scraper.UserID(-1),
|
||||||
DisplayName: "stable display name",
|
DisplayName: "stable display name",
|
||||||
Handle: scraper.UserHandle("handle stable"),
|
Handle: scraper.UserHandle("handle stable"),
|
||||||
Bio: "stable bio",
|
Bio: "stable bio",
|
||||||
@ -78,7 +78,7 @@ func create_stable_tweet() scraper.Tweet {
|
|||||||
tweet_id := scraper.TweetID("-1")
|
tweet_id := scraper.TweetID("-1")
|
||||||
return scraper.Tweet{
|
return scraper.Tweet{
|
||||||
ID: tweet_id,
|
ID: tweet_id,
|
||||||
UserID: "-1",
|
UserID: -1,
|
||||||
Text: "stable text",
|
Text: "stable text",
|
||||||
PostedAt: time.Unix(10000000, 0),
|
PostedAt: time.Unix(10000000, 0),
|
||||||
NumLikes: 10,
|
NumLikes: 10,
|
||||||
@ -101,12 +101,12 @@ func create_stable_tweet() scraper.Tweet {
|
|||||||
*/
|
*/
|
||||||
func create_dummy_user() scraper.User {
|
func create_dummy_user() scraper.User {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
userID := fmt.Sprint(rand.Int())
|
userID := rand.Int()
|
||||||
|
|
||||||
return scraper.User{
|
return scraper.User{
|
||||||
ID: scraper.UserID(userID),
|
ID: scraper.UserID(userID),
|
||||||
DisplayName: "display name",
|
DisplayName: "display name",
|
||||||
Handle: scraper.UserHandle("handle" + userID),
|
Handle: scraper.UserHandle(fmt.Sprintf("handle%d", userID)),
|
||||||
Bio: "bio",
|
Bio: "bio",
|
||||||
FollowersCount: 0,
|
FollowersCount: 0,
|
||||||
FollowingCount: 1000,
|
FollowingCount: 1000,
|
||||||
|
@ -15,7 +15,7 @@ type API struct{}
|
|||||||
|
|
||||||
func (api API) GetFeedFor(user_id UserID, cursor string) (TweetResponse, error) {
|
func (api API) GetFeedFor(user_id UserID, cursor string) (TweetResponse, error) {
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
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 {
|
if err != nil {
|
||||||
return TweetResponse{}, err
|
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) {
|
func (api API) GetTweet(id TweetID, cursor string) (TweetResponse, error) {
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
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 {
|
if err != nil {
|
||||||
return TweetResponse{}, err
|
return TweetResponse{}, err
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ type APITweet struct {
|
|||||||
} `json:"urls"`
|
} `json:"urls"`
|
||||||
Mentions []struct {
|
Mentions []struct {
|
||||||
UserName string `json:"screen_name"`
|
UserName string `json:"screen_name"`
|
||||||
UserID string `json:"id_str"`
|
UserID int64 `json:"id_str,string"`
|
||||||
} `json:"user_mentions"`
|
} `json:"user_mentions"`
|
||||||
} `json:"entities"`
|
} `json:"entities"`
|
||||||
ExtendedEntities struct {
|
ExtendedEntities struct {
|
||||||
@ -59,7 +59,7 @@ type APITweet struct {
|
|||||||
RetweetedStatusIDStr string `json:"retweeted_status_id_str"`
|
RetweetedStatusIDStr string `json:"retweeted_status_id_str"`
|
||||||
QuotedStatusIDStr string `json:"quoted_status_id_str"`
|
QuotedStatusIDStr string `json:"quoted_status_id_str"`
|
||||||
Time time.Time `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
UserIDStr string `json:"user_id_str"`
|
UserID int64 `json:"user_id_str,string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *APITweet) NormalizeContent() {
|
func (t *APITweet) NormalizeContent() {
|
||||||
@ -107,7 +107,7 @@ type APIUser struct {
|
|||||||
FavouritesCount int `json:"favourites_count"`
|
FavouritesCount int `json:"favourites_count"`
|
||||||
FollowersCount int `json:"followers_count"`
|
FollowersCount int `json:"followers_count"`
|
||||||
FriendsCount int `json:"friends_count"`
|
FriendsCount int `json:"friends_count"`
|
||||||
IDStr string `json:"id_str"`
|
ID int64 `json:"id_str,string"`
|
||||||
ListedCount int `json:"listed_count"`
|
ListedCount int `json:"listed_count"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Location string `json:"location"`
|
Location string `json:"location"`
|
||||||
@ -124,14 +124,14 @@ type APIUser struct {
|
|||||||
type UserResponse struct {
|
type UserResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
User struct {
|
User struct {
|
||||||
ID string `json:"rest_id"`
|
ID int64 `json:"rest_id,string"`
|
||||||
Legacy APIUser `json:"legacy"`
|
Legacy APIUser `json:"legacy"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
func (u UserResponse) ConvertToAPIUser() APIUser {
|
func (u UserResponse) ConvertToAPIUser() APIUser {
|
||||||
ret := u.Data.User.Legacy
|
ret := u.Data.User.Legacy
|
||||||
ret.IDStr = u.Data.User.ID
|
ret.ID = u.Data.User.ID
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,8 +52,8 @@ func TestUserProfileToAPIUser(t *testing.T) {
|
|||||||
|
|
||||||
result := user_resp.ConvertToAPIUser()
|
result := user_resp.ConvertToAPIUser()
|
||||||
|
|
||||||
if result.IDStr != "44067298" {
|
if result.ID != 44067298 {
|
||||||
t.Errorf("Expected IDStr %q, got %q", "44067298", result.IDStr)
|
t.Errorf("Expected ID %q, got %q", 44067298, result.ID)
|
||||||
}
|
}
|
||||||
if result.FollowersCount != user_resp.Data.User.Legacy.FollowersCount {
|
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)
|
t.Errorf("Expected user count %d, got %d", user_resp.Data.User.Legacy.FollowersCount, result.FollowersCount)
|
||||||
|
@ -16,7 +16,7 @@ type Retweet struct {
|
|||||||
func ParseSingleRetweet(apiTweet APITweet) (ret Retweet, err error) {
|
func ParseSingleRetweet(apiTweet APITweet) (ret Retweet, err error) {
|
||||||
ret.RetweetID = TweetID(apiTweet.ID)
|
ret.RetweetID = TweetID(apiTweet.ID)
|
||||||
ret.TweetID = TweetID(apiTweet.RetweetedStatusIDStr)
|
ret.TweetID = TweetID(apiTweet.RetweetedStatusIDStr)
|
||||||
ret.RetweetedByID = UserID(apiTweet.UserIDStr)
|
ret.RetweetedByID = UserID(apiTweet.UserID)
|
||||||
ret.RetweetedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt)
|
ret.RetweetedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,12 @@ func TestParseSingleRetweet(t *testing.T) {
|
|||||||
if retweet.TweetID != "1404269989646028804" {
|
if retweet.TweetID != "1404269989646028804" {
|
||||||
t.Errorf("Expected %q, got %q", "1404269989646028804", retweet.TweetID)
|
t.Errorf("Expected %q, got %q", "1404269989646028804", retweet.TweetID)
|
||||||
}
|
}
|
||||||
if retweet.RetweetedByID != "44067298" {
|
expected_id = 44067298
|
||||||
t.Errorf("Expected %q, got %q", "44067298", retweet.RetweetedBy)
|
if retweet.RetweetedByID != scraper.UserID(expected_id) {
|
||||||
|
t.Errorf("Expected %d, got %d", expected_id, retweet.RetweetedByID)
|
||||||
}
|
}
|
||||||
if retweet.RetweetedAt.Unix() != 1623639042 {
|
expected_id = 1623639042
|
||||||
t.Errorf("Expected %d, got %d", 1623639042, retweet.RetweetedAt.Unix())
|
if retweet.RetweetedAt.Unix() != int64(expected_id) {
|
||||||
|
t.Errorf("Expected %d, got %d", expected_id, retweet.RetweetedAt.Unix())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ func ParseSingleTweet(apiTweet APITweet) (ret Tweet, err error) {
|
|||||||
apiTweet.NormalizeContent()
|
apiTweet.NormalizeContent()
|
||||||
|
|
||||||
ret.ID = TweetID(apiTweet.ID)
|
ret.ID = TweetID(apiTweet.ID)
|
||||||
ret.UserID = UserID(apiTweet.UserIDStr)
|
ret.UserID = UserID(apiTweet.UserID)
|
||||||
ret.Text = apiTweet.FullText
|
ret.Text = apiTweet.FullText
|
||||||
|
|
||||||
ret.PostedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt)
|
ret.PostedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package scraper_test
|
package scraper_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"offline_twitter/terminal_utils"
|
"offline_twitter/terminal_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserID string
|
type UserID int64
|
||||||
type UserHandle string
|
type UserHandle string
|
||||||
|
|
||||||
func JoinArrayOfHandles(handles []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
|
// Turn an APIUser, as returned from the scraper, into a properly structured User object
|
||||||
func ParseSingleUser(apiUser APIUser) (ret User, err error) {
|
func ParseSingleUser(apiUser APIUser) (ret User, err error) {
|
||||||
ret.ID = UserID(apiUser.IDStr)
|
ret.ID = UserID(apiUser.ID)
|
||||||
ret.DisplayName = apiUser.Name
|
ret.DisplayName = apiUser.Name
|
||||||
ret.Handle = UserHandle(apiUser.ScreenName)
|
ret.Handle = UserHandle(apiUser.ScreenName)
|
||||||
ret.Bio = apiUser.Description
|
ret.Bio = apiUser.Description
|
||||||
|
@ -25,8 +25,9 @@ func TestParseSingleUser(t *testing.T) {
|
|||||||
t.Errorf(err.Error())
|
t.Errorf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.ID != "44067298" {
|
expected_id := 44067298
|
||||||
t.Errorf("Expected %q, got %q", "44067298", user.ID)
|
if user.ID != scraper.UserID(expected_id) {
|
||||||
|
t.Errorf("Expected %q, got %q", expected_id, user.ID)
|
||||||
}
|
}
|
||||||
if user.DisplayName != "Michael Malice" {
|
if user.DisplayName != "Michael Malice" {
|
||||||
t.Errorf("Expected %q, got %q", "Michael Malice", user.DisplayName)
|
t.Errorf("Expected %q, got %q", "Michael Malice", user.DisplayName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user