Last active
October 17, 2019 22:02
-
-
Save davidfloyd91/428fb3b9acaf229d803e421ac319325b to your computer and use it in GitHub Desktop.
Getting started with Tweepy. Accompanies this brief explainer: https://davidfloyd91.github.io/tweepy/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://davidfloyd91.github.io/tweepy/ | |
# install Tweepy: | |
# $ pip install tweepy | |
# set Twitter credentials as temporary environment variables (see post for further explanation) | |
# $ export CONSUMER_KEY="blahblahthisismykey98725987" | |
# repeat for CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET | |
# test environment variables with: | |
# $ echo $CONSUMER_KEY | |
import tweepy | |
import os | |
twitter_app_auth = { | |
'consumer_key': os.environ.get('CONSUMER_KEY'), | |
'consumer_secret': os.environ.get('CONSUMER_SECRET'), | |
'access_token': os.environ.get('ACCESS_TOKEN'), | |
'access_token_secret': os.environ.get('ACCESS_TOKEN_SECRET'), | |
} | |
auth = tweepy.OAuthHandler(twitter_app_auth['consumer_key'], twitter_app_auth['consumer_secret']) | |
auth.set_access_token(twitter_app_auth['access_token'], twitter_app_auth['access_token_secret']) | |
api = tweepy.API(auth) | |
def get_input(): | |
print('What user would you like to see?') | |
return input() | |
input = get_input() | |
user = api.get_user(input) | |
print('creation date:', user.created_at) | |
print('default profile image?', user.default_profile_image) | |
print('statuses count:', user.statuses_count) | |
print('favorites count:', user.favourites_count) | |
print('followers count:', user.followers_count) | |
print('friends count:', user.friends_count) | |
print('friends:') | |
friends = user.friends() | |
for friend in friends: | |
print(friend.screen_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment