コード例 #1
0
    def download_image(self, dirname, image_url):
        """ Check if overwrite is enabled.  Check if album cover already exists
        Call method to download album cover art image to specified directory"""

        # Set name of image file based on extension from image URL
        if ".png" in image_url.lower():
            cover_name = "cover.png"
        elif ".jpg" in image_url.lower():
            cover_name = "cover.jpg"
        elif ".jpeg" in image_url.lower():
            cover_name = "cover.jpg"
        elif ".gif" in image_url.lower():
            cover_name = "cover.gif"
        elif ".tif" in image_url.lower():
            cover_name = "cover.tiff"
        elif ".tiff" in image_url.lower():
            cover_name = "cover.tiff"
        elif ".svg" in image_url.lower():
            cover_name = "cover.svg"
        else:
            return

        # Does cover.(png|jpg|gif|tiff|svg) already exist?  
        if os.path.exists(os.path.join(dirname, cover_name)):
            # If overwrite is set to True, then go ahead and re-download album cover
            if self.overwrite:
                self.do_download(dirname, image_url, cover_name)
            else:
                logger.warning(u'Cover ({covername}) already exists in {dir_name}'.format(covername=cover_name, dir_name = dirname))
        else:
            # If cover doesn't exist, go ahead and download
            self.do_download(dirname, image_url, cover_name)
コード例 #2
0
    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)