July 24, 2008

Playlog Displayer

Written by theY4Kman at 5:23 am under Python and Programming

I never did show you the full source code for the playlog page, so here you go! One day I’ll comment it for you :D

from mod_python import apache, util
from PIL import Image
import eyeD3 as id3
import MySQLdb, time, cStringIO
 
def handler(req):
	args = util.parse_qs(req.args or "")
	if args.has_key("pic"):
		return pic(req, args["pic"])
 
	return main(req)
 
def main(req):
	req.content_type = "text/html"
	req.write("""\
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>theY4Kman's Playlog</title>
<link rel="stylesheet" href="style.css" />
</head>
 
<body>""")
 
	db = MySQLdb.connect(host="localhost", user="root", passwd="fooledu", db="amarok")
	crs = db.cursor()
	crs.execute("""SELECT playlog.play_time, song_paths.song_path, song_paths.song_id
	FROM playlog, song_paths
	WHERE playlog.song_id = song_paths.song_id
	ORDER BY playlog.play_time DESC
	LIMIT 0,20""")
 
	grey = False
	res = crs.fetchone()
	while res:
		title = artist = album = "<none>"
		year = ""
 
		tag = id3.Tag()
		try:
			tag.link(res[1])
			title = tag.getTitle()
			artist = tag.getArtist()
			album = tag.getAlbum()
			year = tag.getYear()
		except:
			res = crs.fetchone()
			continue
 
		req.write("""\
<div class="track %s">
	<div class="basicinfo">
		<div class="extendedinfo">
			<br />
		</div>
 
		<em class="time">%s</em>
		<div class="cover">
			<img class="coverimg" src="music.py?pic=%s" />
		</div>
		<div class="trackinfo">
			<strong>%s</strong>%s%s
		</div>
	</div>
</div>
 
"""
		% ( ( grey != False and "bg1" or "bg2" ),
		time.strftime( "%Z %I:%M%p %A, %B %d", time.localtime(res[0]) ),
		res[2],
		title,
		artist != "" and ("<br /> by <strong>%s</strong>" % artist) or "",
		album != "" and ("<br /> on <strong>%s</strong>" % album) or "" ) )
 
		grey = not grey	# Toggle between grey and white backgrounds.
 
		res = crs.fetchone()
 
	req.write("</none></body>\n</html>")
 
	return apache.OK
 
def pic(req, id):
	req.content_type = "text/html"
 
	db = MySQLdb.connect(host="localhost", user="root", passwd="fooledu", db="playlog")
	crs = db.cursor()
	crs.execute("SELECT song_path FROM songs WHERE song_id = %s LIMIT 1", id)
 
	res = crs.fetchone()
	if not res: return defaultpic(req)
 
	tag = id3.Tag()
 
	try:
		tag.link(res[0])
		imgs = tag.getImages()
 
		if not tag or len(imgs) < 1: return defaultpic(req)
 
		req.content_type = imgs[0].mimeType
 
		im_str = cStringIO.StringIO(imgs[0].imageData)
		im = Image.open(im_str)
 
		if im.size[0] > 130:
			im = im.resize( (130, 130), Image.ANTIALIAS )
 
		im_str.close()
		im_str = cStringIO.StringIO()
		im.save(im_str, "JPEG")
 
		req.write("%s" % im_str.getvalue())
		im_str.close()
	except:
		return defaultpic(req)
 
	return apache.OK
 
def defaultpic(req):
	img = open("/home/they4kman/site/music/covers/__default.png", "rb")
	if not img: return apache.OK
 
	req.content_type = "image/png"
	req.write(img.read())
 
	img.close()
 
	return apache.OK

No Comments »

Huh. No comments. You make one!

Comments RSS TrackBack URL

Leave a comment, would ya?

Powered by WordPress. Driven by caffeine.