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 19:18 (modification of post by view diff)
View followups from gg | download | new post

  1. #!/usr/bin/python
  2.  
  3. import commands
  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.             raise SystemExit()
  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. def check_album():
  57.     if artist == "" and album == "":
  58.         if os.path.exists(home + "/.album"):
  59.             os.remove(home + "/.album")
  60.         if os.path.exists("/tmp/trackinfo"):
  61.             os.remove("/tmp/trackinfo")
  62.         return False
  63.     elif os.path.exists("/tmp/trackinfo") and open("/tmp/trackinfo").read() == artist + album:
  64.         return False
  65.     return True
  66.    
  67. #fetch album
  68. def get_album():
  69.     api_album = api.get_album(album, artist)
  70.     if api_album.image["medium"]:
  71.         urllib.urlretrieve(api_album.image["medium"], home + "/.album")
  72.     elif api_album.image["small"]:
  73.         urllib.urlretrieve(api_album.image["small"], home + "/.album")
  74.     elif api_album.image["large"]:
  75.         urllib.urlretrieve(api_album.image["large"], home + "/.album")
  76.     elif api_album.image["extralarge"]:
  77.         urllib.urlretrieve(api_album.image["extralarge"], home + "/.album")
  78.     else:
  79.         commands.getoutput("cp %s %s" % (home + "/.noalbum", home + "/.album"))
  80.     open("/tmp/trackinfo","w").write(artist + album)
  81.     size_image(width, height, home + album_path)
  82.     if options.reflect:
  83.         reflect(width, height, home + album_path)
  84.    
  85. def check_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.         return False
  92.     elif os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() == artist:
  93.         return False
  94.     return True
  95.          
  96. #fetch artist art
  97. def get_artist_art():
  98.     api_artist = api.get_artist(artist)
  99.     if api_artist.image["medium"]:
  100.         urllib.urlretrieve(api_artist.image["medium"], home + "/.artist")
  101.     elif api_artist.image["small"]:
  102.         urllib.urlretrieve(api_artist.image["small"], home + "/.artist")
  103.     elif api_artist.image["large"]:
  104.         urllib.urlretrieve(api_artist.image["large"], home + "/.artist")
  105.     elif api_artist.image["extralarge"]:
  106.         urllib.urlretrieve(api_artist.image["extralarge"], home + "/.artist")
  107.     else:
  108.         commands.getoutput("cp %s %s" % (home + "/.noalbum", home + "/.artist"))
  109.     open("/tmp/artistinfo","w").write(artist)
  110.     size_image(width, height, home + artist_path)
  111.     if options.reflect:
  112.         reflect(width, height, home + artist_path)
  113.            
  114. #set up command line options                                       
  115. parser = OptionParser()
  116. parser.add_option("-p", "--player", dest="player", default="moc", help="media player")
  117. parser.add_option("-s", "--size", dest="size", default="80x80", help="image size")
  118. parser.add_option("-r", "--reflect", action="store_true", dest="reflect", default=False, help="image reflection")
  119. parser.add_option("-a", "--artist-art", action="store_true", dest="artist_art", default=False, help="artist image")
  120. parser.add_option("--artist", action="store_true", dest="return_artist", default=False, help="artist")
  121. parser.add_option("--album", action="store_true", dest="return_album", default=False, help="album")
  122. parser.add_option("--title", action="store_true", dest="return_title", default=False, help="title")
  123. (options, args) = parser.parse_args()
  124.  
  125. #check if size is valid
  126. try:
  127.     width,height = options.size.split("x")
  128.     width = int(width)
  129.     height = int(height)
  130. except:
  131.     parser.error("please specify size in WIDTHxHEIGHT format")
  132.  
  133. #check if player requested is supported   
  134. if options.player in ["moc", "rhythmbox", "mpd"]:
  135.     artist, album, title = get_info(options.player)
  136. else:
  137.     parser.error("player not supported")
  138.  
  139. #return artis, album, or title
  140. if options.return_artist:
  141.     print artist
  142.     raise SystemExit()
  143. if options.return_album:
  144.     print album
  145.     raise SystemExit()
  146. if options.return_title:
  147.     print title
  148.     raise SystemExit()
  149.  
  150.  
  151. import Image, os
  152. #set up variables
  153. home = os.getenv("HOME")
  154. album_path = "/.album"
  155. artist_path = "/.artist"
  156. api_key = "b25b959554ed76058ac220b7b2e0a026"
  157.  
  158. if check_artist_art() or check_album():
  159.     import urllib, lastfm
  160.     api = lastfm.Api(api_key)
  161.  
  162. if options.artist_art and check_artist_art():
  163.     get_artist_art()
  164. if check_album():
  165.     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