bestofimms
branchimmsview
changeset 33 ad808d18c693
parent 32 85c8f5280d48
--- a/bestofimms	Sun Feb 08 17:27:21 2004 -0500
+++ b/bestofimms	Mon Feb 09 23:29:08 2004 -0500
@@ -1,36 +1,17 @@
 #!/usr/bin/python
 
-import imms
+import os
+from sys import stderr, argv
 from htmltmpl import TemplateManager, TemplateProcessor
-import os
-import ID3
-from sys import stderr
-from ogg.vorbis import VorbisFile
-
-_template = __file__ + '.tmpl'
-
-def rating_to_color(rating):
-	i = rating - 75
-    	if i <= 25:
-       		red = 255
-		green = i * 255 / 25
-		blue = 0
-	elif i <= 50:
-		red = (50-i) * 255 / 25
-		green = 255
-		blue = 0
-	else:
-		red = 0
-		green = 255
-		blue = (i-50) * 255 / 25
-	return "#%02X%02X%02X" % (red, green, blue)
+from utils import get_song_info
+from imms import IMMSDb, rating_to_color
 
 def sort_rating(x, y):
         return x['rating']-y['rating']
 
-def grab_tunes():
-	db = imms.IMMSDb()
-        rates = db.get_ratings(125)
+def grab_tunes(minrate = 75, maxrate = 150):
+	db = IMMSDb()
+        rates = db.get_ratings(minrate, maxrate)
         uids = map(lambda x: x[0], rates)
         files = db.get_paths(uids)
         d = {}
@@ -51,37 +32,27 @@
 	res = []
 	for tune in tunes:
 		song = tune['path']
-		try:
-			os.stat(song)
-		except:
-			continue
+                if not os.path.isfile(song):
+                        continue
+                artist, title = get_song_info(song)
+                if artist and title:
+                        tune['path'] = artist + ' - ' + title
 		tune['color'] = rating_to_color(tune['rating'])
-		if song[-4:] == '.mp3':
-			id3 = ID3.ID3(song)
-			try:
-				tune['path'] = \
-					id3['ARTIST'] + \
-					' - ' + id3['TITLE']
-			except:
-				pass
-		elif song[-4:] == '.ogg':
-			vf = VorbisFile(song)
-			vc = vf.comment()
-			try:
-				tune['path'] = \
-					vc['ARTIST'][0] + \
-					u' - ' + vc['TITLE'][0]
-			except:
-				pass
 		res.append(tune)
 	return res
 
 
-def output_web():
+def output_web(template):
 	tproc = TemplateProcessor()
-	tmpl = TemplateManager().prepare(_template)
-	tproc.set('Bestof', check_tunes(grab_tunes()))
-	print tproc.process(tmpl)
+	tmpl = TemplateManager().prepare(template)
+	tproc.set('Bestof', check_tunes(grab_tunes(125)))
+	return tproc.process(tmpl)
+
+_template = 'bestofimms.tmpl'
+if globals().has_key('__file__'):
+        _template = __file__ + '.tmpl'
 
 if __name__ == '__main__':
-	output_web()
+        if len(argv) > 1:
+                _template = argv[1]
+	print output_web(_template)