| 31 |      1 | #!/usr/bin/python
 | 
|  |      2 | 
 | 
| 32 |      3 | import imms
 | 
| 31 |      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 | _template = __file__ + '.tmpl'
 | 
|  |     11 | 
 | 
|  |     12 | def rating_to_color(rating):
 | 
|  |     13 | 	i = rating - 75
 | 
|  |     14 |     	if i <= 25:
 | 
|  |     15 |        		red = 255
 | 
|  |     16 | 		green = i * 255 / 25
 | 
|  |     17 | 		blue = 0
 | 
|  |     18 | 	elif i <= 50:
 | 
|  |     19 | 		red = (50-i) * 255 / 25
 | 
|  |     20 | 		green = 255
 | 
|  |     21 | 		blue = 0
 | 
|  |     22 | 	else:
 | 
|  |     23 | 		red = 0
 | 
|  |     24 | 		green = 255
 | 
|  |     25 | 		blue = (i-50) * 255 / 25
 | 
|  |     26 | 	return "#%02X%02X%02X" % (red, green, blue)
 | 
|  |     27 | 
 | 
| 32 |     28 | def sort_rating(x, y):
 | 
|  |     29 |         return x['rating']-y['rating']
 | 
|  |     30 | 
 | 
| 31 |     31 | def grab_tunes():
 | 
| 32 |     32 | 	db = imms.IMMSDb()
 | 
|  |     33 |         rates = db.get_ratings(125)
 | 
|  |     34 |         uids = map(lambda x: x[0], rates)
 | 
|  |     35 |         files = db.get_paths(uids)
 | 
|  |     36 |         d = {}
 | 
|  |     37 |         for fn in files:
 | 
|  |     38 |                 d[fn[0]] = fn[2]
 | 
|  |     39 |         res = []
 | 
|  |     40 |         for rate in rates:
 | 
|  |     41 |                 try:
 | 
|  |     42 |                         t = { 'rating': rate[1],
 | 
|  |     43 |                               'path' : d[rate[0]] }
 | 
|  |     44 |                         res.append(t)
 | 
|  |     45 |                 except KeyError:
 | 
|  |     46 |                         pass
 | 
|  |     47 |         res.sort(lambda x, y: -sort_rating(x, y))
 | 
|  |     48 |         return res
 | 
| 31 |     49 | 
 | 
|  |     50 | def check_tunes(tunes):
 | 
|  |     51 | 	res = []
 | 
|  |     52 | 	for tune in tunes:
 | 
|  |     53 | 		song = tune['path']
 | 
|  |     54 | 		try:
 | 
|  |     55 | 			os.stat(song)
 | 
|  |     56 | 		except:
 | 
|  |     57 | 			continue
 | 
|  |     58 | 		tune['color'] = rating_to_color(tune['rating'])
 | 
|  |     59 | 		if song[-4:] == '.mp3':
 | 
|  |     60 | 			id3 = ID3.ID3(song)
 | 
|  |     61 | 			try:
 | 
|  |     62 | 				tune['path'] = \
 | 
|  |     63 | 					id3['ARTIST'] + \
 | 
|  |     64 | 					' - ' + id3['TITLE']
 | 
|  |     65 | 			except:
 | 
|  |     66 | 				pass
 | 
|  |     67 | 		elif song[-4:] == '.ogg':
 | 
|  |     68 | 			vf = VorbisFile(song)
 | 
|  |     69 | 			vc = vf.comment()
 | 
|  |     70 | 			try:
 | 
|  |     71 | 				tune['path'] = \
 | 
|  |     72 | 					vc['ARTIST'][0] + \
 | 
|  |     73 | 					u' - ' + vc['TITLE'][0]
 | 
|  |     74 | 			except:
 | 
|  |     75 | 				pass
 | 
|  |     76 | 		res.append(tune)
 | 
|  |     77 | 	return res
 | 
|  |     78 | 
 | 
|  |     79 | 
 | 
|  |     80 | def output_web():
 | 
|  |     81 | 	tproc = TemplateProcessor()
 | 
|  |     82 | 	tmpl = TemplateManager().prepare(_template)
 | 
|  |     83 | 	tproc.set('Bestof', check_tunes(grab_tunes()))
 | 
|  |     84 | 	print tproc.process(tmpl)
 | 
|  |     85 | 
 | 
|  |     86 | if __name__ == '__main__':
 | 
|  |     87 | 	output_web()
 |