lswest

mpd-cover

Mar 5th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import shutil
  5. import subprocess
  6. import urllib.request, urllib.parse, urllib.error
  7.  
  8. def copycover(currentalbum, src, dest, defaultfile):
  9.     searchstring = currentalbum.replace(" ", "+")
  10.     if not os.path.exists(src):
  11.         url = "http://www.albumart.org/index.php?srchkey=" + searchstring + "&itempage=1&newsearch=1&searchindex=Music"
  12.         cover = urllib.request.urlopen(url).read()
  13.         image = ""
  14.         for line in cover.decode("utf-8").split("\n"):
  15.             if "http://www.albumart.org/images/zoom-icon.jpg" in line:
  16.                 image = line.partition('src="')[2].partition('"')[0]
  17.                 break
  18.         if image:
  19.             urllib.request.urlretrieve(image, src)
  20.     if os.path.exists(src):
  21.         shutil.copy(src, dest)
  22.     elif os.path.exists(defaultfile):
  23.         shutil.copy(defaultfile, dest)
  24.     else:
  25.         pass
  26.  
  27. # Path where the images are saved
  28. imgpath = os.getenv("HOME") + "/.covers/"
  29.  
  30. # image displayed when no image found
  31. noimg = imgpath + "nocover.png"
  32.  
  33. # Cover displayed by conky
  34. cover = "/tmp/cover"
  35.  
  36. # Name of current album
  37. album = subprocess.getoutput("mpc --format %artist%+%album% | head -n 1")
  38.  
  39. # If tags are empty, use noimg.
  40. try:
  41.     if album == "":
  42.         if os.path.exists(conkycover):
  43.             os.remove(conkycover)
  44.         if os.path.exists(noimg):
  45.             shutil.copy(noimg, conkycover)
  46.         else:
  47.             print("Image not found!")
  48.     else:
  49.  
  50.         filename = imgpath + album + ".jpg"
  51.         if os.path.exists("/tmp/nowplaying") and os.path.exists("/tmp/cover"):
  52.             nowplaying = open("/tmp/nowplaying").read()
  53.             if nowplaying == album:
  54.                 pass
  55.             else:
  56.                 copycover(album, filename, cover, noimg)
  57.                 open("/tmp/nowplaying", "w").write(album)
  58.         else:
  59.             copycover(album, filename, cover, noimg)
  60.             open("/tmp/nowplaying", "w").write(album)
  61. except:
  62.      pass
Add Comment
Please, Sign In to add comment