From 0adefb32f399666017e0fb51263e57634b95e17b Mon Sep 17 00:00:00 2001 From: windowsdeveloperwannabe <47078223+windowsdeveloperwannabe@users.noreply.github.com> Date: Wed, 10 May 2023 05:02:21 -0700 Subject: [PATCH] add user mentions --- timeline.go | 13 +++++++++++++ tweets_test.go | 18 ++++++++++++++++++ types.go | 8 ++++++++ 3 files changed, 39 insertions(+) diff --git a/timeline.go b/timeline.go index 26c7efe..cb6b46d 100644 --- a/timeline.go +++ b/timeline.go @@ -27,6 +27,11 @@ type timeline struct { ExpandedURL string `json:"expanded_url"` URL string `json:"url"` } `json:"urls"` + UserMentions []struct { + IDStr string `json:"id_str"` + Name string `json:"name"` + ScreenName string `json:"screen_name"` + } `json:"user_mentions"` } `json:"entities"` ExtendedEntities struct { Media []struct { @@ -200,6 +205,14 @@ func (timeline *timeline) parseTweet(id string) *Tweet { tw.Hashtags = append(tw.Hashtags, hash.Text) } + for _, mention := range tweet.Entities.UserMentions { + tw.Mentions = append(tw.Mentions, Mention{ + ID: mention.IDStr, + Username: mention.ScreenName, + Name: mention.Name, + }) + } + for _, media := range tweet.ExtendedEntities.Media { if media.Type == "photo" { photo := Photo{ diff --git a/tweets_test.go b/tweets_test.go index b65302c..f088b7c 100644 --- a/tweets_test.go +++ b/tweets_test.go @@ -99,6 +99,24 @@ func TestGetTweet(t *testing.T) { } } +func TestTweetMentions(t *testing.T) { + sample := []twitterscraper.Mention{{ + ID: "7018222", + Username: "davidmcraney", + Name: "David McRaney", + }} + scraper := twitterscraper.New() + tweet, err := scraper.GetTweet("1554522888904101890") + println(tweet.Mentions[0].Username) + if err != nil { + t.Error(err) + } else { + if diff := cmp.Diff(sample, tweet.Mentions, cmpOptions...); diff != "" { + t.Error("Resulting tweet does not match the sample", diff) + } + } +} + func TestQuotedAndReply(t *testing.T) { sample := &twitterscraper.Tweet{ HTML: "The Easiest Problem Everyone Gets Wrong

[new video] --> https://t.co/YdaeDYmPAU
", diff --git a/types.go b/types.go index eef4566..36c6955 100644 --- a/types.go +++ b/types.go @@ -3,6 +3,13 @@ package twitterscraper import "time" type ( + // Mention type. + Mention struct { + ID string + Username string + Name string + } + // Photo type. Photo struct { ID string @@ -27,6 +34,7 @@ type ( IsReply bool IsRetweet bool Likes int + Mentions []Mention PermanentURL string Photos []Photo Place *Place