Skip to content

Instantly share code, notes, and snippets.

@lollipoppizza
Last active April 15, 2016 15:48
Show Gist options
  • Save lollipoppizza/abad69d7964d6766ae6799172a9c045b to your computer and use it in GitHub Desktop.
Save lollipoppizza/abad69d7964d6766ae6799172a9c045b to your computer and use it in GitHub Desktop.
import praw
from collections import deque
from time import sleep
from datetime import date
import calendar
import shelve
r = praw.Reddit("Python:SportsTeamBiasAnalyser:1.0 (by /u/lollipoppizza)")
cache = deque(maxlen=200) # To make sure we don't duplicate effort
drivers = ["Rosberg", "Hamilton", "Vettel", "Raikkonen", "Massa", "Bottas", "Ricciardo", "Kvyat", "Perez", "Hulkenberg", "Palmer", "Magnussen", "Verstappen", "Sainz", "Ericsson", "Nasr", "Alonso", "Button", "Wehrlein", "Haryanto", "Grosjean", "Gutierrez", "Mercedes", "Ferrari", "Williams", "Red Bull", "Force India", "Renault", "Toro Rosso", "Sauber", "McLaren", "Manor", "Haas"]
words = set(drivers)
data = {driver:0 for driver in drivers}
driverFound = ""
my_date = date.today()
file = "driverCount"+calendar.day_name[my_date.weekday()]
d = shelve.open(file)
def word_check(comment_body, words):
# Will split the comment_body into individual words and check each for membership in words
global driverFound
# Split comment into words
comment_words = comment_body.split()
# Check each word for hot word and return True if found
for word in comment_words:
if word in words:
driverFound = word
return True
# Return false if no words in words
return False
# Loop through comments
running = True
while running:
all = r.get_comments('formula1', limit=None)
for comment in all:
# if comment id exists in cache, break
if comment.id in cache:
break
cache.append(comment.id) # cache already found comment id
# execute method for comment body and hotword(s)
if word_check(comment.body, words):
try:
# action
data[driverFound] += 1
print(driverFound, data[driverFound])
d.update(data)
except KeyboardInterrupt:
d.close()
running = False
except praw.errors.APIException as e:
print("[ERROR]:", e)
print("Sleeping for 30 seconds")
sleep(30)
except Exception as e:
print("[ERROR]:", e)
print("Blindly handling error")
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment