| author | Fabien Ninoles <fabien@tzone.org> | 
| Sun, 07 Dec 2008 16:05:33 -0500 | |
| branch | immsview | 
| changeset 39 | a26e907b8022 | 
| parent 33 | ad808d18c693 | 
| permissions | -rwxr-xr-x | 
| 31 | 1  | 
#!/usr/bin/python  | 
2  | 
||
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
3  | 
import os  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
4  | 
from sys import stderr, argv  | 
| 31 | 5  | 
from htmltmpl import TemplateManager, TemplateProcessor  | 
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
6  | 
from utils import get_song_info  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
7  | 
from imms import IMMSDb, rating_to_color  | 
| 31 | 8  | 
|
| 32 | 9  | 
def sort_rating(x, y):  | 
10  | 
return x['rating']-y['rating']  | 
|
11  | 
||
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
12  | 
def grab_tunes(minrate = 75, maxrate = 150):  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
13  | 
db = IMMSDb()  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
14  | 
rates = db.get_ratings(minrate, maxrate)  | 
| 32 | 15  | 
uids = map(lambda x: x[0], rates)  | 
16  | 
files = db.get_paths(uids)  | 
|
17  | 
        d = {}
 | 
|
18  | 
for fn in files:  | 
|
19  | 
d[fn[0]] = fn[2]  | 
|
20  | 
res = []  | 
|
21  | 
for rate in rates:  | 
|
22  | 
try:  | 
|
23  | 
                        t = { 'rating': rate[1],
 | 
|
24  | 
'path' : d[rate[0]] }  | 
|
25  | 
res.append(t)  | 
|
26  | 
except KeyError:  | 
|
27  | 
pass  | 
|
28  | 
res.sort(lambda x, y: -sort_rating(x, y))  | 
|
29  | 
return res  | 
|
| 31 | 30  | 
|
31  | 
def check_tunes(tunes):  | 
|
32  | 
res = []  | 
|
33  | 
for tune in tunes:  | 
|
34  | 
song = tune['path']  | 
|
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
35  | 
if not os.path.isfile(song):  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
36  | 
continue  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
37  | 
artist, title = get_song_info(song)  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
38  | 
if artist and title:  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
39  | 
tune['path'] = artist + ' - ' + title  | 
| 31 | 40  | 
tune['color'] = rating_to_color(tune['rating'])  | 
41  | 
res.append(tune)  | 
|
42  | 
return res  | 
|
43  | 
||
44  | 
||
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
45  | 
def output_web(template):  | 
| 31 | 46  | 
tproc = TemplateProcessor()  | 
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
47  | 
tmpl = TemplateManager().prepare(template)  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
48  | 
	tproc.set('Bestof', check_tunes(grab_tunes(125)))
 | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
49  | 
return tproc.process(tmpl)  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
50  | 
|
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
51  | 
_template = 'bestofimms.tmpl'  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
52  | 
if globals().has_key('__file__'):
 | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
53  | 
_template = __file__ + '.tmpl'  | 
| 31 | 54  | 
|
55  | 
if __name__ == '__main__':  | 
|
| 
33
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
56  | 
if len(argv) > 1:  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
57  | 
_template = argv[1]  | 
| 
 
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
 
fabien 
parents: 
32 
diff
changeset
 | 
58  | 
print output_web(_template)  |