Created
August 8, 2015 15:20
-
-
Save H0neyBadger/ed8ebeba0a86d4eaed6f to your computer and use it in GitHub Desktop.
Splatfest popularity predictions (based on twitter search API, Python3 and python requests)
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
{ | |
"api_keys":{ | |
"key":"your_consumer_key", | |
"secret":"your_oauth_secret" | |
}, | |
"search_query":"#Splatoon AND (north -america -american OR south) since:2015-08-01 -filter:retweets", | |
"team1":{ | |
"name":"Regex South", | |
"regex":"South" | |
}, | |
"team2":{ | |
"name":"Regex North", | |
"regex":"North(?!\\s{0,1}America)" | |
} | |
} |
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
import requests | |
from requests.auth import HTTPBasicAuth | |
import json | |
import operator | |
import re | |
def get_token(login, password): | |
data = "grant_type=client_credentials" | |
headers = { | |
'Content-type':"application/x-www-form-urlencoded;charset=UTF-8", | |
} | |
con = requests.post('https://api.twitter.com/oauth2/token', | |
auth=HTTPBasicAuth(login, password), | |
data=data, | |
headers=headers) | |
if con.status_code != 200: | |
print(con.json()) | |
exit(1) | |
return con.json() | |
def search(tokens, query, max_id=None, hashtags={}): | |
headers = { | |
'Authorization': 'Bearer {access_token}'.format(**tokens), | |
} | |
params = { | |
"q": query, | |
"count":100 | |
} | |
back_id = None | |
if max_id : | |
params["max_id"]=max_id | |
back_id = max_id | |
r = requests.get("https://api.twitter.com/1.1/search/tweets.json", | |
params=params, | |
headers=headers) | |
if r.status_code == 200: | |
for row in r.json()['statuses']: | |
print(row['text']) | |
if team1.search(row['text']): | |
#print("match a") | |
count = hashtags.get(team1name,0) | |
hashtags[team1name]= count + 1 | |
if team2.search(row['text']): | |
#print("match b") | |
count = hashtags.get(team2name,0) | |
hashtags[team2name]= count + 1 | |
for hash in row['entities']['hashtags']: | |
tag = hash['text'].lower() | |
if team1.search(tag) or team2.search(tag): | |
count = hashtags.get(tag,0) | |
hashtags[tag]= count + 1 | |
back_id = max_id | |
max_id = row['id_str'] | |
sorted_x = sorted(hashtags.items(), key=operator.itemgetter(1)) | |
print("hash;count") | |
for x in sorted_x: | |
print("{0};{1}".format(*x)) | |
print("") | |
search(tokens, query, max_id=max_id,hashtags=hashtags) | |
elif r.status_code == 404: | |
print("ERROR {0}: {1}".format(r.status_code,r.url)) | |
if back_id : | |
search(tokens, query, max_id=back_id,hashtags=hashtags) | |
else: | |
print("ERROR {0}: {1}".format(r.status_code,r.text)) | |
if __name__ == '__main__': | |
with open('conf.json') as conf_file: | |
conf = json.load(conf_file) | |
key = conf["api_keys"]["key"] | |
secret = conf["api_keys"]["secret"] | |
query = conf["search_query"] | |
team1name = conf["team1"]["name"] | |
team2name = conf["team2"]["name"] | |
team1 = re.compile(conf["team1"]["regex"], re.IGNORECASE|re.MULTILINE) | |
team2 = re.compile(conf["team2"]["regex"], re.IGNORECASE|re.MULTILINE) | |
tokens = get_token(key,secret) | |
search(tokens,query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment