Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Greco
committed
Feb 4, 2017
1 parent
6fe9f4d
commit 016ef7b
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
i = [ | ||
'rt:', | ||
'https', | ||
'@', | ||
'trump', | ||
'president', | ||
'amp' | ||
] | ||
|
||
d = [ | ||
'trump', | ||
"say", | ||
"new", | ||
"want", | ||
"trump's", | ||
'donald', | ||
'why', | ||
'i', | ||
'a', | ||
'the', | ||
'how', | ||
'when', | ||
'where', | ||
'what', | ||
'who', | ||
'to', | ||
'of', | ||
'so', | ||
'them', | ||
'you', | ||
'him', | ||
'but', | ||
'his', | ||
'will', | ||
'now', | ||
'via', | ||
'amp' | ||
] | ||
|
||
def excludeWord(s): | ||
if isinstance(s, unicode): | ||
return True | ||
if len(s) <= 2: | ||
return True | ||
for word in i: | ||
if word in s: | ||
return True | ||
for word in d: | ||
if word == s: | ||
return True | ||
return False |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import json, sys, os, tweepy, string, excludeDict | ||
from tweepy import OAuthHandler, Stream | ||
from tweepy.streaming import StreamListener | ||
|
||
consumer_key = '' | ||
consumer_secret = '' | ||
|
||
access_token = '' | ||
access_secret = '' | ||
|
||
# TWITTER API BUILDING | ||
auth = OAuthHandler(consumer_key, consumer_secret) | ||
auth.set_access_token(access_token, access_secret) | ||
api = tweepy.API(auth) | ||
printable = set(string.printable) | ||
|
||
|
||
|
||
class MyListener(StreamListener): | ||
|
||
|
||
def on_data(self, data): | ||
d = json.loads(data) | ||
if 'text' in d: | ||
txt = d['text'] | ||
txt = filter(lambda x: x in printable, txt) | ||
with open('words.txt', 'a') as fi: | ||
for word in txt.lower().split(): | ||
if not excludeDict.excludeWord(str(word)): | ||
fi.write(word + '\n') | ||
fi.close() | ||
|
||
def on_error(self, status): | ||
print(status) | ||
return True | ||
|
||
# -------------------------------------- | ||
|
||
twitter_stream = Stream(auth, MyListener()) | ||
twitter_stream.filter(track=['Trump', "Donald", "Donald Trump"]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys, os, tweepy, time | ||
from tweepy import OAuthHandler | ||
from wordcloud import WordCloud | ||
import numpy as np | ||
from PIL import Image | ||
from os import path | ||
|
||
consumer_key = '' | ||
consumer_secret = '' | ||
access_token = '' | ||
access_secret = '' | ||
|
||
auth = OAuthHandler(consumer_key, consumer_secret) | ||
auth.set_access_token(access_token, access_secret) | ||
api = tweepy.API(auth) | ||
|
||
d = path.dirname(__file__) | ||
|
||
|
||
|
||
mask = np.array(Image.open(path.join(d, "trump_mask.png"))) | ||
|
||
|
||
|
||
|
||
while(True): | ||
time.sleep(21600) # Change to frequency you want | ||
text = open("words.txt").read() | ||
wc = WordCloud(max_words=250, mask=mask, margin=10, | ||
random_state=1, width=2048, height=2048).generate(text) | ||
wc.to_file("trumpCloud.png") | ||
with open('words.txt', 'w') as fi: | ||
fi.write('') | ||
api.update_with_media('trumpCloud.png', '') | ||
print 'Tweet published' |