Skip to content

Instantly share code, notes, and snippets.

@pkakelas
Last active December 3, 2019 15:04
Show Gist options
  • Save pkakelas/a7fb78c0eaaec999c11e2f9c33e7dc91 to your computer and use it in GitHub Desktop.
Save pkakelas/a7fb78c0eaaec999c11e2f9c33e7dc91 to your computer and use it in GitHub Desktop.
from twython import Twython
import pandas as pd
TTL_TWEETS = 10000
consumer_key="4Y0swuxyRoDNZNWjboh5FI1OP"
consumer_secret="WKbjpokMp9ds3Yg2GwDo69AKeX704ywm2BOLVcR8ieGOri9fWG"
access_token_key="1186727191888957441-piNS1GzpeDw5Mmbjc8w0HzWiSJr2sX"
access_token_secret="LgXyWMbtti2FNnNa1sZrPTIcfPb4sj1azs9vUbJXJQipr"
def find_handle(mentions):
#Search if mentions of the tweet contain any name of the below
names = ["Russell Wilson", "Patrick Mahomes", "Lamar Jackson", "Christian McCaffery", "Deshaun Watson", "Michael Thomas"]
handles = ["DangeRussWilson", "PatrickMahomes", "Lj_era8", "run__cmc", "deshaunwatson", "Cantguardmike"]
handle = name = None
for mention in mentions:
if mention['screen_name'] in handles:
handle = mention['screen_name']
if handle is not None:
name = names[handles.index(handle)]
return [handle, name]
def fetch_tweets():
# Get tweets paginated. 100 per request
api = Twython(consumer_key, consumer_secret, access_token_key, access_token_secret)
tweets = []
oldest_id = 0
res = api.search(q='twitter', result_type='recent', count=100)
while len(tweets) < TTL_TWEETS:
for tweet in res['statuses']:
oldest_tweet = tweet['id']
tweet_list = [tweet['user']['id'], tweet['text']]
tweet_list.extend(find_handle(tweet['entities']['user_mentions']))
tweets.append(tweet_list)
res = api.search(q='twitter', result_type='recent', count=100, max_id=str(oldest_id))
print("Fetched tweets: " + str(len(tweets)))
return tweets
def tweets_to_dataframe(tweets):
# Add all tweets to a dataframe
columns = ['Tweeter', 'Tweet_Text', 'Player_Handle', 'Player']
return pd.DataFrame(tweets, columns=columns)
def main():
tweets = fetch_tweets()
dataframe = tweets_to_dataframe(tweets)
print(dataframe)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment