Skip to content

Instantly share code, notes, and snippets.

@rocapp
Last active January 16, 2017 16:40
Show Gist options
  • Save rocapp/aafe619b828a801f53d074751b1f1bfb to your computer and use it in GitHub Desktop.
Save rocapp/aafe619b828a801f53d074751b1f1bfb to your computer and use it in GitHub Desktop.
import numpy as np
from tweepy import API, OAuthHandler
import json
auth = OAuthHandler(consumer_key, consumer_secret) # Connect to twitter via oAuth
auth.set_access_token(access_token, access_token_secret)
api = API(auth)
def get_haikus(api):
return api.user_timeline('@meta_aiai_haiku') # Get haikus from my timeline
def get_search(api):
return api.search('genius') # Search for a term (e.g. 'genius')
counts = dict()
rs = list()
fs = list()
for h in get_haikus(api):
rs.append(h.retweet_count) # Count number of retweets and favorites for each tweet in the list
fs.append(h.favorite_count)
stext = [ss for ss in h.text.split(' ') if '/' not in ss]
counts[' '.join(stext)] = (h.retweet_count, h.favorite_count) # Store in a dict
for st in get_search(api): # Do the same for any specific search terms
rs.append(st.retweet_count)
fs.append(st.favorite_count)
stext = [ss for ss in st.text.split(' ') if '/' not in ss]
counts[' '.join(stext)] = (st.retweet_count, st.favorite_count)
rs = np.array(rs) # Make an array with the retweet counts
if any([rr for rr in rs if rr != 0.0]):
norm_r = (rs - np.min(rs))*100.00 / ((np.max(rs) - np.min(rs))*100.00) # Normalize the counts
else: norm_r = rs
fs = np.array(fs)
norm_f = (fs - np.min(fs))*100.00 / ((np.max(fs) - np.min(fs))*100.00) # Same for the favorites
for ix, k in enumerate(counts.keys()):
weight = 0.01*np.random.randint(55, 77) * (norm_r[ix] + norm_f[ix]) # Calculate the likelihood of a tweet showing up by adding these metrics and multiplying by a random value
counts[k] = weight
with open('best.txt', 'w+') as bf: # dump to best.txt
json.dump(counts, bf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment