def process_dir(self, args, dirname, filenames): """ callback for each directory encourted by os.path.walk. If directory contains audio files, attempt to extract it's metatags, then search and download it's cover art""" album_name = "" artist_name = "" filehandler = None # If we have files in the directory if filenames: filehandler = HandlerFactory.get_handler(dirname, filenames) # get proper Handler class based on extension of files # If we have a file handler, then continue if filehandler: # If the directory actually contains media files then continue if filehandler.audio_files: (album_name, artist_name) = filehandler.get_album_and_artist() # Lookup album and artist ID3 tag # If metadata/tags exists and we have an album name if album_name: cover_exists = self.check_cover_image_existence(dirname) # Does cover image already exist in the current directory? if cover_exists == False: image_url = self.get_image_url(album_name, artist_name) else: logger.warning(u'cover image for "{artist_name} - {album_name}" already exists, moving on to the next one'.format(artist_name=artist_name, album_name=album_name)) image_url = None # If we found the image URL, then download the image. if image_url: logger.info(u'Downloading album cover image for "{artist_name} - {album_name}"'.format(artist_name=artist_name, album_name=album_name)) self.download_image(dirname, image_url)
def search_for_image(self): """ Use LastFM's API to obtain a URL for the album cover art """ logger.info(u'LastFM: Searching for "{artist_name} - {album_name}"'.format(artist_name=self.artist_name, album_name=self.album_name)) response = urllib.urlopen(self.url).read() # Send HTTP request to LastFM xml_data = ETree.fromstring(response) # Read in XML data for element in xml_data.getiterator("album"): if (element.find('artist').text.lower() == self.artist_name.lower().encode("utf-8")): for elmnt in element.findall('image'): if (elmnt.attrib['size'] == 'extralarge'): url = elmnt.text if url: return url else: return None
def do_walk_path(self): """ Walk specified directory recursively. Call self.process_dir() on each directory """ logger.info(u'Scanning {path}'.format(path=self.path)) os.path.walk(self.path, self.process_dir, None)