bestofimms
branchimmsview
changeset 31 13f56bb29b96
child 32 85c8f5280d48
equal deleted inserted replaced
30:a7f7026f9416 31:13f56bb29b96
       
     1 #!/usr/bin/python
       
     2 
       
     3 import sqlite
       
     4 from htmltmpl import TemplateManager, TemplateProcessor
       
     5 import os
       
     6 import ID3
       
     7 from sys import stderr
       
     8 from ogg.vorbis import VorbisFile
       
     9 
       
    10 _imms_db = os.environ['HOME'] + '/.imms/imms.db'
       
    11 _template = __file__ + '.tmpl'
       
    12 
       
    13 def rating_to_color(rating):
       
    14 	i = rating - 75
       
    15     	if i <= 25:
       
    16        		red = 255
       
    17 		green = i * 255 / 25
       
    18 		blue = 0
       
    19 	elif i <= 50:
       
    20 		red = (50-i) * 255 / 25
       
    21 		green = 255
       
    22 		blue = 0
       
    23 	else:
       
    24 		red = 0
       
    25 		green = 255
       
    26 		blue = (i-50) * 255 / 25
       
    27 	return "#%02X%02X%02X" % (red, green, blue)
       
    28 
       
    29 def grab_tunes():
       
    30 	sql = sqlite.connect(_imms_db, autocommit = 1)
       
    31 	cu = sql.cursor()
       
    32 	cu.execute(''' SELECT r.rating, l.path
       
    33 		FROM Library l, Rating r
       
    34 		WHERE r.uid = l.uid AND r.rating >= 125
       
    35 		ORDER BY r.rating DESC;''')
       
    36 	return map(lambda x: {
       
    37 		'rating' : x[0],
       
    38 		'path': x[1]},
       
    39 		cu.fetchall() )
       
    40 
       
    41 def check_tunes(tunes):
       
    42 	res = []
       
    43 	for tune in tunes:
       
    44 		song = tune['path']
       
    45 		try:
       
    46 			os.stat(song)
       
    47 		except:
       
    48 			continue
       
    49 		tune['color'] = rating_to_color(tune['rating'])
       
    50 		if song[-4:] == '.mp3':
       
    51 			id3 = ID3.ID3(song)
       
    52 			try:
       
    53 				tune['path'] = \
       
    54 					id3['ARTIST'] + \
       
    55 					' - ' + id3['TITLE']
       
    56 			except:
       
    57 				pass
       
    58 		elif song[-4:] == '.ogg':
       
    59 			vf = VorbisFile(song)
       
    60 			vc = vf.comment()
       
    61 			try:
       
    62 				tune['path'] = \
       
    63 					vc['ARTIST'][0] + \
       
    64 					u' - ' + vc['TITLE'][0]
       
    65 			except:
       
    66 				pass
       
    67 		res.append(tune)
       
    68 	return res
       
    69 
       
    70 
       
    71 def output_web():
       
    72 	tproc = TemplateProcessor()
       
    73 	tmpl = TemplateManager().prepare(_template)
       
    74 	tproc.set('Bestof', check_tunes(grab_tunes()))
       
    75 	print tproc.process(tmpl)
       
    76 
       
    77 if __name__ == '__main__':
       
    78 	output_web()