def _logger_provider_error(exception, name): """Show error if providers throw an error""" logger.debug('{}'.format(exception)) logger.error( "Something went wrong with {}. The program will continue with" "the other providers. Please check '{}' for more details.\ ".format(name, logger.get_log_file()))
def make_custom_dir(DIR, TRACK_INFO): """If the dirname has $ in it then we need to make them. The DIR is probably in the format of keyword->keyword->keyword """ pos = DIR.index('$') # base_DIR is where the folders will be made base_DIR = DIR[:pos] remaining = DIR[pos + 1:] order_dir = seperate_kw(remaining) # The last element is to be returned and not considered as # a folder last_element = order_dir[-1] # Replace [] from it if last_element[0] == '[' and last_element[-1] == ']': last_element = last_element.replace('[', '') last_element = last_element.replace(']', '') order_dir[-1] = last_element order_dir = ret_proper_names(order_dir) last_element = order_dir[-1] else: last_element = None order_dir = ret_proper_names(order_dir) logger.debug(TRACK_INFO) if last_element is not None: last_element = getattr(TRACK_INFO, last_element) order_dir = order_dir[:len(order_dir) - 1] for kw_name in order_dir: dir_name = unescape(getattr(TRACK_INFO, kw_name)) # Sometimes, certain strings have / in the name which creates # issues since those strings are used to create directories. # Whenever there is a /, replace it with - dir_name = dir_name.replace("/", "-") new_dir = os.path.join(base_DIR, dir_name) # Make the dir only if it doesn't already exist if not os.path.isdir(new_dir): os.mkdir(new_dir) # Now make the new_dir base_DIR base_DIR = new_dir return (base_DIR, last_element)
def set_MP3_data(song, song_path): """ Set the meta data if the passed data is mp3. """ # A variable to see if cover image was added. IS_IMG_ADDED = False try: SONG_PATH = os.path.join(defaults.DEFAULT.SONG_TEMP_DIR, song_path) audio = MP3(SONG_PATH, ID3=ID3) data = ID3(SONG_PATH) # Download the cover image, if failed, pass if dwCover(song): imagedata = open(defaults.DEFAULT.COVER_IMG, 'rb').read() data.add(APIC(3, 'image/jpeg', 3, 'Front cover', imagedata)) # REmove the image os.remove(defaults.DEFAULT.COVER_IMG) IS_IMG_ADDED = True # If tags are not present then add them try: audio.add_tags() except Exception: pass audio.save() data.add(TYER(encoding=3, text=song.release_date)) data.add(TIT2(encoding=3, text=song.track_name)) data.add(TPE1(encoding=3, text=song.artist_name)) data.add(TALB(encoding=3, text=song.collection_name)) data.add(TCON(encoding=3, text=song.primary_genre_name)) data.add(TRCK(encoding=3, text=str(song.track_number))) data.save() defaults.DEFAULT.SONG_NAME_TO_SAVE = song.track_name + '.mp3' # Rename the downloaded file os.rename( SONG_PATH, os.path.join(defaults.DEFAULT.SONG_TEMP_DIR, defaults.DEFAULT.SONG_NAME_TO_SAVE)) return IS_IMG_ADDED except Exception as e: logger.debug("{}".format(e)) return e, False
def _delete_cached_songs(ext='mp3'): """Delete cached songs""" # We need to call this after song is moved # because otherwise if there is an error along the way # next time a wrong song may be copied. SONGS_PATH = os.path.join(defaults.DEFAULT.SONG_TEMP_DIR, '*{}'.format(ext)) deleted = False for song in glob.glob(SONGS_PATH): deleted = True os.remove(song) logger.debug('Removed "{}" from cache'.format(os.path.basename(song))) if deleted: logger.debug('{}'.format('Deleted cached songs'))
def ret_proper_names(ordered_names): """Return a list with the names changed to itunespy supported ones. For eg: Artist to artist_name """ info_dict = { 'Artist': 'artist_name', 'Title': 'track_name', 'Album': 'collection_name', 'Genre': 'primary_genre_name', 'TrackNumber': 'track_number', 'ReleaseDate': 'release_date' } logger.debug(ordered_names) logger.debug(info_dict) new_names = [] for name in ordered_names: new_names.append(info_dict.get(name)) return new_names
def dry_cleanup(current_path, passed_name): """ Move the song from the current path to the song dir and change the name to the passed_name. This is only for when the meta-skip option is passed, in which case the song needs to be moved from the cache to the user directory. """ try: extension = os.path.basename(current_path).split(".")[-1] logger.debug("ext: {}".format(extension)) new_basename = "{}.{}".format(passed_name, extension) DEST = defaults.DEFAULT.SONG_DIR logger.debug("Moving to: {}".format(DEST)) shutil.move(current_path, os.path.join(DEST, new_basename)) logger.info('Moved to {}...'.format(DEST)) return True except Exception as e: logger.critical("{}".format(e)) return False
def set_MP3_data(SONG_INFO, is_quiet, song_path, choice): """ Set the meta data if the passed data is mp3. """ # A variable to see if cover image was added. IS_IMG_ADDED = False try: # If more than one choice then call getChoice option = 0 if len(SONG_INFO) > 1: if not is_quiet: option = getChoice(SONG_INFO, 'metadata') elif choice is not None and choice in range(1, len(SONG_INFO)): option = choice SONG_PATH = os.path.join(defaults.DEFAULT.SONG_TEMP_DIR, song_path) audio = MP3(SONG_PATH, ID3=ID3) data = ID3(SONG_PATH) # Download the cover image, if failed, pass if dwCover(SONG_INFO, option): imagedata = open(defaults.DEFAULT.COVER_IMG, 'rb').read() data.add(APIC(3, 'image/jpeg', 3, 'Front cover', imagedata)) # REmove the image os.remove(defaults.DEFAULT.COVER_IMG) IS_IMG_ADDED = True # If tags are not present then add them try: audio.add_tags() except Exception: pass audio.save() option = int(option) data.add(TYER(encoding=3, text=SONG_INFO[option].release_date)) data.add(TIT2(encoding=3, text=SONG_INFO[option].track_name)) data.add(TPE1(encoding=3, text=SONG_INFO[option].artist_name)) data.add(TALB(encoding=3, text=SONG_INFO[option].collection_name)) data.add(TCON(encoding=3, text=SONG_INFO[option].primary_genre_name)) data.add(TRCK(encoding=3, text=str(SONG_INFO[option].track_number))) data.save() defaults.DEFAULT.SONG_NAME_TO_SAVE = SONG_INFO[ option].track_name + '.mp3' # Rename the downloaded file os.rename( SONG_PATH, os.path.join(defaults.DEFAULT.SONG_TEMP_DIR, defaults.DEFAULT.SONG_NAME_TO_SAVE)) return option, IS_IMG_ADDED except Exception as e: logger.debug("{}".format(e)) return e, False