Posted by iggykoopa on Mon 3rd Aug 20:34 (modification of post by iggykoopa view diff)
diff | download | new post
- #!/usr/bin/python
- import os, commands, urllib, Image, sys, types, shutil
- from optparse import OptionParser
- def get_info(player):
- check_running = commands.getoutput("ps aux")
- check_running = check_running.split("\n")
- if player == "moc":
- for line in check_running:
- if "mocp" in line:
- return commands.getoutput("mocp -Q %artist"),commands.getoutput("mocp -Q %album"),commands.getoutput("mocp -Q %song")
- return "","",""
- if player == "rhythmbox":
- for line in check_running:
- if "/usr/bin/rhythmbox" in line:
- return commands.getoutput("rhythmbox-client --print-playing-format %ta"),commands.getoutput("rhythmbox-client --print-playing-format %at"),commands.getoutput("rhythmbox-client --print-playing-format %tt")
- return "","",""
- def size_image(width, height):
- image = Image.open(home + "/.album")
- image = image.resize((width, height))
- image.save(home + "/.album", "png")
- def reflect(width, height):
- image = Image.open(home + "/.album")
- flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
- final_image = Image.new('RGBA', (width, (height * 2) + 1) , (0, 0, 0, 0))
- gradient = Image.new('L', (1,255))
- for y in range(255, 0, -1):
- if y < 128:
- gradient.putpixel((0,y),255 - (y * 2))
- else:
- gradient.putpixel((0,255-y),0)
- alpha = gradient.resize(flipped_image.size)
- flipped_image.putalpha(alpha)
- final_image.paste(image, (0, 0))
- final_image.paste(flipped_image, (0, height + 1))
- final_image.save(home + "/.album", "png")
- parser = OptionParser()
- parser.add_option("-p", "--player", dest="player", default="moc", help="media player")
- parser.add_option("-s", "--size", dest="size", default="80x80", help="image size")
- parser.add_option("-r", "--reflect", action="store_true", dest="reflect", default=False, help="image reflection")
- (options, args) = parser.parse_args()
- try:
- width,height = options.size.split("x")
- width = int(width)
- height = int(height)
- except:
- parser.error("please specify size in WIDTHxHEIGHT format")
- if options.player in ["moc", "rhythmbox"]:
- artist, album, title = get_info(options.player)
- else:
- parser.error("player not supported")
- home = os.getenv("HOME")
- if artist == "" and album == "":
- if os.path.exists(home + "/.album"):
- os.remove(home + "/.album")
- if os.path.exists("/tmp/trackinfo"):
- os.remove("/tmp/trackinfo")
- elif os.path.exists("/tmp/trackinfo") and open("/tmp/trackinfo").read() == artist + album:
- pass
- else:
- url = "http://www.albumart.org/index.php?srchkey=" + urllib.quote_plus(artist) + "+" + urllib.quote_plus(album) + "&itempage=1&newsearch=1&searchindex=Music"
- albumart = urllib.urlopen(url).read()
- image = ""
- for line in albumart.split("\n"):
- if "http://www.albumart.org/images/zoom-icon.jpg" in line:
- image = line.partition('src="')[2].partition('"')[0]
- break
- if image:
- urllib.urlretrieve(image, home + "/.album")
- open("/tmp/trackinfo","w").write(artist + album)
- else:
- open("/tmp/trackinfo","w").write("album_not_found")
- shutil.copy(home + "/.noalbum", home + "/.album")
- size_image(width, height)
- if options.reflect:
- reflect(width, height)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.