| | | |
| 1 | 1 | | #!/usr/bin/python |
| 3 | 3 | | import os,commands, urllib, Image, ImageFont, ImageDraw |
| 5 | 5 | | def reflect(home, artist, album, title, found): |
| 6 | 6 | | final_image = Image.new('RGBA', (300, 160) , (0, 0, 0, 0)) |
| 7 | 7 | | mid_image = Image.new('RGBA', (300, 80) , (0, 0, 0, 0)) |
| 8 | 8 | | image_flipped = Image.new('RGBA', (300, 80) , (0, 0, 0, 0)) |
| 9 | 9 | | if found: |
| 10 | 10 | | image = Image.open(home + "/.album") |
| 11 | 11 | | else: |
| 12 | 12 | | image = Image.open(home + "/.noalbum") |
| 13 | 13 | | gradient = Image.new('L', (1,255)) |
| 14 | 14 | | for y in range(255, 0, -1): |
| 15 | 15 | | if y < 128: |
| 16 | 16 | | gradient.putpixel((0,y),255 - (y * 2)) |
| 17 | 17 | | else: |
| 18 | 18 | | gradient.putpixel((0,255-y),0) |
| 19 | 19 | | font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf",12) |
| 20 | 20 | | draw = ImageDraw.Draw(mid_image) |
| 21 | 21 | | draw.text((85, 30), artist, font=font) |
| 22 | 22 | | draw.text((85, 45), album, font=font) |
| 23 | 23 | | draw.text((85, 60), title, font=font) |
| 24 | 24 | | image = image.resize((80,80)) |
| 25 | 25 | | mid_image.paste(image, (0, 0)) |
| 26 | 26 | | image_flipped = mid_image.transpose(Image.FLIP_TOP_BOTTOM) |
| 27 | 27 | | alpha = gradient.resize(image_flipped.size) |
| 28 | 28 | | r,g,b,a = image_flipped.split() |
| 29 | 29 | | a_loaded = a.load() |
| 30 | 30 | | alpha_loaded = alpha.load() |
| 31 | 31 | | for x in range(300): |
| 32 | 32 | | for y in range(80): |
| 33 | 33 | | if a_loaded[x,y] < alpha_loaded[x,y]: |
| 34 | 34 | | alpha_loaded[x,y] = a_loaded[x,y] |
| 35 | 35 | | image_flipped.putalpha(alpha) |
| 36 | 36 | | final_image.paste(mid_image, (0, 0)) |
| 37 | 37 | | final_image.paste(image_flipped, (0, 80)) |
| 38 | 38 | | final_image.save(home + "/.album", "png") |
| 40 | 40 | | home = os.getenv("HOME") |
| 41 | 41 | | artist=commands.getoutput("mocp -Q %artist") |
| 42 | 42 | | album=commands.getoutput("mocp -Q %album") |
| 43 | 43 | | title=commands.getoutput("mocp -Q %song") |
| 44 | 44 | | if artist == "" and album == "": |
| 45 | 45 | | if os.path.exists(home + "/.album"): |
| 46 | 46 | | os.remove(home + "/.album") |
| 47 | 47 | | else: |
| 48 | 48 | | url = "http://www.albumart.org/index.php?srchkey=" + urllib.quote_plus(artist) + "+" + urllib.quote_plus(album) + "&itempage=1&newsearch=1&searchindex=Music" |
| 49 | 49 | | albumart = urllib.urlopen(url).read() |
| 50 | 50 | | image = "" |
| 51 | 51 | | for line in albumart.split("\n"): |
| 52 | 52 | | if "http://www.albumart.org/images/zoom-icon.jpg" in line: |
| 53 | 53 | | image = line.partition('src="')[2].partition('"')[0] |
| 54 | 54 | | break |
| 55 | 55 | | if image: |
| 56 | 56 | | if os.path.exists("/tmp/imagepath") and os.path.exists(home + "/.album"): |
| 57 | 57 | | imagepath = open("/tmp/imagepath").read() |
| 58 | | - | if imagepath == image: |
| 58 | + | if imagepath == artist + album + title: |
| 59 | 59 | | pass |
| 60 | 60 | | else: |
| 61 | 61 | | urllib.urlretrieve(image, home + "/.album") |
| 62 | 62 | | reflect(home, artist, album, title, 1) |
| 63 | 63 | | else: |
| 64 | 64 | | urllib.urlretrieve(image, home + "/.album") |
| 65 | 65 | | reflect(home, artist, album, title, 1) |
| 66 | | - | open("/tmp/imagepath","w").write(image) |
| 66 | + | open("/tmp/imagepath","w").write(artist + album + title) |
| 67 | 67 | | else: |
| 68 | 68 | | open("/tmp/imagepath","w").write("album_not_found") |
| 69 | 69 | | reflect(home, artist, album, title, 0) |