Skip to content

Instantly share code, notes, and snippets.

@danthedaniel
Last active August 29, 2015 14:24
Show Gist options
  • Save danthedaniel/d05cff24c5227feb1f11 to your computer and use it in GitHub Desktop.
Save danthedaniel/d05cff24c5227feb1f11 to your computer and use it in GitHub Desktop.
Scans through one month of Reddit mod log history and outputs a .csv
# Reddit mod log parser
# Author: teaearlgraycold
#
# Scans through one month of mod log history and outputs a .csv, the first
# column being the date of the mod actions, and each subsequent column the
# count of mod actions for each moderator (only includes moderators currently
# registered to the subreddit).
#
# So each line ends up being a summary for that day's moderator activity.
import praw
import csv
import time
from time import gmtime
from datetime import date
r = praw.Reddit(user_agent='teaearlgraycolds mod log scanner v1')
r.login('', '')
sub = r.get_subreddit('')
mods = [] # Storage for moderator's names
# Get the list of moderators
response = r.request('https://www.reddit.com/r/' + sub.display_name + '/about/moderators.json').json()
for user in response['data']['children']:
mods.append(user['name'])
end_utc = gmtime(time.time() - 60*60*24*30) # One month ago
end_date = date(end_utc[0], end_utc[1], end_utc[2])
done = False
successful = False
last = None
last_time = -1
count = 0
prev_date = ''
current_day = {} # I should use a sorted dict...
f = open('mod_log.csv', 'w')
# Write headers
f.write('date')
for mod in sorted(mods):
f.write(',' + mod)
f.write('\n')
while not done:
log = sub.get_mod_log(limit=100, params={"after": last})
for log_entry in log:
created_utc = log_entry.created_utc
time_utc = gmtime(created_utc)
date_utc = date(time_utc[0], time_utc[1], time_utc[2])
# Check if the ending date has been reached
if date_utc >= end_date:
if date_utc.strftime("%Y-%m-%d") != prev_date: # We're on a new day
if prev_date != '':
print(prev_date + ' Processed')
f.write(prev_date)
for key in sorted(current_day.keys()):
f.write(',' + str(current_day[key]))
f.write('\n')
for mod in mods:
current_day[mod] = 0 # Reset action count
try:
current_day[log_entry.mod] += 1
except KeyError: # For when you run into an old mod that's no longer with us...
pass
# Reached end date
else:
done = True
break
# Check if the last known action (I don't trust the API's ordering)
if log_entry.target_fullname is not None and (created_utc < last_time or last_time < 0):
last = log_entry.id
last_time = created_utc
prev_date = date_utc.strftime("%Y-%m-%d")
count += 100
last_date = gmtime(last_time)
print("On {}/{}/{}: {} parsed".format(last_date[1], last_date[2], last_date[0], count))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment