-
Notifications
You must be signed in to change notification settings - Fork 7
/
coverart.py
59 lines (51 loc) · 1.76 KB
/
coverart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import os
import shutil
import commands
import urllib
def copycover(currentalbum, src, dest, defaultfile):
searchstring = currentalbum.replace(" ", "+")
if not os.path.exists(src):
url = "http://www.albumart.org/index.php?srchkey=" + searchstring + "&itempage=1&newsearch=1&searchindex=Music"
cover = urllib.urlopen(url).read()
image = ""
for line in cover.split("\n"):
if "http://www.albumart.org/images/zoom-icon.jpg" in line:
image = line.partition('src="')[2].partition('"')[0]
break
if image:
urllib.urlretrieve(image, src)
if os.path.exists(src):
shutil.copy(src, dest)
elif os.path.exists(defaultfile):
shutil.copy(defaultfile, dest)
else:
print "Image not found!"
# Path where the images are saved
imgpath = os.getenv("HOME") + "/.covers/"
# image displayed when no image found
noimg = imgpath + "nocover.png"
# Cover displayed by conky
cover = "/tmp/cover"
# Name of current album
album = commands.getoutput("mpc --format %artist%+%album% | head -n 1")
# If tags are empty, use noimg.
if album == "":
if os.path.exists(conkycover):
os.remove(conkycover)
if os.path.exists(noimg):
shutil.copy(noimg, conkycover)
else:
print "Image not found!"
else:
filename = imgpath + album + ".jpg"
if os.path.exists("/tmp/nowplaying") and os.path.exists("/tmp/cover"):
nowplaying = open("/tmp/nowplaying").read()
if nowplaying == album:
pass
else:
copycover(album, filename, cover, noimg)
open("/tmp/nowplaying", "w").write(album)
else:
copycover(album, filename, cover, noimg)
open("/tmp/nowplaying", "w").write(album)