Skip to content

Commit

Permalink
[twitter] add option to download all media from a conversation
Browse files Browse the repository at this point in the history
(fixes #1319)
  • Loading branch information
mikf committed Feb 26, 2021
1 parent cf5fa75 commit 5d69e43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,17 @@ Description
Fetch media from `Cards <https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/abouts-cards>`__.


extractor.twitter.conversations
-------------------------------
Type
``bool``
Default
``false``
Description
Fetch media from all Tweets and replies in a `conversation
<https://help.twitter.com/en/using-twitter/twitter-conversations>`__.


extractor.twitter.quoted
------------------------
Type
Expand Down
18 changes: 18 additions & 0 deletions gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,20 @@ class TwitterTweetExtractor(TwitterExtractor):
"date" : "dt:2020-08-20 04:00:28",
},
}),
# all Tweets from a conversation (#1319)
("https://twitter.com/BlankArts_/status/1323314488611872769", {
"options": (("conversations", True),),
"count": ">= 50",
}),
)

def __init__(self, match):
TwitterExtractor.__init__(self, match)
self.tweet_id = match.group(2)

def tweets(self):
if self.config("conversations", False):
return TwitterAPI(self).conversation(self.tweet_id)
return TwitterAPI(self).tweet(self.tweet_id)


Expand Down Expand Up @@ -554,6 +561,10 @@ def tweet(self, tweet_id):
break
return tweets

def conversation(self, conversation_id):
endpoint = "/2/timeline/conversation/{}.json".format(conversation_id)
return self._pagination(endpoint)

def timeline_profile(self, screen_name):
user_id = self._user_id_by_screen_name(screen_name)
endpoint = "/2/timeline/profile/{}.json".format(user_id)
Expand Down Expand Up @@ -723,6 +734,13 @@ def _pagination(self, endpoint, params=None):
tweet = True
cursor = cursor["value"]

elif entry_startswith("conversationThread-"):
tweet_ids.extend(
item["entryId"][6:]
for item in entry["content"]["timelineModule"]["items"]
if item["entryId"].startswith("tweet-")
)

# process tweets
for tweet_id in tweet_ids:
try:
Expand Down

0 comments on commit 5d69e43

Please sign in to comment.