From d386ba2cb45bb4977728c5c18906581870256dda Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 24 Jul 2021 10:55:44 -0700 Subject: [PATCH] Make String() methods more useful for terminal output --- scraper/tweet.go | 41 +++++++++++++++++++++++++++++++++++++---- scraper/user.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/scraper/tweet.go b/scraper/tweet.go index 3964e68..c600456 100644 --- a/scraper/tweet.go +++ b/scraper/tweet.go @@ -3,6 +3,8 @@ package scraper import ( "time" "fmt" + + "offline_twitter/terminal_utils" ) const DEFAULT_MAX_REPLIES_EAGER_LOAD = 50 @@ -29,11 +31,42 @@ type Tweet struct { QuotedTweet TweetID } + func (t Tweet) String() string { - return fmt.Sprintf( -`ID %s, User %s: %q (%s). Likes: %d, Retweets: %d, QTs: %d, Replies: %d. -Urls: %v Images: %v Mentions: %v Hashtags: %v`, - t.ID, t.UserID, t.Text, t.PostedAt, t.NumLikes, t.NumRetweets, t.NumQuoteTweets, t.NumReplies, t.Urls, t.Images, t.Mentions, t.Hashtags) + var author string + if t.User != nil { + author = fmt.Sprintf("%s\n@%s", t.User.DisplayName, t.User.Handle) + } else { + author = "@???" + } + + ret := fmt.Sprintf( +`%s +%s +%s +Replies: %d RT: %d QT: %d Likes: %d +`, + author, + terminal_utils.FormatDate(t.PostedAt), + terminal_utils.WrapText(t.Text, 60), + t.NumReplies, + t.NumRetweets, + t.NumQuoteTweets, + t.NumLikes, + ) + + if len(t.Images) > 0 { + ret += fmt.Sprintf(terminal_utils.COLOR_GREEN + "images: %d\n" + terminal_utils.COLOR_RESET, len(t.Images)) + } + if len(t.Urls) > 0 { + ret += "urls: [\n" + for _, url := range(t.Urls) { + ret += " " + url + "\n" + } + ret += "]" + } + + return ret } // Turn an APITweet, as returned from the scraper, into a properly structured Tweet object diff --git a/scraper/user.go b/scraper/user.go index e95bf66..a9fe51a 100644 --- a/scraper/user.go +++ b/scraper/user.go @@ -4,6 +4,8 @@ import ( "time" "fmt" "strings" + + "offline_twitter/terminal_utils" ) type UserID string @@ -36,7 +38,37 @@ type User struct { } func (u User) String() string { - return fmt.Sprintf("%s (@%s)[%s]: %q", u.DisplayName, u.Handle, u.ID, u.Bio) + var verified string + if u.IsVerified { + verified = "[\u2713]" + } + ret := fmt.Sprintf( +`%s%s +@%s + %s + +Following: %d Followers: %d + +Joined %s +%s +%s +`, + u.DisplayName, + verified, + u.Handle, + terminal_utils.WrapText(u.Bio, 60), + u.FollowingCount, + u.FollowersCount, + terminal_utils.FormatDate(u.JoinDate), + u.Location, + u.Website, + ) + if u.PinnedTweet != nil { + ret += "\n" + terminal_utils.WrapText(u.PinnedTweet.Text, 60) + } else { + println("Pinned tweet id:", u.PinnedTweetID) + } + return ret } // Turn an APIUser, as returned from the scraper, into a properly structured User object