From 6a929cde9c620bb95575c5ca74eb1faf53cafaaf Mon Sep 17 00:00:00 2001
From: windowsdeveloperwannabe
<47078223+windowsdeveloperwannabe@users.noreply.github.com>
Date: Wed, 10 May 2023 03:18:34 -0700
Subject: [PATCH] save photo ids
---
timeline.go | 18 +++++++++++++++---
tweets_test.go | 23 +++++++++++++----------
types.go | 8 +++++++-
3 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/timeline.go b/timeline.go
index 0f622e9..26c7efe 100644
--- a/timeline.go
+++ b/timeline.go
@@ -202,7 +202,12 @@ func (timeline *timeline) parseTweet(id string) *Tweet {
for _, media := range tweet.ExtendedEntities.Media {
if media.Type == "photo" {
- tw.Photos = append(tw.Photos, media.MediaURLHttps)
+ photo := Photo{
+ ID: media.IDStr,
+ URL: media.MediaURLHttps,
+ }
+
+ tw.Photos = append(tw.Photos, photo)
} else if media.Type == "video" {
video := Video{
ID: media.IDStr,
@@ -217,7 +222,6 @@ func (timeline *timeline) parseTweet(id string) *Tweet {
}
}
- tw.Photos = append(tw.Photos, video.Preview)
tw.Videos = append(tw.Videos, video)
}
@@ -259,7 +263,15 @@ func (timeline *timeline) parseTweet(id string) *Tweet {
}
return tco
})
- for _, url := range tw.Photos {
+ for _, photo := range tw.Photos {
+ url := photo.URL
+ if stringInSlice(url, foundedMedia) {
+ continue
+ }
+ tw.HTML += fmt.Sprintf(`
`, url)
+ }
+ for _, video := range tw.Videos {
+ url := video.Preview
if stringInSlice(url, foundedMedia) {
continue
}
diff --git a/tweets_test.go b/tweets_test.go
index 7856ade..b65302c 100644
--- a/tweets_test.go
+++ b/tweets_test.go
@@ -76,7 +76,7 @@ func TestGetTweet(t *testing.T) {
HTML: "That thing you didn’t Tweet but wanted to but didn’t but got so close but then were like nah.
We have a place for that now—Fleets!
Rolling out to everyone starting today.
",
ID: "1328684389388185600",
PermanentURL: "https://twitter.com/Twitter/status/1328684389388185600",
- Photos: []string{"https://pbs.twimg.com/amplify_video_thumb/1328684333599756289/img/cP5KwbIXbGunNSBy.jpg"},
+ Photos: nil,
Text: "That thing you didn’t Tweet but wanted to but didn’t but got so close but then were like nah. \n\nWe have a place for that now—Fleets! \n\nRolling out to everyone starting today. https://t.co/auQAHXZMfH",
TimeParsed: time.Date(2020, 11, 17, 13, 0, 18, 0, time.FixedZone("UTC", 0)),
Timestamp: 1605618018,
@@ -105,15 +105,18 @@ func TestQuotedAndReply(t *testing.T) {
ID: "1237110546383724547",
Likes: 485,
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] --> 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",
+ Photos: []twitterscraper.Photo{{
+ ID: "1237110473486729218",
+ URL: "https://pbs.twimg.com/media/ESsZa9AXgAIAYnF.jpg",
+ }},
+ Replies: 12,
+ Retweets: 18,
+ Text: "The Easiest Problem Everyone Gets Wrong \n\n[new video] --> https://t.co/YdaeDYmPAU https://t.co/iKu4Xs6o2V",
+ TimeParsed: time.Date(2020, 0o3, 9, 20, 18, 33, 0, time.FixedZone("UTC", 0)),
+ Timestamp: 1583785113,
+ URLs: []string{"https://youtu.be/ytfCdqWhmdg"},
+ UserID: "978944851",
+ Username: "VsauceTwo",
}
scraper := twitterscraper.New()
tweet, err := scraper.GetTweet("1237110897597976576")
diff --git a/types.go b/types.go
index 9c5b0fe..eef4566 100644
--- a/types.go
+++ b/types.go
@@ -3,6 +3,12 @@ package twitterscraper
import "time"
type (
+ // Photo type.
+ Photo struct {
+ ID string
+ URL string
+ }
+
// Video type.
Video struct {
ID string
@@ -22,7 +28,7 @@ type (
IsRetweet bool
Likes int
PermanentURL string
- Photos []string
+ Photos []Photo
Place *Place
QuotedStatus *Tweet
Replies int