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 pvsage on Sat 21st Nov 03:28 (modification of post by pvsage view diff)
diff | download | new post

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