def test_tag_single_album_with_video_tracks(self): self.ogsrelid = "13748" # construct config with only default values tagger_config = TaggerConfig(os.path.join(parentdir, "test/empty.conf")) dummy_response = TestDummyResponse(self.ogsrelid) discogs_album = DummyDiscogsAlbum(dummy_response) self.album = discogs_album.map() # copy file to source directory and rename it self.copy_files_single_album(7) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() testTagHandler = TagHandler(self.album, self.tagger_config) testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_files() testTagHandler.tag_album() target_dir = os.path.join(self.target_dir, self.album.target_dir) metadata = MediaFile( os.path.join(target_dir, "01-coldcut-timber_(chopped_down_radio_edit).flac")) assert metadata.artist == "Coldcut" assert metadata.albumartists == ["Coldcut", "Hexstatic"] assert metadata.discogs_id == self.ogsrelid assert metadata.year == 1998 assert metadata.disctotal == 1 assert metadata.genre == "Electronic" assert metadata.tracktotal == 7 # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None assert metadata.country == "Canada" metadata = MediaFile( os.path.join( target_dir, "07-coldcut-timber_(the_cheech_wizards_polythump_requiem_for_the_ancient_forests_mix).flac" )) assert metadata.artist == "Coldcut" assert metadata.albumartists == ["Coldcut", "Hexstatic"] assert metadata.discogs_id == self.ogsrelid assert metadata.tracktotal == 7
def test_tag_single_track(self): shutil.copyfile(self.source_file, os.path.join(self.source_dir, self.target_file_name)) testTagHandler = TagHandler(self.album, self.tagger_config) self.album.disc(1).track(1).new_file = self.target_file_name testTagHandler.tag_single_track(self.source_dir, self.album.disc(1).track(1)) metadata = MediaFile( os.path.join(self.source_dir, self.target_file_name)) assert metadata.artist == "Gigi D'Agostino" assert len(metadata.albumartists) == 1 assert metadata.albumartists == ['Various'] assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2001 assert metadata.disctotal == 2 assert metadata.comp assert metadata.genres == ["Electronic", "Hip Hop", "Pop", "Rock"] assert metadata.freedb_id == "4711" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None # use a different file to check other metadata handling (e.g. artist) shutil.copyfile(self.source_file, os.path.join(self.source_dir, self.target_file_name)) testTagHandler = TagHandler(self.album, self.tagger_config) self.album.disc(2).track(19).new_file = self.target_file_name testTagHandler.tag_single_track(self.source_dir, self.album.disc(2).track(19)) metadata = MediaFile( os.path.join(self.source_dir, self.target_file_name)) logger.debug("artist_sort: %s" % metadata.artist_sort) logger.debug("artist: %s" % metadata.artist) assert metadata.artist == "D-Flame Feat. Eißfeldt" assert metadata.artist_sort == "D-Flame" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2001 assert metadata.disctotal == 2 assert metadata.disc == 2 assert metadata.track == 19 assert metadata.comp assert metadata.genres == ["Electronic", "Hip Hop", "Pop", "Rock"] # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None
def test_tag_album(self): self.copy_files(self.album) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() testTagHandler = TagHandler(self.album, self.tagger_config) testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_files() testTagHandler.tag_album() target_dir = os.path.join(self.target_dir, self.album.target_dir, self.album.disc(1).target_dir) metadata = MediaFile( os.path.join(target_dir, "01-gigi_dagostino-la_passion_(radio_cut).flac")) assert metadata.artist == "Gigi D'Agostino" assert metadata.albumartist == "Various" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2001 assert metadata.disctotal == 2 assert metadata.comp assert metadata.genres == ["Electronic", "Hip Hop", "Pop", "Rock"] assert metadata.freedb_id == "4711" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None metadata = MediaFile( os.path.join( target_dir, "20-papa_roach-last_resort_(album_version_explizit).flac")) logger.debug("artist: %s" % metadata.artist_sort) assert metadata.artist == "Papa Roach" assert metadata.artist_sort == "Papa Roach" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2001 assert metadata.disctotal == 2 assert metadata.disc == 1 assert metadata.track == 20 assert metadata.comp assert metadata.genres == ["Electronic", "Hip Hop", "Pop", "Rock"] # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None
def embed_coverart_track(self, disc, track, imgdata): """ Embed cover art into a single file """ if disc.target_dir != None: track_dir = os.path.join(self.album.target_dir, disc.target_dir) else: track_dir = self.album.target_dir track_file = os.path.join(track_dir, track.new_file) metadata = MediaFile(track_file) metadata.art = imgdata metadata.save()
def test_embed_coverart_album(self): testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) testFileHandler = FileHandler(self.album, self.tagger_config) self.copy_files(self.album) testTagUtils._get_target_list() testFileHandler.copy_files() assert os.path.exists(self.album.sourcedir) assert os.path.exists(self.album.target_dir) source_file = "test/files/cover.jpeg" image_file = os.path.join(self.album.target_dir, "folder.jpg") shutil.copyfile(source_file, image_file) testFileHandler.embed_coverart_album() track = self.album.disc(1).track(1) track_file = os.path.join(self.album.target_dir, self.album.disc(1).target_dir, track.new_file) metadata = MediaFile(track_file) assert not metadata.art == None
def gather_addional_properties(self): ''' Fetches additional technical information about the tracks ''' for disc in self.album.discs: dn = disc.discnumber for track in disc.tracks: tn = track.tracknumber metadata = MediaFile(track.full_path) # for field in metadata.readable_fields(): # print('fieldname: {}: '.format(field)) #, getattr(metadata, field) self.album.disc(dn).track(tn).codec = metadata.type codec = metadata.type lossless = ('flac', 'alac', 'wma', 'ape', 'wav') encod = 'lossless' if codec.lower() in lossless else 'lossy' self.album.disc(dn).track(tn).encoding = encod self.album.disc(dn).track(tn).samplerate = metadata.samplerate self.album.disc(dn).track(tn).bitrate = metadata.bitrate self.album.disc(dn).track(tn).bitdepth = metadata.bitdepth chans = metadata.channels ch_opts = {1: 'mono', 2: 'stereo'} self.album.disc(dn).track(tn).channels = ch_opts[ chans] if chans in ch_opts else '{}ch'.format(chans) self.album.disc(dn).track( tn).length_seconds_fp = metadata.length length_seconds_fp = metadata.length self.album.disc(dn).track(tn).length_seconds = int( length_seconds_fp) self.album.disc(dn).track(tn).length = str( timedelta(seconds=int(length_seconds_fp))) length_ex_str = str( timedelta(seconds=round(length_seconds_fp, 4))) self.album.disc(dn).track(tn).length_ex = length_ex_str[:-2]
def embed_coverart_track(self, disc, track, imgdata): """ Embed cover art into a single file """ if disc.target_dir != None: track_dir = os.path.join(self.album.target_dir, disc.target_dir) else: track_dir = self.album.target_dir track_file = os.path.join(track_dir, track.new_file) metadata = MediaFile(track_file) try: metadata.art = imgdata metadata.save() except Exception as e: logger.error("Unable to embed image '{}'".format(track_file)) print(e)
def test_tag_album_wo_country(self): self.ogsrelid = "543030" # construct config with only default values tagger_config = TaggerConfig(os.path.join(parentdir, "test/empty.conf")) dummy_response = TestDummyResponse(self.ogsrelid) discogs_album = DummyDiscogsAlbum(dummy_response) self.album = discogs_album.map() # copy file to source directory and rename it self.copy_files_single_album(11) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() testTagHandler = TagHandler(self.album, self.tagger_config) testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_files() testTagHandler.tag_album() target_dir = os.path.join(self.target_dir, self.album.target_dir) metadata = MediaFile( os.path.join(target_dir, "01-front_242-masterhit.flac")) assert metadata.artist == "Front 242" assert metadata.albumartist == "Front 242" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 1992 assert metadata.disctotal == 1 assert metadata.genre == "Electronic" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None assert metadata.country == ""
def getSearchParams(self, source_dir): """ get search parameters from exiting tags to find release on discogs. Minimum tags = artist, album title, disc, tracknumber and date is also helpful. If track numbers are not present they are guessed by their index. """ logger.info('Retrieving original metadata for search purposes') # reset candidates & searchParams self.search_params = {} self.candidates = {} files = self._getMusicFiles(source_dir) files.sort() subdirectories = self._fetchSubdirectories(source_dir, files) searchParams = self.search_params searchParams['sourcedir'] = source_dir trackcount = 0 discnumber = 0 searchParams['artists'] = [] for i, file in enumerate(files): trackcount = trackcount + 1 metadata = MediaFile(os.path.join(file)) for a in metadata.artist: searchParams['artists'].append(a) searchParams['albumartist'] = ', '.join(set(metadata.albumartist)) searchParams['album'] = metadata.album searchParams['album'] = re.sub('\[.*?\]', '', searchParams['album']) searchParams['year'] = metadata.year searchParams['date'] = metadata.date # print(file) # print(subdirectories) if metadata.disc is not None and int(metadata.disc) > 1: searchParams['disc'] = metadata.disc elif metadata.disc is None and len(set(subdirectories)) > 1: trackdisc = re.search( r'^(?i)(cd|disc)\s?(?P<discnumber>[0-9]{1,2})', subdirectories[i]) searchParams['disc'] = int(trackdisc.group('discnumber')) # print(searchParams) if 'disc' in searchParams.keys( ) and searchParams['disc'] != discnumber: trackcount = 1 if 'tracks' not in searchParams: searchParams['tracks'] = [] tracknumber = str(searchParams['disc'] ) + '-' if 'disc' in searchParams.keys() else '' if metadata.track is not None: tracknumber += str(metadata.track) else: tracknumber += str(trackcount) # print(searchParams) trackInfo = {} if re.search(r'^(?i)[a-z]', str(metadata.track)): trackInfo['real_tracknumber'] = metadata.track trackInfo['position'] = tracknumber trackInfo['duration'] = str( timedelta(seconds=round(metadata.length, 0))) trackInfo['title'] = metadata.title trackInfo['artist'] = metadata.artist # useful for compilations searchParams['tracks'].append(trackInfo) searchParams['artists'] = list(dict.fromkeys(searchParams['artists'])) searchParams['artist'] = ', '.join(searchParams['artists']) if len(searchParams['artists']) == 0 \ and ('albumartist' not in searchParams or searchParams['albumartist'] == '') \ and ('album' not in searchParams or searchParams['album'] == ''): logger.warning('No metadata available in the audio files') self.metadataFromFileNaming(source_dir, files) searchParams = None return None
def test_tag_album_with_specific_track_artists(self): self.ogsrelid = "112146" # construct config with only default values tagger_config = TaggerConfig(os.path.join(parentdir, "test/empty.conf")) dummy_response = TestDummyResponse(self.ogsrelid) discogs_album = DummyDiscogsAlbum(dummy_response) self.album = discogs_album.map() self.copy_files(self.album) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() testTagHandler = TagHandler(self.album, self.tagger_config) testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_files() testTagHandler.tag_album() target_dir = os.path.join(self.target_dir, self.album.target_dir, self.album.disc(1).target_dir) metadata = MediaFile( os.path.join( target_dir, "01-artful_dodger-re-rewind_the_crowd_say_bo_selecta_(radio_edit).flac" )) assert metadata.artist == "Artful Dodger" assert metadata.albumartist == "Artful Dodger" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2000 assert metadata.disctotal == 2 assert metadata.genre == "Electronic" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None metadata = MediaFile( os.path.join( target_dir, "04-artful_dodger_feat_romina_johnson-movin_too_fast_(artful_dodger_original_mix).flac" )) assert metadata.artist == "Artful Dodger Feat. Romina Johnson" assert metadata.albumartist == "Artful Dodger" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2000 assert metadata.disctotal == 2 assert metadata.track == 4 assert metadata.genre == "Electronic" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None target_dir = os.path.join(self.target_dir, self.album.target_dir, self.album.disc(2).target_dir) metadata = MediaFile( os.path.join(target_dir, "20-paul_johnson-get_get_down_(illicit_remix).flac")) assert metadata.artist == "Paul Johnson" assert metadata.albumartist == "Artful Dodger" assert metadata.discogs_id == self.ogsrelid assert metadata.year == 2000 assert metadata.disctotal == 2 assert metadata.disc == 2 assert metadata.track == 20 assert metadata.genre == "Electronic" # obviously the encoder element is not in the file, but it is returned # empty anyway, no need to check this then... assert metadata.encoder == None
def tag_single_track(self, target_folder, track): # load metadata information logger.debug("target_folder: %s" % target_folder) metadata = MediaFile(os.path.join(target_folder, track.new_file)) # read already existing (and still wanted) properties keepTags = {} for name in self.keep_tags.split(","): logger.debug("name %s" % name) if getattr(metadata, name): keepTags[name] = getattr(metadata, name) # remove current metadata metadata.delete() # set album metadata metadata.album = self.album.title metadata.composer = self.album.artist # use list of albumartists metadata.albumartists = self.album.artists # !TODO really, or should we generate this using a specific method? metadata.albumartist_sort = self.album.sort_artist # !TODO should be joined metadata.label = self.album.labels[0] metadata.year = self.album.year metadata.country = self.album.country metadata.catalognum = self.album.catnumbers[0] # add styles to the grouping tag metadata.groupings = self.album.styles # use genres to allow multiple genres in muliple fields metadata.genres = self.album.genres # this assumes, that there is a metadata-tag with the id_tag_name in the # metadata object setattr(metadata, self.config.id_tag_name, self.album.id) metadata.discogs_release_url = self.album.url metadata.disc = track.discnumber metadata.disctotal = len(self.album.discs) if self.album.is_compilation: metadata.comp = True metadata.comments = self.album.notes tags = self.config.get_configured_tags logger.debug("tags: %s" % tags) for name in tags: value = self.config.get("tags", name) if not value == None: setattr(metadata, name, value) # set track metadata metadata.title = track.title metadata.artists = track.artists # !TODO take care about sortartist ;-) metadata.artist_sort = track.sort_artist metadata.track = track.tracknumber metadata.tracktotal = len(self.album.disc(track.discnumber).tracks) if not keepTags is None: for name in keepTags: setattr(metadata, name, keepTags[name]) metadata.save()