Last active
May 16, 2017 16:16
-
-
Save dave-martinez/dbf988405ba079e814511c104acdf50d to your computer and use it in GitHub Desktop.
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
from twitter import * | |
import re | |
import numpy as np | |
from wordcloud import WordCloud, STOPWORDS | |
import matplotlib.pyplot as plt | |
ACCESS_TOKEN = "INSERT" | |
ACCESS_SECRET = "INSERT" | |
CONSUMER_KEY = "INSERT" | |
CONSUMER_SECRET = "INSERT" | |
t = Twitter(auth=OAuth(ACCESS_TOKEN, | |
ACCESS_SECRET, | |
CONSUMER_KEY, | |
CONSUMER_SECRET)) | |
#Emoji Code | |
emoji_pattern = re.compile("[" | |
u"\U0001F600-\U0001F64F" # emoticons | |
u"\U0001F300-\U0001F5FF" # symbols & pictographs | |
u"\U0001F680-\U0001F6FF" # transport & map symbols | |
u"\U0001F1E0-\U0001F1FF" # flags (iOS) | |
"]+", flags=re.UNICODE) | |
# Tweets | |
all_tweets = [] | |
new_tweets = t.statuses.user_timeline(count = 200, | |
include_rts = 0, | |
exclude_replies = 1) | |
oldest = new_tweets[-1]["id"] -1 | |
# Filter emoji and links | |
for tweet in new_tweets: | |
id = tweet["id"] | |
tweet = emoji_pattern.sub('',tweet["text"]) | |
tweet = re.sub(r'http\S+', '', tweet) | |
all_tweets.append([id, tweet]) | |
print("Initial get.") | |
# Grab more Tweets | |
while len(new_tweets) > 0: | |
new_tweets = t.statuses.user_timeline(count = 200, | |
include_rts = 0, | |
exclude_replies = 1, | |
max_id = oldest) | |
for tweet in new_tweets: | |
id = tweet["id"] | |
tweet = emoji_pattern.sub('',tweet["text"]) | |
tweet = re.sub(r'http\S+', '', tweet) | |
all_tweets.append([id, tweet]) | |
oldest = all_tweets[-1][0] - 1 | |
print("...%s tweets downloaded so far" % (len(all_tweets))) | |
tweet_array = np.array(all_tweets) | |
# Create the Word Cloud | |
word_string = "" | |
for x in tweet_array: | |
word_string += x[1] | |
wordcloud = WordCloud(stopwords=STOPWORDS, | |
max_words=800, | |
height=1600, | |
width=800, | |
background_color='white').generate(word_string) | |
plt.figure( figsize=(20,10), facecolor='k') | |
plt.imshow(wordcloud) | |
plt.axis("off") | |
plt.tight_layout(pad=0) | |
plt.show() | |
plt.savefig('wordcloud.png', facecolor='k', bbox_inches='tight') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment