Skip to content

Instantly share code, notes, and snippets.

@impshum
Created March 1, 2020 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impshum/bf1fc342b3f46fb440b8e22a352b5a50 to your computer and use it in GitHub Desktop.
Save impshum/bf1fc342b3f46fb440b8e22a352b5a50 to your computer and use it in GitHub Desktop.
[TWITTER]
twitter_consumer_key = XXXX
twitter_consumer_secret = XXXX
twitter_access_token = XXXX
twitter_access_token_secret = XXXX
import tweepy
import configparser
from requests import get
def download_img(img_url):
if img_url.endswith(('.jpg', '.jpeg', '.png', '.gif')):
img_name = img_url.split('/')[-1]
img = get(img_url).content
with open(img_name, 'wb') as f:
f.write(img)
config = configparser.ConfigParser()
config.read('conf.ini')
twitter_consumer_key = config['TWITTER']['twitter_consumer_key']
twitter_consumer_secret = config['TWITTER']['twitter_consumer_secret']
twitter_access_token = config['TWITTER']['twitter_access_token']
twitter_access_token_secret = config['TWITTER']['twitter_access_token_secret']
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
auth.set_access_token(twitter_access_token, twitter_access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
tweets = tweepy.Cursor(api.search, q='cats', lang='en').items(10)
for tweet in tweets:
for media in tweet.entities.get('media',[{}]):
if media.get('type', None) == 'photo':
print(media['media_url'])
download_img(media['media_url'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment