bestofimms
author fabien
Sun, 08 Feb 2004 16:55:24 -0500
branchimmsview
changeset 31 13f56bb29b96
child 32 85c8f5280d48
permissions -rwxr-xr-x
[svn] New bestofimms, cleanimms, imms.py and utils.py.

#!/usr/bin/python

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

_imms_db = os.environ['HOME'] + '/.imms/imms.db'
_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 grab_tunes():
	sql = sqlite.connect(_imms_db, autocommit = 1)
	cu = sql.cursor()
	cu.execute(''' SELECT r.rating, l.path
		FROM Library l, Rating r
		WHERE r.uid = l.uid AND r.rating >= 125
		ORDER BY r.rating DESC;''')
	return map(lambda x: {
		'rating' : x[0],
		'path': x[1]},
		cu.fetchall() )

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()