bestofimms
branchimmsview
changeset 32 85c8f5280d48
parent 31 13f56bb29b96
child 33 ad808d18c693
equal deleted inserted replaced
31:13f56bb29b96 32:85c8f5280d48
     1 #!/usr/bin/python
     1 #!/usr/bin/python
     2 
     2 
     3 import sqlite
     3 import imms
     4 from htmltmpl import TemplateManager, TemplateProcessor
     4 from htmltmpl import TemplateManager, TemplateProcessor
     5 import os
     5 import os
     6 import ID3
     6 import ID3
     7 from sys import stderr
     7 from sys import stderr
     8 from ogg.vorbis import VorbisFile
     8 from ogg.vorbis import VorbisFile
     9 
     9 
    10 _imms_db = os.environ['HOME'] + '/.imms/imms.db'
       
    11 _template = __file__ + '.tmpl'
    10 _template = __file__ + '.tmpl'
    12 
    11 
    13 def rating_to_color(rating):
    12 def rating_to_color(rating):
    14 	i = rating - 75
    13 	i = rating - 75
    15     	if i <= 25:
    14     	if i <= 25:
    24 		red = 0
    23 		red = 0
    25 		green = 255
    24 		green = 255
    26 		blue = (i-50) * 255 / 25
    25 		blue = (i-50) * 255 / 25
    27 	return "#%02X%02X%02X" % (red, green, blue)
    26 	return "#%02X%02X%02X" % (red, green, blue)
    28 
    27 
       
    28 def sort_rating(x, y):
       
    29         return x['rating']-y['rating']
       
    30 
    29 def grab_tunes():
    31 def grab_tunes():
    30 	sql = sqlite.connect(_imms_db, autocommit = 1)
    32 	db = imms.IMMSDb()
    31 	cu = sql.cursor()
    33         rates = db.get_ratings(125)
    32 	cu.execute(''' SELECT r.rating, l.path
    34         uids = map(lambda x: x[0], rates)
    33 		FROM Library l, Rating r
    35         files = db.get_paths(uids)
    34 		WHERE r.uid = l.uid AND r.rating >= 125
    36         d = {}
    35 		ORDER BY r.rating DESC;''')
    37         for fn in files:
    36 	return map(lambda x: {
    38                 d[fn[0]] = fn[2]
    37 		'rating' : x[0],
    39         res = []
    38 		'path': x[1]},
    40         for rate in rates:
    39 		cu.fetchall() )
    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
    40 
    49 
    41 def check_tunes(tunes):
    50 def check_tunes(tunes):
    42 	res = []
    51 	res = []
    43 	for tune in tunes:
    52 	for tune in tunes:
    44 		song = tune['path']
    53 		song = tune['path']