Skip to content

Commit

Permalink
Updated code to incorporate Auth Token changes in Twitter API
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishanu Konar committed Oct 21, 2022
1 parent f8056dd commit e503e90
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 111 deletions.
14 changes: 8 additions & 6 deletions API_Tokens.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'''
Contains necessary tokens to use twitter's API.
Contains necessary tokens to use twitter's API.
'''

## all the 4 required Tokens
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
## This script can run with just the BEARER_TOKEN,
## other keys can be left blank.

BEARER_TOKEN = ""

API_KEY = ""
API_KEY_SECRET = ""
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Twitter Image Downloader
Downloads images/photos uploaded on twitter by a given user.
Downloads images/photos uploaded on twitter from a given twitter handle.

### Dependencies

Expand All @@ -9,19 +9,20 @@ You can install requirements using `requirements.txt`.

`pip install -r requirements.txt`

### Authorization Tokens

You will also need to create an app account on https://dev.twitter.com/apps to get authorization tokens

1. Sign in with your Twitter account
2. Create a new app account
2. Create a new app.
3. Fill necessary details
4. Generate a new OAuth token with those permissions

Following these steps will create 4 tokens that you will need to authenticate your account.
4. Generate OAuth tokens for the app.

### How to Use
Edit the `API_Tokens.py` file and add all the 4 tokens you got in the previous step and save.
Run the script by `python twitter_image_downloader.py`
Following these steps and a successful app creation, you will recieve 3 tokens for your app, namely `API_KEY`, `API_KEY_SECRET` and `BEARER_TOKEN`.
### Usage

Enter the user's twitter handle `(@twitter_handle)` you want to download images from, followed by max. number of tweets you want to search for (0 for all/max. allowed twwets by Twitter's API).
The images are downloaded inside a folder named `twitter_images`, in the folder named `user_handle`.
1. Edit the `API_Tokens.py` file and add these tokens you recieved in the previous step and save.
2. Run the script (`python twitter_image_downloader.py`).
3. Enter the user's twitter handle `(@twitter_handle)` you want to download images from, followed by max. number of tweets you want to search for.
4. The images are downloaded inside a folder named `twitter_images`, in the folder named `<user_handle>`.

13 changes: 9 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Use the following command before running the tool to install all related dependencies
# `pip install -r requirements.txt`
tweepy
wget
certifi==2022.9.24
charset-normalizer==2.1.1
idna==3.4
oauthlib==3.2.2
requests==2.28.1
requests-oauthlib==1.3.1
tweepy==4.10.1
urllib3==1.26.12
wget==3.2
187 changes: 96 additions & 91 deletions twitter_image_downloader.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,116 @@
#!/usr/bin/python

'''
A script that downloads all the pictures posted by a given user.
A script that downloads all the pictures posted by a given twitter handle.
Author: Krishanu Konar
email: krishh_konar
email: [email protected]
'''

import API_Tokens as t
import json
from tweepy import OAuthHandler, API, Stream
import API_Tokens as tokens
from tweepy import OAuth2BearerHandler, API
import os
import wget
import sys
import requests


def main():
#Authentication
api = authenticate()
print ('\n\nTwitter Image Downloader:\n========================\n')
username = input("\nEnter the twitter handle of the Account to download media from: ")
max_tweets = int(input("\nEnter Max. number of tweets to search (0 for all tweets): "))

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

downloadFiles(media_URLs,username)
print ('\n\nFinished Downloading.\n')

def getTweetsFromUser(username,max_tweets,api):
## Fetches Tweets from user with the handle 'username' upto max of 'max_tweets' tweets
last_tweet_id, num_images = 0,0
try:
raw_tweets = api.user_timeline(screen_name=username,include_rts=False,exclude_replies=True)
except Exception as e:
print (e)
sys.exit()

last_tweet_id = int(raw_tweets[-1].id-1)

print ('\nFetching tweets.....')

if max_tweets == 0:
max_tweets = 3500

while len(raw_tweets)<max_tweets:
sys.stdout.write("\rTweets fetched: %d" % len(raw_tweets))
sys.stdout.flush()
temp_raw_tweets = api.user_timeline(screen_name=username, max_id=last_tweet_id, include_rts=False, exclude_replies=True)

if len(temp_raw_tweets) == 0:
break
else:
last_tweet_id = int(temp_raw_tweets[-1].id-1)
raw_tweets = raw_tweets + temp_raw_tweets

print ('\nFinished fetching ' + str(min(len(raw_tweets),max_tweets)) + ' Tweets.')
return raw_tweets
#Authentication
api = authenticate()

print ('\nTwitter Image Downloader:\n========================\n')

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)

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

downloadFiles(media_URLs,username)
print('\n\nFinished Downloading.\n')

def getTweetsFromUser(api, username, max_tweets=1000):
'''
Fetches Tweets from user with the handle 'username'
upto max of 'max_tweets' tweets.
'''

last_tweet_id = 0

try:
raw_tweets = api.user_timeline(screen_name=username, include_rts=False, exclude_replies=True)
except Exception as e:
print (e)
sys.exit(-1)

last_tweet_id = int(raw_tweets[-1].id - 1)

print ('\nFetching tweets.....')

while len(raw_tweets) < max_tweets:
sys.stdout.write("\rTweets fetched: %d" % len(raw_tweets))
sys.stdout.flush()

tweets = api.user_timeline(screen_name=username, max_id=last_tweet_id, \
include_rts=False, exclude_replies=True)

if len(tweets) == 0:
break

else:
last_tweet_id = int(tweets[-1].id - 1)
raw_tweets = raw_tweets + tweets

print ('\nFinished fetching ' + str(min(len(raw_tweets),max_tweets)) + ' Tweets.')
return raw_tweets

def getTweetMediaURL(all_tweets):
print('\nCollecting Media URLs.....')
tweets_with_media = set()
for tweet in all_tweets:
media = tweet.entities.get('media',[])
if (len(media)>0):
tweets_with_media.add(media[0]['media_url'])
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):
# response = requests.get(url)

# if response.status_code == 200:
# filename = url[0:-18]
# path = "/home/krishh/twitter_media/"+str(counter) + '.jpg'
# f = open(path,'wb')
# f.write(response.content)
# f.close()
print ('\nDownloading Images.....')
try:
os.mkdir('twitter_images')
os.chdir('twitter_images')
except:
os.chdir('twitter_images')

try:
os.mkdir(username)
os.chdir(username)
except:
os.chdir(username)

for url in media_url:
wget.download(url)
'''
Fetches the media URLs from downloaded tweets.
'''

print('\nCollecting Media URLs.....')
tweets_with_media = set()

for tweet in all_tweets:
media = tweet.entities.get('media',[])
if len(media) > 0:
tweets_with_media.add(media[0]['media_url'])
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):
'''
Downloads the fetched media URLs.
'''

print ('\nDownloading Images.....')

try:
os.mkdir('twitter_images')
os.chdir('twitter_images')
except:
os.chdir('twitter_images')

try:
os.mkdir(username)
os.chdir(username)
except:
os.chdir(username)

for url in media_url:
wget.download(url)


def authenticate():
''' Authenticate the use of twitter API '''
auth = OAuthHandler(t.CONSUMER_KEY, t.CONSUMER_SECRET)
auth.set_access_token(t.ACCESS_TOKEN,t.ACCESS_TOKEN_SECRET)
api = API(auth)
return api
''' Authenticate the use of twitter API '''
auth = OAuth2BearerHandler(tokens.BEARER_TOKEN)
api = API(auth)
return api


if __name__ == '__main__':
main()
main()

0 comments on commit e503e90

Please sign in to comment.