Skip to content

Instantly share code, notes, and snippets.

@ajoyoommen
Created May 21, 2018 18:08
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 ajoyoommen/8db76bb22e14669619df098e09adcd18 to your computer and use it in GitHub Desktop.
Save ajoyoommen/8db76bb22e14669619df098e09adcd18 to your computer and use it in GitHub Desktop.
This program will fetch tweets from you Twitter timeline and generate a wordcloud
import tweepy
from wordcloud import WordCloud
import matplotlib.pyplot as plt
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def parse(tweet):
return {
'author': tweet.author.screen_name,
'created_at': tweet.created_at.isoformat(),
'likes': tweet.favorite_count,
'retweets': tweet.retweet_count,
'text': tweet.text
}
def fetch():
data = []
ctr = 1
for tweet in tweepy.Cursor(api.home_timeline).items():
print(ctr, 'Fetched tweet:', tweet.text)
data.append(parse(tweet))
ctr += 1
return data
def clean_tweets(tweets):
data = []
for t in tweets:
text = t['text'].lower().replace('.', '').replace("'", '')
clean = []
for w in text.split():
if w.startswith('@') or w.startswith('https://'):
continue
word = w
for c in '.,!@#$%^&*()-_+=/?<>:;|':
word = word.strip(c)
clean.append(word)
t['text'] = ' '.join(clean)
data.append(t)
return data
def word_cloud(tweets):
cleaned = clean_tweets(tweets)
text = ' '.join((t['text'] for t in cleaned))
cloud = WordCloud(background_color="white", max_words=1000).generate(text)
plt.imshow(cloud, interpolation='bilinear')
plt.axis("off")
plt.show()
def main():
tweets = fetch()
word_cloud(tweets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment