Add InReplyToStatus, QuotedStatus and RetweetedStatus parsing in Tweet

BREAKING CHANGE: Retweet property renamed to RetweetedStatus
Closed #41
This commit is contained in:
Alexander Sheiko 2021-07-16 12:21:41 +03:00
parent c370a9078f
commit be2c3ec5b1
3 changed files with 60 additions and 22 deletions

View file

@ -167,13 +167,15 @@ func (timeline *timeline) parseTweet(id string) *Tweet {
if tweet.QuotedStatusIDStr != "" { if tweet.QuotedStatusIDStr != "" {
tw.IsQuoted = true tw.IsQuoted = true
tw.QuotedStatus = timeline.parseTweet(tweet.QuotedStatusIDStr)
} }
if tweet.InReplyToStatusIDStr != "" { if tweet.InReplyToStatusIDStr != "" {
tw.IsReply = true tw.IsReply = true
tw.InReplyToStatus = timeline.parseTweet(tweet.InReplyToStatusIDStr)
} }
if tweet.RetweetedStatusIDStr != "" { if tweet.RetweetedStatusIDStr != "" {
tw.IsRetweet = true tw.IsRetweet = true
tw.Retweet = timeline.parseTweet(tweet.RetweetedStatusIDStr) tw.RetweetedStatus = timeline.parseTweet(tweet.RetweetedStatusIDStr)
} }
for _, pinned := range timeline.GlobalObjects.Users[tweet.UserIDStr].PinnedTweetIdsStr { for _, pinned := range timeline.GlobalObjects.Users[tweet.UserIDStr].PinnedTweetIdsStr {

View file

@ -95,6 +95,40 @@ func TestGetTweet(t *testing.T) {
} }
} }
func TestQuotedAndReply(t *testing.T) {
sample := &Tweet{
HTML: "The Easiest Problem Everyone Gets Wrong <br><br>[new video] --&gt; <a href=\"https://youtu.be/ytfCdqWhmdg\">https://t.co/YdaeDYmPAU</a> <br><a href=\"https://t.co/iKu4Xs6o2V\"><img src=\"https://pbs.twimg.com/media/ESsZa9AXgAIAYnF.jpg\"/></a>",
ID: "1237110546383724547",
Likes: 484,
PermanentURL: "https://twitter.com/VsauceTwo/status/1237110546383724547",
Photos: []string{"https://pbs.twimg.com/media/ESsZa9AXgAIAYnF.jpg"},
Replies: 12,
Retweets: 18,
Text: "The Easiest Problem Everyone Gets Wrong \n\n[new video] --&gt; https://t.co/YdaeDYmPAU https://t.co/iKu4Xs6o2V",
TimeParsed: time.Date(2020, 03, 9, 20, 18, 33, 0, time.FixedZone("UTC", 0)),
Timestamp: 1583785113,
URLs: []string{"https://youtu.be/ytfCdqWhmdg"},
UserID: "978944851",
Username: "VsauceTwo",
}
tweet, err := defaultScraper.GetTweet("1237110897597976576")
if err != nil {
t.Error(err)
} else {
if diff := cmp.Diff(sample, tweet.QuotedStatus); diff != "" {
t.Error("Resulting quote does not match the sample", diff)
}
}
tweet, err = defaultScraper.GetTweet("1237111868445134850")
if err != nil {
t.Error(err)
} else {
if diff := cmp.Diff(sample, tweet.InReplyToStatus); diff != "" {
t.Error("Resulting reply does not match the sample", diff)
}
}
}
func TestRetweet(t *testing.T) { func TestRetweet(t *testing.T) {
sample := &Tweet{ sample := &Tweet{
HTML: "Weve seen an increase in attacks against Asian communities and individuals around the world. Its important to know that this isnt new; throughout history, Asians have experienced violence and exclusion. However, their diverse lived experiences have largely been overlooked.", HTML: "Weve seen an increase in attacks against Asian communities and individuals around the world. Its important to know that this isnt new; throughout history, Asians have experienced violence and exclusion. However, their diverse lived experiences have largely been overlooked.",
@ -113,7 +147,7 @@ func TestRetweet(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
if diff := cmp.Diff(sample, tweet.Retweet); diff != "" { if diff := cmp.Diff(sample, tweet.RetweetedStatus); diff != "" {
t.Error("Resulting retweet does not match the sample", diff) t.Error("Resulting retweet does not match the sample", diff)
} }
} }

View file

@ -21,26 +21,28 @@ type (
// Tweet type. // Tweet type.
Tweet struct { Tweet struct {
Hashtags []string Hashtags []string
HTML string HTML string
ID string ID string
IsQuoted bool InReplyToStatus *Tweet
IsPin bool IsQuoted bool
IsReply bool IsPin bool
IsRetweet bool IsReply bool
Likes int IsRetweet bool
PermanentURL string Likes int
Photos []string PermanentURL string
Replies int Photos []string
Retweets int QuotedStatus *Tweet
Retweet *Tweet Replies int
Text string Retweets int
TimeParsed time.Time RetweetedStatus *Tweet
Timestamp int64 Text string
URLs []string TimeParsed time.Time
UserID string Timestamp int64
Username string URLs []string
Videos []Video UserID string
Username string
Videos []Video
} }
// ProfileResult of scrapping. // ProfileResult of scrapping.