Skip to content

Commit

Permalink
Merge pull request #7 from krishh-konar/patch
Browse files Browse the repository at this point in the history
Added media type selection.
  • Loading branch information
krishh-konar authored Oct 23, 2022
2 parents 3d1e351 + 72bc707 commit d2e90e0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions twitter_image_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
import wget
import sys

MEDIA_FORMATS = {
# https://help.twitter.com/en/using-twitter/tweeting-gifs-and-pictures
"images": [ "jpg", "jpeg", "png", "webp", "heic" ],
"videos": [ "mp4", "m4v", "mov" ],
"gifs": [ "gif" ]
}

def main():
#Authentication
api = authenticate()
Expand All @@ -21,9 +28,10 @@ def main():

username = input("\nEnter the twitter handle of the Account to download media from: ")
max_tweets = int(input("Enter Max. number of tweets to search (default: 1000): ") or 1000)
media_formats = input("Enter type of media (images/gifs/videos/all) (default: images) ") or "images"

all_tweets = getTweetsFromUser(api, username, max_tweets)
media_URLs = getTweetMediaURL(all_tweets)
media_URLs = getTweetMediaURL(all_tweets, media_formats)

downloadFiles(media_URLs,username)
print('\n\nFinished Downloading.\n')
Expand Down Expand Up @@ -63,7 +71,7 @@ def getTweetsFromUser(api, username, max_tweets=1000):
print ('\nFinished fetching ' + str(min(len(raw_tweets),max_tweets)) + ' Tweets.')
return raw_tweets

def getTweetMediaURL(all_tweets):
def getTweetMediaURL(all_tweets, media_formats = "images"):
'''
Fetches the media URLs from downloaded tweets.
'''
Expand All @@ -74,12 +82,17 @@ def getTweetMediaURL(all_tweets):
for tweet in all_tweets:
media = tweet.entities.get('media',[])
if len(media) > 0:
tweets_with_media.add(media[0]['media_url'])
# print(media)
if media_formats == "all":
tweets_with_media.add(media[0]['media_url_https'])
else:
if media[0]['media_url_https'].split(".")[-1] in MEDIA_FORMATS[media_formats]:
tweets_with_media.add(media[0]['media_url_https'])

sys.stdout.write("\rMedia Links fetched: %d" % len(tweets_with_media))
sys.stdout.flush()

print ('\nFinished fetching ' + str(len(tweets_with_media)) + ' links.')

return tweets_with_media

def downloadFiles(media_url, username):
Expand Down

0 comments on commit d2e90e0

Please sign in to comment.