Skip to content
Snippets Groups Projects
Commit 4f3d9374 authored by ale's avatar ale
Browse files

Merge branch 'master' of https://git.autistici.org/djrandom

parents 78aec21c fe6c8c0f
Branches
No related tags found
No related merge requests found
import hashlib
import os
import re
import subprocess
import urllib
import urllib2
......@@ -12,6 +13,12 @@ class AlbumImageDiskCache(object):
Files are saved and converted to JPEG using Imagemagick. Negative
matches are saved as empty files.
If you want to periodically retry 'missed' entries (to recover from
temporary errors, for example), you can simply run:
find $DIR -type f -size 0 -mtime +$DAYS -exec rm -f \{\} +
"""
def __init__(self, root):
......@@ -61,16 +68,25 @@ class AlbumImageRetriever(object):
def get_album_image(self, artist, album):
if not self.cache.has(artist, album):
try:
xml = self._get_album_info(artist, album)
xp = etree.XPath('album/image[@size="extralarge"]')
img = xp(xml)
if img:
self.cache.download(artist, album, img[0].text)
else:
self.cache.set_negative_match(artist, album)
except:
return None
queries = [(artist, album)]
# Fix a minor annoyance that is popular in ID3 tags: if the
# album name ends in a number, it might be part of a series;
# in that case, try again without the number.
m = re.search(r'^(.+) \d+$', album)
if m:
queries.append((artist, m.group(1)))
for query_artist, query_album in queries:
try:
xml = self._get_album_info(query_artist, query_album)
xp = etree.XPath('album/image[@size="extralarge"]')
img = xp(xml)
if img:
self.cache.download(artist, album, img[0].text)
else:
self.cache.set_negative_match(artist, album)
except:
continue
break
return self.cache.get(artist, album)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment