From 048fce5cf21fd6340118b68cb7ddf114fcec5ac2 Mon Sep 17 00:00:00 2001 From: Regynald Augustin Date: Thu, 18 May 2023 15:14:28 -0400 Subject: [PATCH 1/3] Add views to tweet type --- types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/types.go b/types.go index c09a528..2d5aeff 100644 --- a/types.go +++ b/types.go @@ -50,6 +50,7 @@ type ( UserID string Username string Videos []Video + Views int SensitiveContent bool } From b458d2bd82b0564f2a22ffef44bf96765941ec27 Mon Sep 17 00:00:00 2001 From: Regynald Augustin Date: Thu, 18 May 2023 15:16:49 -0400 Subject: [PATCH 2/3] Add views to timeline and parse --- timeline.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/timeline.go b/timeline.go index a882a8d..b9f4bc6 100644 --- a/timeline.go +++ b/timeline.go @@ -2,6 +2,7 @@ package twitterscraper import ( "fmt" + "strconv" "strings" "time" ) @@ -60,6 +61,10 @@ type timeline struct { QuotedStatusIDStr string `json:"quoted_status_id_str"` Time time.Time `json:"time"` UserIDStr string `json:"user_id_str"` + Views struct { + State string `json:"state"` + Count string `json:"count"` + } `json:"ext_views"` } `json:"tweets"` Users map[string]struct { CreatedAt string `json:"created_at"` @@ -196,6 +201,14 @@ func (timeline *timeline) parseTweet(id string) *Tweet { tw.RetweetedStatus = timeline.parseTweet(tweet.RetweetedStatusIDStr) } + if tweet.Views.Count != "" { + views, viewsErr := strconv.Atoi(tweet.Views.Count) + if viewsErr != nil { + views = 0 + } + tw.Views = views + } + for _, pinned := range timeline.GlobalObjects.Users[tweet.UserIDStr].PinnedTweetIdsStr { if tweet.ConversationIDStr == pinned { tw.IsPin = true From 45d30f73049fc4e0454e063d21261639e2e79911 Mon Sep 17 00:00:00 2001 From: Regynald Augustin Date: Thu, 18 May 2023 15:51:03 -0400 Subject: [PATCH 3/3] Add test --- tweets_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tweets_test.go b/tweets_test.go index c416176..b548fda 100644 --- a/tweets_test.go +++ b/tweets_test.go @@ -190,3 +190,30 @@ func TestRetweet(t *testing.T) { } } } + +func TestTweetViews(t *testing.T) { + sample := &twitterscraper.Tweet{ + HTML: "Replies and likes don’t tell the whole story. We’re making it easier to tell *just* how many people have seen your Tweets with the addition of view counts, shown right next to likes. Now on iOS and Android, web coming soon.", + ID: "1606055187348688896", + Likes: 2839, + Name: "Twitter Support", + PermanentURL: "https://twitter.com/TwitterSupport/status/1606055187348688896", + Replies: 3427, + Retweets: 783, + Text: "Replies and likes don’t tell the whole story. We’re making it easier to tell *just* how many people have seen your Tweets with the addition of view counts, shown right next to likes. Now on iOS and Android, web coming soon.", + TimeParsed: time.Date(2022, 12, 22, 22, 32, 50, 0, time.FixedZone("UTC", 0)), + Timestamp: 1612881838, + UserID: "17874544", + Username: "TwitterSupport", + Views: 3189278, + } + scraper := twitterscraper.New() + tweet, err := scraper.GetTweet("1606055187348688896") + if err != nil { + t.Error(err) + } else { + if tweet.Views < sample.Views { + t.Error("Views must be greater than or equal to the sample") + } + } +}