bestofimms
author fabien
Sun, 08 Feb 2004 17:27:21 -0500
branchimmsview
changeset 32 85c8f5280d48
parent 31 13f56bb29b96
child 33 ad808d18c693
permissions -rwxr-xr-x
[svn] Conversion of bestofimms to imms.py.

#!/usr/bin/python

import imms
from htmltmpl import TemplateManager, TemplateProcessor
import os
import ID3
from sys import stderr
from ogg.vorbis import VorbisFile

_template = __file__ + '.tmpl'

def rating_to_color(rating):
	i = rating - 75
    	if i <= 25:
       		red = 255
		green = i * 255 / 25
		blue = 0
	elif i <= 50:
		red = (50-i) * 255 / 25
		green = 255
		blue = 0
	else:
		red = 0
		green = 255
		blue = (i-50) * 255 / 25
	return "#%02X%02X%02X" % (red, green, blue)

def sort_rating(x, y):
        return x['rating']-y['rating']

def grab_tunes():
	db = imms.IMMSDb()
        rates = db.get_ratings(125)
        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']
		try:
			os.stat(song)
		except:
			continue
		tune['color'] = rating_to_color(tune['rating'])
		if song[-4:] == '.mp3':
			id3 = ID3.ID3(song)
			try:
				tune['path'] = \
					id3['ARTIST'] + \
					' - ' + id3['TITLE']
			except:
				pass
		elif song[-4:] == '.ogg':
			vf = VorbisFile(song)
			vc = vf.comment()
			try:
				tune['path'] = \
					vc['ARTIST'][0] + \
					u' - ' + vc['TITLE'][0]
			except:
				pass
		res.append(tune)
	return res


def output_web():
	tproc = TemplateProcessor()
	tmpl = TemplateManager().prepare(_template)
	tproc.set('Bestof', check_tunes(grab_tunes()))
	print tproc.process(tmpl)

if __name__ == '__main__':
	output_web()