CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by iggykoopa on Tue 4th Aug 16:21 (modification of post by view diff)
download | new post

  1. #!/usr/bin/python
  2.  
  3. import os, commands, Image, sys, types, shutil, urllib, lastfm
  4. from optparse import OptionParser
  5.  
  6. #retrieve track info from player
  7. def get_info(player):
  8.     check_running = commands.getoutput("ps aux")
  9.     check_running = check_running.split("\n")
  10.     if player == "moc":
  11.         for line in check_running:
  12.             if "mocp" in line:
  13.                 return commands.getoutput("mocp -Q %artist"),commands.getoutput("mocp -Q %album"),commands.getoutput("mocp -Q %song")
  14.         return "","",""
  15.     if player == "rhythmbox":     
  16.         for line in check_running:
  17.             if "/usr/bin/rhythmbox" in line:
  18.                 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")
  19.         return "","",""
  20.     if player == "mpd":
  21.         try:
  22.             import mpdclient2
  23.         except:
  24.             print "please install python-mpdclient"
  25.             sys.exit()
  26.         mpd = mpdclient2.connect()
  27.         song = mpd.currentsong()
  28.         if song:
  29.             return song.artist, song.album, song.title
  30.         else:
  31.             return "","",""
  32.  
  33. #resize image
  34. def size_image(width, height, path):
  35.     image = Image.open(path)
  36.     image = image.resize((width, height))
  37.     image.save(path, "png")
  38.  
  39. #add reflection to image   
  40. def reflect(width, height, path):
  41.     image = Image.open(path)
  42.     flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
  43.     final_image = Image.new('RGBA', (width, (height * 2) + 1) , (0, 0, 0, 0))
  44.     gradient = Image.new('L', (1,255))
  45.     for y in range(255, 0, -1):
  46.         if y < 128:
  47.             gradient.putpixel((0,y),255 - (y * 2))
  48.         else:
  49.             gradient.putpixel((0,255-y),0)
  50.     alpha = gradient.resize(flipped_image.size)
  51.     flipped_image.putalpha(alpha)
  52.     final_image.paste(image, (0, 0))
  53.     final_image.paste(flipped_image, (0, height + 1))
  54.     final_image.save(path, "png")
  55.    
  56. #fetch album
  57. def get_album():
  58.     #if no artist and title remove album art
  59.     if artist == "" and album == "":
  60.         if os.path.exists(home + "/.album"):
  61.             os.remove(home + "/.album")
  62.         if os.path.exists("/tmp/trackinfo"):
  63.             os.remove("/tmp/trackinfo")
  64.     elif os.path.exists("/tmp/trackinfo") and open("/tmp/trackinfo").read() == artist + album:
  65.         pass
  66.     else:
  67.         api_album = api.get_album(album, artist)
  68.         if api_album.image["medium"]:
  69.             urllib.urlretrieve(api_album.image["medium"], home + "/.album")
  70.         elif api_album.image["small"]:
  71.             urllib.urlretrieve(api_album.image["small"], home + "/.album")
  72.         elif api_album.image["large"]:
  73.             urllib.urlretrieve(api_album.image["large"], home + "/.album")
  74.         elif api_album.image["extralarge"]:
  75.             urllib.urlretrieve(api_album.image["extralarge"], home + "/.album")
  76.         else:
  77.             shutil.copy(home + "/.noalbum", home + "/.album")
  78.         open("/tmp/trackinfo","w").write(artist + album)
  79.         size_image(width, height, home + album_path)
  80.         if options.reflect:
  81.             reflect(width, height, home + album_path)
  82.            
  83. #fetch artist art
  84. def get_artist_art():
  85.     #if no artist and title remove artist art
  86.     if artist == "":
  87.         if os.path.exists(home + "/.artist"):
  88.             os.remove(home + "/.artist")
  89.         if os.path.exists("/tmp/artistinfo"):
  90.             os.remove("/tmp/artistinfo")
  91.     elif os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() == artist:
  92.         pass
  93.     else:
  94.         api_artist = api.get_artist(artist)
  95.         if api_artist.image["medium"]:
  96.             urllib.urlretrieve(api_artist.image["medium"], home + "/.artist")
  97.         elif api_artist.image["small"]:
  98.             urllib.urlretrieve(api_artist.image["small"], home + "/.artist")
  99.         elif api_artist.image["large"]:
  100.             urllib.urlretrieve(api_artist.image["large"], home + "/.artist")
  101.         elif api_artist.image["extralarge"]:
  102.             urllib.urlretrieve(api_artist.image["extralarge"], home + "/.artist")
  103.         else:
  104.             shutil.copy(home + "/.noalbum", home + "/.artist")
  105.         open("/tmp/artistinfo","w").write(artist)
  106.         size_image(width, height, home + artist_path)
  107.         if options.reflect:
  108.             reflect(width, height, home + artist_path)
  109.            
  110. #set up command line options                                       
  111. parser = OptionParser()
  112. parser.add_option("-p", "--player", dest="player", default="moc", help="media player")
  113. parser.add_option("-s", "--size", dest="size", default="80x80", help="image size")
  114. parser.add_option("-r", "--reflect", action="store_true", dest="reflect", default=False, help="image reflection")
  115. parser.add_option("-a", "--artist-art", action="store_true", dest="artist_art", default=False, help="artist image")
  116. parser.add_option("--artist", action="store_true", dest="return_artist", default=False, help="artist")
  117. parser.add_option("--album", action="store_true", dest="return_album", default=False, help="album")
  118. parser.add_option("--title", action="store_true", dest="return_title", default=False, help="title")
  119. (options, args) = parser.parse_args()
  120.  
  121. #check if size is valid
  122. try:
  123.     width,height = options.size.split("x")
  124.     width = int(width)
  125.     height = int(height)
  126. except:
  127.     parser.error("please specify size in WIDTHxHEIGHT format")
  128.  
  129. #check if player requested is supported   
  130. if options.player in ["moc", "rhythmbox", "mpd"]:
  131.     artist, album, title = get_info(options.player)
  132. else:
  133.     parser.error("player not supported")
  134.  
  135. #return artis, album, or title
  136. if options.return_artist:
  137.     print artist
  138.     sys.exit()
  139. if options.return_album:
  140.     print album
  141.     sys.exit()
  142. if options.return_title:
  143.     print title
  144.     sys.exit()
  145.  
  146. #set up variables
  147. home = os.getenv("HOME")
  148. album_path = "/.album"
  149. artist_path = "/.artist"
  150. api_key = "b25b959554ed76058ac220b7b2e0a026"
  151. api = lastfm.Api(api_key)
  152.  
  153. if options.artist_art:
  154.     get_artist_art()
  155. 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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me