[svn] Better OO approach.
#!/usr/bin/python
import os
from sys import stderr, argv
from htmltmpl import TemplateManager, TemplateProcessor
from utils import get_song_info
from imms import IMMSDb, rating_to_color
def sort_rating(x, y):
return x['rating']-y['rating']
def grab_tunes(minrate = 75, maxrate = 150):
db = IMMSDb()
rates = db.get_ratings(minrate, maxrate)
uids = map(lambda x: x[0], rates)
files = db.get_paths(uids)
d = {}
for fn in files:
d[fn[0]] = fn[2]
res = []
for rate in rates:
try:
t = { 'rating': rate[1],
'path' : d[rate[0]] }
res.append(t)
except KeyError:
pass
res.sort(lambda x, y: -sort_rating(x, y))
return res
def check_tunes(tunes):
res = []
for tune in tunes:
song = tune['path']
if not os.path.isfile(song):
continue
artist, title = get_song_info(song)
if artist and title:
tune['path'] = artist + ' - ' + title
tune['color'] = rating_to_color(tune['rating'])
res.append(tune)
return res
def output_web(template):
tproc = TemplateProcessor()
tmpl = TemplateManager().prepare(template)
tproc.set('Bestof', check_tunes(grab_tunes(125)))
return tproc.process(tmpl)
_template = 'bestofimms.tmpl'
if globals().has_key('__file__'):
_template = __file__ + '.tmpl'
if __name__ == '__main__':
if len(argv) > 1:
_template = argv[1]
print output_web(_template)