Posted by iggykoopa on Tue 4th Aug 16:21 (modification of post by view diff)
download | new post
- #!/usr/bin/python
- import os, commands, Image, sys, types, shutil, urllib, lastfm
- from optparse import OptionParser
- #retrieve track info from player
- 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 "","",""
- if player == "mpd":
- try:
- import mpdclient2
- except:
- print "please install python-mpdclient"
- sys.exit()
- mpd = mpdclient2.connect()
- song = mpd.currentsong()
- if song:
- return song.artist, song.album, song.title
- else:
- return "","",""
- #resize image
- def size_image(width, height, path):
- image = Image.open(path)
- image = image.resize((width, height))
- image.save(path, "png")
- #add reflection to image
- def reflect(width, height, path):
- image = Image.open(path)
- 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(path, "png")
- #fetch album
- def get_album():
- #if no artist and title remove album art
- 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:
- api_album = api.get_album(album, artist)
- if api_album.image["medium"]:
- urllib.urlretrieve(api_album.image["medium"], home + "/.album")
- elif api_album.image["small"]:
- urllib.urlretrieve(api_album.image["small"], home + "/.album")
- elif api_album.image["large"]:
- urllib.urlretrieve(api_album.image["large"], home + "/.album")
- elif api_album.image["extralarge"]:
- urllib.urlretrieve(api_album.image["extralarge"], home + "/.album")
- else:
- shutil.copy(home + "/.noalbum", home + "/.album")
- open("/tmp/trackinfo","w").write(artist + album)
- size_image(width, height, home + album_path)
- if options.reflect:
- reflect(width, height, home + album_path)
- #fetch artist art
- def get_artist_art():
- #if no artist and title remove artist art
- if artist == "":
- if os.path.exists(home + "/.artist"):
- os.remove(home + "/.artist")
- if os.path.exists("/tmp/artistinfo"):
- os.remove("/tmp/artistinfo")
- elif os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() == artist:
- pass
- else:
- api_artist = api.get_artist(artist)
- if api_artist.image["medium"]:
- urllib.urlretrieve(api_artist.image["medium"], home + "/.artist")
- elif api_artist.image["small"]:
- urllib.urlretrieve(api_artist.image["small"], home + "/.artist")
- elif api_artist.image["large"]:
- urllib.urlretrieve(api_artist.image["large"], home + "/.artist")
- elif api_artist.image["extralarge"]:
- urllib.urlretrieve(api_artist.image["extralarge"], home + "/.artist")
- else:
- shutil.copy(home + "/.noalbum", home + "/.artist")
- open("/tmp/artistinfo","w").write(artist)
- size_image(width, height, home + artist_path)
- if options.reflect:
- reflect(width, height, home + artist_path)
- #set up command line options
- 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")
- parser.add_option("-a", "--artist-art", action="store_true", dest="artist_art", default=False, help="artist image")
- parser.add_option("--artist", action="store_true", dest="return_artist", default=False, help="artist")
- parser.add_option("--album", action="store_true", dest="return_album", default=False, help="album")
- parser.add_option("--title", action="store_true", dest="return_title", default=False, help="title")
- (options, args) = parser.parse_args()
- #check if size is valid
- try:
- width,height = options.size.split("x")
- width = int(width)
- height = int(height)
- except:
- parser.error("please specify size in WIDTHxHEIGHT format")
- #check if player requested is supported
- if options.player in ["moc", "rhythmbox", "mpd"]:
- artist, album, title = get_info(options.player)
- else:
- parser.error("player not supported")
- #return artis, album, or title
- if options.return_artist:
- print artist
- sys.exit()
- if options.return_album:
- print album
- sys.exit()
- if options.return_title:
- print title
- sys.exit()
- #set up variables
- home = os.getenv("HOME")
- album_path = "/.album"
- artist_path = "/.artist"
- api_key = "b25b959554ed76058ac220b7b2e0a026"
- api = lastfm.Api(api_key)
- if options.artist_art:
- get_artist_art()
- get_album()
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.