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 != "" {
tw.IsQuoted = true
tw.QuotedStatus = timeline.parseTweet(tweet.QuotedStatusIDStr)
}
if tweet.InReplyToStatusIDStr != "" {
tw.IsReply = true
tw.InReplyToStatus = timeline.parseTweet(tweet.InReplyToStatusIDStr)
}
if tweet.RetweetedStatusIDStr != "" {
tw.IsRetweet = true
tw.Retweet = timeline.parseTweet(tweet.RetweetedStatusIDStr)
tw.RetweetedStatus = timeline.parseTweet(tweet.RetweetedStatusIDStr)
}
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) {
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.",
@ -113,7 +147,7 @@ func TestRetweet(t *testing.T) {
if err != nil {
t.Error(err)
} 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)
}
}

View file

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