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