<s> import urllib2 <EOL> import xml . dom . minidom <EOL> import os <EOL> import logging <EOL> import re <EOL> import hashlib <EOL> import hmac <EOL> import base64 <EOL> import datetime <EOL> from pylons import app_globals as g <EOL> class NoArtError ( Exception ) : <EOL> pass <EOL> class NoArtOnDisk ( Exception ) : <EOL> pass <EOL> class AlbumArt : <EOL> amazonurl = None <EOL> imgurl = None <EOL> logger = None <EOL> def __init__ ( self ) : <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . www_root = '<STR_LIT>' <EOL> self . disk_root = '<STR_LIT>' <EOL> def album_fetch ( self , artist , album ) : <EOL> """<STR_LIT>""" <EOL> self . artist = artist <EOL> self . album = album <EOL> disc_num_found = re . search ( '<STR_LIT>' , self . album , re . IGNORECASE ) <EOL> if disc_num_found : <EOL> self . album = self . album [ : disc_num_found . start ( ) - <NUM_LIT:1> ] <EOL> self . set_file_paths ( ) <EOL> try : <EOL> self . check_disk ( ) <EOL> except NoArtOnDisk : <EOL> self . amazon_fetch ( ) <EOL> def artist_art ( self , artist ) : <EOL> """<STR_LIT>""" <EOL> images = [ ] <EOL> filenames = [ filename for filename in os . listdir ( self . disk_root ) if filename . startswith ( "<STR_LIT>" % artist ) ] <EOL> for i in filenames : <EOL> album_name = i . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] [ : - <NUM_LIT:4> ] <EOL> images . append ( { <EOL> '<STR_LIT>' : album_name , <EOL> '<STR_LIT>' : "<STR_LIT>" % ( self . www_root , i ) <EOL> } ) <EOL> return images <EOL> def log ( self , msg ) : <EOL> self . logger . info ( msg ) <EOL> def amazon_fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> if g . tc . awskey == '<STR_LIT>' : <EOL> raise NoArtError <EOL> artist_safe = urllib2 . quote ( self . artist ) <EOL> album_safe = urllib2 . quote ( self . album ) <EOL> urls = [ ] <EOL> date = datetime . datetime . utcnow ( ) <EOL> date = date . replace ( microsecond = <NUM_LIT:0> ) <EOL> timestamp = date . isoformat ( ) <EOL> query_string = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : g . tc . awskey , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : artist_safe , <EOL> '<STR_LIT>' : album_safe , <EOL> '<STR_LIT>' : timestamp + '<STR_LIT>' } <EOL> query_string_sorted = '<STR_LIT:&>' . join ( [ '<STR_LIT:=>' . join ( ( k , query_string [ k ] ) ) for k in sorted ( query_string . iterkeys ( ) ) ] ) <EOL> urls . append ( { '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : query_string_sorted . replace ( '<STR_LIT::>' , '<STR_LIT>' ) } ) <EOL> del query_string [ '<STR_LIT>' ] <EOL> query_string_sorted = '<STR_LIT:&>' . join ( [ '<STR_LIT:=>' . join ( ( k , query_string [ k ] ) ) for k in sorted ( query_string . iterkeys ( ) ) ] ) <EOL> urls . append ( { '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : query_string_sorted . replace ( '<STR_LIT::>' , '<STR_LIT>' ) } ) <EOL> for url in urls : <EOL> encode_string = '<STR_LIT:\n>' . join ( ( url [ '<STR_LIT>' ] , url [ '<STR_LIT:host>' ] , url [ '<STR_LIT>' ] , url [ '<STR_LIT>' ] ) ) <EOL> h = hmac . new ( str ( g . tc . aws_secret ) , str ( encode_string ) , hashlib . sha256 ) <EOL> hmac_string = h . digest ( ) <EOL> signature = base64 . b64encode ( hmac_string ) . replace ( '<STR_LIT:+>' , '<STR_LIT>' ) . replace ( '<STR_LIT:=>' , '<STR_LIT>' ) <EOL> real_url = url [ '<STR_LIT>' ] + url [ '<STR_LIT:host>' ] + url [ '<STR_LIT>' ] + '<STR_LIT:?>' + url [ '<STR_LIT>' ] + '<STR_LIT>' % signature <EOL> try : <EOL> self . log ( '<STR_LIT>' % real_url ) <EOL> urlfile = urllib2 . urlopen ( real_url ) <EOL> except urllib2 . URLError : <EOL> self . log ( '<STR_LIT>' ) <EOL> raise NoArtError <EOL> doc = xml . dom . minidom . parse ( urlfile ) <EOL> urlfile . close ( ) <EOL> imgnodes = doc . getElementsByTagName ( '<STR_LIT>' ) <EOL> if len ( imgnodes ) > <NUM_LIT:0> : <EOL> node = imgnodes [ <NUM_LIT:0> ] <EOL> self . amazonurl = node . firstChild . firstChild . nodeValue <EOL> self . log ( '<STR_LIT>' % self . amazonurl ) <EOL> break <EOL> if not self . amazonurl : <EOL> raise NoArtError <EOL> self . save_to_disk ( ) <EOL> def set_file_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> artist_pathsafe = self . artist . replace ( os . sep , '<STR_LIT:U+0020>' ) <EOL> album_pathsafe = self . album . replace ( os . sep , '<STR_LIT:U+0020>' ) <EOL> filename = "<STR_LIT>" % ( artist_pathsafe , album_pathsafe ) <EOL> self . www_path = os . path . join ( self . www_root , filename ) <EOL> self . disk_path = os . path . join ( self . disk_root , filename ) <EOL> def check_disk ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( self . disk_path ) : <EOL> self . imgurl = self . www_path <EOL> else : <EOL> raise NoArtOnDisk <EOL> def save_to_disk ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> urlfile = urllib2 . urlopen ( self . amazonurl ) <EOL> except urllib2 . URLError : <EOL> raise NoArtError <EOL> f = open ( self . disk_path , '<STR_LIT:wb>' ) <EOL> f . write ( urlfile . read ( ) ) <EOL> f . close ( ) <EOL> self . imgurl = self . www_path <EOL> def dir_size ( self ) : <EOL> """<STR_LIT>""" <EOL> dir_size = <NUM_LIT:0> <EOL> for ( path , dirs , files ) in os . walk ( self . disk_root ) : <EOL> for file in files : <EOL> filename = os . path . join ( path , file ) <EOL> dir_size += os . path . getsize ( filename ) <EOL> return dir_size </s>
