Skip to content

Instantly share code, notes, and snippets.

@mhl
Created January 3, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhl/1554906 to your computer and use it in GitHub Desktop.
Save mhl/1554906 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import re, csv, sys, urllib, urllib2, time, cgi
from collections import defaultdict
spreadsheet_url = 'https://docs.google.com/spreadsheet/ccc?key=0Am9Hd8ELMkEsdDg4QVd6d0JGeS1WQ0VpTnNBSm5xS2c&output=csv'
season_output = defaultdict(list)
decision_data = {
"Best": ("background-color: #89ff89; color: #000",
'One of my favourites'),
"Keep": ("background-color: #c4ff89; color: #000",
'An episode I liked'),
"Undecided": ("background-color: #f3ff89; color: #000",
"An episode I'm still undecided about"),
"Ignore": ("background-color: #ffffff; color: #888",
"A quite missable episode")
}
def td(s, style):
return " <td style=\"{0}\">{1}</td>\n".format(style, s)
def a(url, text):
return '<a href="{0}">{1}</a>'.format(url, text)
request = urllib2.Request(spreadsheet_url)
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
urllib2.HTTPCookieProcessor())
fp = opener.open(request)
try:
reader = csv.reader(fp)
for i, row in enumerate(reader):
if i < 1:
continue
if len(row) == 9:
n, wiki_ep, title, comment, decision, zh_grade, wiki, zh_link, ww_link = row
else:
n, wiki_ep, title, comment, decision, zh_grade, wiki, zh_link = row
m = re.match('(\d)x(\d+)', n)
if not m:
print >> sys.stderr, "Skipping row: " + " ".join(row)
continue
season, episode = m.groups()
title_underscores = re.sub(' ', '_', title)
ma_url = "http://en.memory-alpha.org/wiki/{0}_(episode)".format(urllib.quote(title_underscores))
wiki_url = 'http://en.wikipedia.org/wiki/List_of_Star_Trek:_The_Next_Generation_episodes#ep' + wiki_ep
style = decision_data[decision][0]
output = td(n, style)
output += td(a(ma_url, title), style)
output += td(a(wiki_url, 'WP:') + ' ' + cgi.escape(wiki), style)
output += td(a(zh_link, 'ZH') + '&nbsp;({0})'.format(zh_grade), style)
if ww_link:
output += td(a(ww_link, 'WW'), style)
output = ' <tr style="{0}">\n{1} </tr>'.format(style, output)
season_output[int(season,10)].append(output)
finally:
fp.close()
print '<p>The key to this colours in this table is:</p>'
print '<ul>'
for decision in "Best", "Keep", "Undecided", "Ignore":
t = decision_data[decision]
print '<li style="{0}">{1}</li>'.format(t[0], t[1])
print '</ul>'
print '</p>'
for s in range(1,8):
print "<h3>Season {0}</h3>\n".format(s)
print "<table>"
for r in season_output[s]:
print r
print "</table>\n"
#!/usr/bin/python
import re, csv, sys, urllib, urllib2, time, cgi
from collections import defaultdict
import random
from subprocess import call
from glob import glob
spreadsheet_url = 'https://docs.google.com/spreadsheet/ccc?key=0Am9Hd8ELMkEsdDg4QVd6d0JGeS1WQ0VpTnNBSm5xS2c&output=csv'
season_output = defaultdict(list)
decisions = set(['Best', 'Keep', 'Undecided', 'Ignore'])
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: %s (%s)" % (sys.argv[0], "|".join(decisions))
sys.exit(1)
chosen_decision = sys.argv[1]
by_decision = defaultdict(list)
request = urllib2.Request(spreadsheet_url)
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
urllib2.HTTPCookieProcessor())
fp = opener.open(request)
try:
reader = csv.reader(fp)
for i, row in enumerate(reader):
if i < 1:
continue
if len(row) == 9:
n, wiki_ep, title, comment, decision, zh_grade, wiki, zh_link, ww_link = row
else:
n, wiki_ep, title, comment, decision, zh_grade, wiki, zh_link = row
m = re.match('(\d)x(\d+)', n)
if not m:
print >> sys.stderr, "Skipping row with unparseable number " + n
continue
season, episode = m.groups()
if decision not in decisions:
print >> sys.stderr, "Skipping row with unknown decision: " + decision
continue
by_decision[decision].append((season, episode))
finally:
fp.close()
to_try = random.sample(by_decision[chosen_decision], 1)[0]
print "trying", to_try
matching_file = glob('/media/no2id/video/star-trek-season*/st-%s-%s*' % to_try)[0]
call(['mplayer', matching_file])
print "Finished playing %dx%d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment