Script for å hente album art til album basert på mappestrukturen ([artist]/[album])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os, re, sys | |
from urllib import FancyURLopener | |
class _StealthOpener(FancyURLopener): | |
version = 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) \ | |
Gecko/20120827 Firefox/10.0.7' | |
def _last_fm(artist, album, opener, directory): | |
suffix = '+'.join(artist.split()) + '/' + '+'.join(album.split()) | |
for line in opener.open('http://www.last.fm/music/' + suffix): | |
m = re.findall(r'<img src="(http://userserve\-ak\.last\.fm/' + | |
'serve/.*\.((png)|(jpg)))" class="album\-cover"', line) | |
if m: break | |
if m: | |
print 'Fetching ' + m[0][0] | |
os.system('curl -s -A \'' + opener.version + '\' -o "' + directory | |
+ '/album.' + m[0][1] + '"' + ' ' + m[0][0]) | |
return True | |
return False | |
def _seekacover(artist, album, opener, directory): | |
suffix = '+'.join(artist.split()) + '+' + '+'.join(album.split()) | |
for line in opener.open('http://www.seekacover.com/cd/' + suffix): | |
m = re.findall(r'http://ecx\.images\-amazon\.com/images/.*L\.jpg', line) | |
if m: break | |
if m: | |
print 'Fetching ' + m[0] | |
os.system('curl -s -A \'' + opener.version + '\' -o "' + directory | |
+ '/album.jpg"' + ' ' + m[0]) | |
return True | |
return False | |
def _get_art(args, directory, names): | |
if directory == None: return | |
elif directory == 0: directory = os.getcwd() | |
if not '.wav' in str(names): return | |
artist, album = directory.split(os.sep)[-2:] | |
print '\nProcessing \'' + artist + ' - ' + album + '\'...' | |
for ext in ['.jpg', '.png', '.JPG', '.PNG', '.gif', '.GIF']: | |
if ext in str(names): | |
print 'Album art already present.' | |
return | |
artist, album = artist.lower(), album.lower() | |
opener = _StealthOpener() | |
while True: | |
album = re.sub(r'\(.+\)|\[.+\]|\-|&|!|\?|\'|\.|:', '', album).strip() | |
artist = re.sub(r' &|\'|!|\?', r'', artist).strip() | |
if args['lfm']: | |
if _last_fm(artist, album, opener, directory): return | |
print 'Could not find a match for album \'' + album + '\'.' | |
print 'Trying seekacover.com...' | |
if _seekacover(artist, album, opener, directory): return | |
print 'seekacover yielded no result.' | |
else: | |
if _seekacover(artist, album, opener, directory): return | |
print 'Could not find a match for album \'' + album + '\'.' | |
print 'Trying last.fm...' | |
if _last_fm(artist, album, opener, directory): return | |
print 'last.fm yielded no result.' | |
if not args['ask_input']: return | |
print 'Please specify artist and album to try again, ' + \ | |
'or type \'s\' to skip or \'w\' to switch image provider.' | |
in1 = raw_input('artist [' + artist + ']: ') | |
if in1.lower() == 'w': | |
args['lfm'] = not args['lfm'] | |
if args['lfm']: print 'Switched to last.fm.' | |
else: print 'Switched to seekacover.com.' | |
continue | |
elif in1.lower() == 's': | |
return | |
in2 = raw_input('album [' + album + ']: ') | |
if in1 != '': artist = in1 | |
if in2 != '': album = in2 | |
if __name__ == '__main__': | |
inp, r = {'ask_input': True, 'lfm': False}, False | |
if '-r' in sys.argv: recursive = True | |
if '-s' in sys.argv: inp['ask_input'] = False | |
if '-l' in sys.argv: inp['lfm'] = True | |
if recursive: os.path.walk(os.getcwd(), _get_art, inp) | |
else: _get_art(True, 0, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment