equal
deleted
inserted
replaced
1 #!/usr/bin/python |
1 #!/usr/bin/python |
2 |
2 |
3 import imms |
3 import os |
|
4 from sys import stderr, argv |
4 from htmltmpl import TemplateManager, TemplateProcessor |
5 from htmltmpl import TemplateManager, TemplateProcessor |
5 import os |
6 from utils import get_song_info |
6 import ID3 |
7 from imms import IMMSDb, rating_to_color |
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 |
8 |
28 def sort_rating(x, y): |
9 def sort_rating(x, y): |
29 return x['rating']-y['rating'] |
10 return x['rating']-y['rating'] |
30 |
11 |
31 def grab_tunes(): |
12 def grab_tunes(minrate = 75, maxrate = 150): |
32 db = imms.IMMSDb() |
13 db = IMMSDb() |
33 rates = db.get_ratings(125) |
14 rates = db.get_ratings(minrate, maxrate) |
34 uids = map(lambda x: x[0], rates) |
15 uids = map(lambda x: x[0], rates) |
35 files = db.get_paths(uids) |
16 files = db.get_paths(uids) |
36 d = {} |
17 d = {} |
37 for fn in files: |
18 for fn in files: |
38 d[fn[0]] = fn[2] |
19 d[fn[0]] = fn[2] |
49 |
30 |
50 def check_tunes(tunes): |
31 def check_tunes(tunes): |
51 res = [] |
32 res = [] |
52 for tune in tunes: |
33 for tune in tunes: |
53 song = tune['path'] |
34 song = tune['path'] |
54 try: |
35 if not os.path.isfile(song): |
55 os.stat(song) |
36 continue |
56 except: |
37 artist, title = get_song_info(song) |
57 continue |
38 if artist and title: |
|
39 tune['path'] = artist + ' - ' + title |
58 tune['color'] = rating_to_color(tune['rating']) |
40 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) |
41 res.append(tune) |
77 return res |
42 return res |
78 |
43 |
79 |
44 |
80 def output_web(): |
45 def output_web(template): |
81 tproc = TemplateProcessor() |
46 tproc = TemplateProcessor() |
82 tmpl = TemplateManager().prepare(_template) |
47 tmpl = TemplateManager().prepare(template) |
83 tproc.set('Bestof', check_tunes(grab_tunes())) |
48 tproc.set('Bestof', check_tunes(grab_tunes(125))) |
84 print tproc.process(tmpl) |
49 return tproc.process(tmpl) |
|
50 |
|
51 _template = 'bestofimms.tmpl' |
|
52 if globals().has_key('__file__'): |
|
53 _template = __file__ + '.tmpl' |
85 |
54 |
86 if __name__ == '__main__': |
55 if __name__ == '__main__': |
87 output_web() |
56 if len(argv) > 1: |
|
57 _template = argv[1] |
|
58 print output_web(_template) |