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