Support for threaded tweets

Close #84
This commit is contained in:
Alexander Sheiko 2023-06-05 13:49:56 +03:00
parent 0f96589c74
commit d9d8ee67b6
3 changed files with 83 additions and 42 deletions

View file

@ -54,6 +54,7 @@ type entry struct {
Items []struct {
Item struct {
ItemContent struct {
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
@ -61,6 +62,7 @@ type entry struct {
} `json:"item"`
} `json:"items"`
ItemContent struct {
TweetDisplayType string `json:"tweetDisplayType"`
TweetResults struct {
Result result `json:"result"`
} `json:"tweet_results"`
@ -124,12 +126,18 @@ func (conversation *threadedConversation) parse() []*Tweet {
for _, entry := range instruction.Entries {
if entry.Content.ItemContent.TweetResults.Result.Typename == "Tweet" {
if tweet := entry.Content.ItemContent.TweetResults.Result.parse(); tweet != nil {
if entry.Content.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
for _, item := range entry.Content.Items {
if item.Item.ItemContent.TweetResults.Result.Typename == "Tweet" {
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
if item.Item.ItemContent.TweetDisplayType == "SelfThread" {
tweet.IsSelfThread = true
}
tweets = append(tweets, tweet)
}
}
@ -145,6 +153,16 @@ func (conversation *threadedConversation) parse() []*Tweet {
}
}
}
if tweet.IsSelfThread && tweet.ConversationID == tweet.ID {
for _, childTweet := range tweets {
if childTweet.IsSelfThread && childTweet.ID != tweet.ID {
tweet.Thread = append(tweet.Thread, childTweet)
}
}
if len(tweet.Thread) == 0 {
tweet.IsSelfThread = false
}
}
}
return tweets
}
@ -154,6 +172,7 @@ func parseLegacyTweet(user *legacyUser, tweet *legacyTweet) *Tweet {
name := user.Name
tweetID := tweet.IDStr
tw := &Tweet{
ConversationID: tweet.ConversationIDStr,
ID: tweetID,
Likes: tweet.FavoriteCount,
Name: name,

View file

@ -73,6 +73,7 @@ func TestGetTweets(t *testing.T) {
func TestGetTweet(t *testing.T) {
sample := twitterscraper.Tweet{
ConversationID: "1328684389388185600",
HTML: "That thing you didnt Tweet but wanted to but didnt but got so close but then were like nah. <br><br>We have a place for that now—Fleets! <br><br>Rolling out to everyone starting today. <br><a href=\"https://t.co/auQAHXZMfH\"><img src=\"https://pbs.twimg.com/amplify_video_thumb/1328684333599756289/img/cP5KwbIXbGunNSBy.jpg\"/></a>",
ID: "1328684389388185600",
Name: "Twitter",
@ -119,6 +120,7 @@ func TestTweetMentions(t *testing.T) {
func TestQuotedAndReply(t *testing.T) {
sample := &twitterscraper.Tweet{
ConversationID: "1237110546383724547",
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: 485,
@ -164,6 +166,7 @@ func TestQuotedAndReply(t *testing.T) {
}
func TestRetweet(t *testing.T) {
sample := &twitterscraper.Tweet{
ConversationID: "1359151057872580612",
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.",
ID: "1359151057872580612",
Likes: 6683,
@ -217,3 +220,18 @@ func TestTweetViews(t *testing.T) {
}
}
}
func TestTweetThread(t *testing.T) {
scraper := twitterscraper.New()
tweet, err := scraper.GetTweet("1665602315745673217")
if err != nil {
t.Fatal(err)
} else {
if !tweet.IsSelfThread {
t.Error("IsSelfThread must be True")
}
if len(tweet.Thread) != 7 {
t.Error("Thread length must be 7")
}
}
}

View file

@ -25,6 +25,7 @@ type (
// Tweet type.
Tweet struct {
ConversationID string
Hashtags []string
HTML string
ID string
@ -34,6 +35,7 @@ type (
IsPin bool
IsReply bool
IsRetweet bool
IsSelfThread bool
Likes int
Name string
Mentions []Mention
@ -47,6 +49,7 @@ type (
RetweetedStatus *Tweet
RetweetedStatusID string
Text string
Thread []*Tweet
TimeParsed time.Time
Timestamp int64
URLs []string
@ -70,6 +73,7 @@ type (
}
legacyTweet struct {
ConversationIDStr string `json:"conversation_id_str"`
CreatedAt string `json:"created_at"`
FavoriteCount int `json:"favorite_count"`
FullText string `json:"full_text"`