Add retweet info

Close #29
This commit is contained in:
Alexander Sheiko 2021-03-28 12:25:55 +03:00
parent 502d58953e
commit 0533b6e909
3 changed files with 40 additions and 0 deletions

View file

@ -94,3 +94,21 @@ func TestGetTweet(t *testing.T) {
} }
} }
} }
func TestRetweet(t *testing.T) {
sample := Retweet{
ID: "1359151057872580612",
TimeParsed: time.Date(2021, 02, 9, 14, 43, 58, 0, time.FixedZone("UTC", 0)),
Timestamp: 1612881838,
UserID: "773578328498372608",
Username: "TwitterTogether",
}
tweet, err := defaultScraper.GetTweet("1362849141248974853")
if err != nil {
t.Error(err)
} else {
if diff := cmp.Diff(sample, tweet.Retweet); diff != "" {
t.Error("Resulting retweet does not match the sample", diff)
}
}
}

View file

@ -10,6 +10,15 @@ type (
URL string URL string
} }
// Retweet type
Retweet struct {
ID string
TimeParsed time.Time
Timestamp int64
UserID string
Username string
}
// Tweet type. // Tweet type.
Tweet struct { Tweet struct {
Hashtags []string Hashtags []string
@ -24,6 +33,7 @@ type (
Photos []string Photos []string
Replies int Replies int
Retweets int Retweets int
Retweet Retweet
Text string Text string
TimeParsed time.Time TimeParsed time.Time
Timestamp int64 Timestamp int64

12
util.go
View file

@ -129,6 +129,18 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
} }
if tweet.RetweetedStatusIDStr != "" { if tweet.RetweetedStatusIDStr != "" {
tw.IsRetweet = true tw.IsRetweet = true
if retweet, ok := timeline.GlobalObjects.Tweets[tweet.RetweetedStatusIDStr]; ok {
tw.Retweet = Retweet{
ID: tweet.RetweetedStatusIDStr,
UserID: retweet.UserIDStr,
Username: timeline.GlobalObjects.Users[retweet.UserIDStr].ScreenName,
}
tm, err := time.Parse(time.RubyDate, retweet.CreatedAt)
if err == nil {
tw.Retweet.TimeParsed = tm
tw.Retweet.Timestamp = tm.Unix()
}
}
} }
for _, pinned := range timeline.GlobalObjects.Users[tweet.UserIDStr].PinnedTweetIdsStr { for _, pinned := range timeline.GlobalObjects.Users[tweet.UserIDStr].PinnedTweetIdsStr {