Created
February 26, 2014 19:09
-
-
Save hamaluik/9236275 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
print '+----------------------------------------+' | |
print '| GradeMe-Watchman Bot by /u/FuzzyWuzzie |' | |
print '+----------------------------------------+' | |
import praw | |
from pprint import pprint | |
from datetime import datetime | |
import sys | |
import time | |
import math | |
# configuration | |
daysOld = 10 | |
botUsername = "<enter username here>" | |
botPassword = "<enter password here>" | |
peopleToMessage = ['theinfamousj'] | |
def log(str, prefixDate = True, newLine = True): | |
if prefixDate: | |
d = datetime.now() | |
sys.stdout.write(d.strftime('%y-%m-%d %H:%M:%S => ')) | |
sys.stdout.write(str) | |
if newLine: | |
print '' | |
def humanTimeDiff(secs): | |
mins, secs = divmod(secs, 60) | |
hours, mins = divmod(mins, 60) | |
days, hours = divmod(hours, 24) | |
weeks, days = divmod(days, 7) | |
months, weeks = divmod(weeks, 4) | |
years, months = divmod(months, 12) | |
if years > 0: | |
return '%d years' % years | |
if months > 0: | |
return '%d months' % months | |
elif weeks > 0: | |
return '%d weeks' % weeks | |
elif days > 0: | |
return '%d days' % days | |
elif hours > 0: | |
return '%d hours' % hours | |
elif minutes > 0: | |
return '%d minutes' % mins | |
return '%d seconds' % secs | |
# connect and log in | |
log('Logging in...', True, False) | |
r = praw.Reddit('GradeMe Watchman bot by /u/FuzzyWuzzie') | |
r.login(botUsername, botPassword) | |
log(' done', False, True) | |
# keep track of threads we've already checked | |
alreadyDone = [] | |
secondsOld = daysOld * 24 * 3600 | |
# loop forever! | |
while True: | |
# get our subreddit | |
log('Getting subreddit...', True, False) | |
subreddit = r.get_subreddit('GradeMe') | |
log(' done', False, True) | |
# get the latest posts | |
log('Getting submissions...', True, False) | |
submissions = subreddit.get_new(limit=100) | |
log(' done', False, True) | |
# get the current timestamp | |
currentTimeStamp = time.time() | |
# loop through them | |
log('Finding old submissions without comment...', True, True) | |
for submission in submissions: | |
# make sure we haven't already touched it | |
# find a post with 0 comments: | |
# only look at old posts: | |
if submission.id not in alreadyDone and submission.num_comments == 0 and submission.created + secondsOld < currentTimeStamp: | |
# yup, we found an old post with no comments! | |
# alert the mods! | |
log('Found old submission (over %s old) with no comments by %s' % (humanTimeDiff(currentTimeStamp - submission.created), submission.author.name), True, True) | |
msg = 'GradeMe-Watchman found an unanswered post in /r/GradeMe: [%s](%s) by /u/%s! The post is over %s old!' % (submission.title, submission.short_link, submission.author.name, humanTimeDiff(currentTimeStamp - submission.created)) | |
for target in peopleToMessage: | |
r.user.send_message(target, msg) | |
alreadyDone.append(submission.id) | |
# sleep for a while | |
log('Sleeping for half an hour...', True, True) | |
time.sleep(1800) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment