Skip to content

Instantly share code, notes, and snippets.

@SimonS
Last active September 27, 2015 02:38
Show Gist options
  • Save SimonS/1198915 to your computer and use it in GitHub Desktop.
Save SimonS/1198915 to your computer and use it in GitHub Desktop.
Google Spreadsheet-generated CSV -> Reddit Flair Python script
#!/usr/bin/python
# This is the script we used to use to manage the flair on /r/bjj. It's now useless due to
# reddit doing flair assignment, but left it up for posterity or just in case someone wants
# a working example of reading from a CSV.
import csv, re, time
from urllib2 import urlopen
from redditclient import RedditClient
SETTINGS = {
'URL': <CSV URL>,
'REDDIT': <subreddit URL>
}
rc = RedditClient()
while not rc.logged_in:
rc.log_in()
csvReader = csv.DictReader(urlopen(SETTINGS['URL']))
flair_list = []
count = 0
for row in csvReader:
stripes = row['How many Stripes?']
awarded_by = row['Who awarded your Belt?']
belt = re.sub('[-\s]+', '-', row['What is your current Belt grade?']).lower()
user = row['What is your Reddit username?']
flair_description = ''
if stripes != '0 Stripe':
flair_description += stripes + ' - '
flair_description += awarded_by
flair_list.append((user, flair_description, belt))
count += 1
if len(flair_list) == 100:
rc.flaircsv(SETTINGS['REDDIT'], flair_list)
flair_list = []
rc.flaircsv(SETTINGS['REDDIT'], flair_list)
print "[%s] %s users modified" % (time.strftime("%d-%m-%Y %H:%M:%S"), count)
@SimonS
Copy link
Author

SimonS commented Sep 6, 2011

This is the script we use to manage the flair on /r/bjj, it relies on this sweet little API helper to interact with the reddit API. I've hacked the library slightly to allow me to pass my reddit credentials directly in order that this script may be run from a cronjob. I'll leave that as an exercise for the reader ;D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment