def test_get_target_list_single_disc_with_subtracks(self): """ Some releases do have "subtracks" (see 513904, track 15), which means that there are two tracks assigned to one position (e.g. 15.1 and 15.2). This gets quite complicated, because this is rather similar to the multi-disc handling """ self.ogsrelid = "513904" # 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(14) target_file_name = "513904.json" shutil.copyfile(self.source_copy_file, os.path.join(self.source_dir, target_file_name)) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) try: taggerutils._get_target_list() except TaggerError as te: assert True return assert False
def test_get_target_list_single_disc(self): self.ogsrelid = "3083" # 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(17) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.sourcedir == self.source_dir assert self.album.discs[0].sourcedir == None assert self.album.target_dir == os.path.join(self.target_dir, "yonderboi-shallow_and_profound-(molecd023-2)-2000") assert self.album.discs[0].target_dir == None assert self.album.discs[0].tracks[0].orig_file == "01-song.flac" assert self.album.discs[0].tracks[0].new_file == "01-yonderboi-intro.flac" assert self.album.discs[0].tracks[16].orig_file == "17-song.flac" assert self.album.discs[0].tracks[16].new_file == "17-yonderboi-outro.flac" assert self.album.discs[0].copy_files[0] == "album.cue" assert self.album.discs[0].copy_files[1] == "album.m3u" assert self.album.discs[0].copy_files[2] == "id.txt"
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 test_get_images(self): """ It downloads only one image, since this is the default configuration This test needs network connection, as well as authentication support """ testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() # the following stuff is only needed in the test, since we cannot use # a config option for these values ;-( # we are unfortunately treated to login every time this method is called ;-( consumer_key = None consumer_secret = None if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_KEY"): consumer_key = os.environ.get('TRAVIS_DISCOGS_CONSUMER_KEY') if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_SECRET"): consumer_secret = os.environ.get("TRAVIS_DISCOGS_CONSUMER_SECRET") config = self.tagger_config config.set("discogs", "consumer_key", consumer_key) config.set("discogs", "consumer_secret", consumer_secret) discogs_connection = DiscogsConnector(config) testFileHandler = FileHandler(self.album, config) testFileHandler.get_images(discogs_connection) onlyfiles = [ f for f in listdir(self.album.target_dir) if isfile(join(self.album.target_dir, f))] logger.debug("files: %s " % onlyfiles) assert os.path.exists(os.path.join(self.album.target_dir, "folder.jpg")) assert not os.path.exists(os.path.join(self.album.target_dir, "image-01.jpg"))
def test_get_target_list_with_multiple_release_artists(self): """ Some releases do have multiple release-artists (see 2452735), the release artists are treated differently from the track artists, check it in here, to make sure it is really working (should be in test_discogs.py usually) """ self.ogsrelid = "2454735" # 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() taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert len(self.album.artists) == 2 assert self.album.artists[0] == "Frank Zappa" assert self.album.artists[1] == "Ensemble Modern" assert self.album.artist == "Frank Zappa" assert self.album.discs[0].tracks[0].new_file == "01-frank_zappa-intro"
def test_copy_files(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) disc_dir = os.path.join(self.album.target_dir, self.album.disc(1).target_dir) assert os.path.exists(disc_dir) disc_dir = os.path.join(self.album.target_dir, self.album.disc(2).target_dir) assert os.path.exists(disc_dir) track_file = os.path.join(self.album.target_dir, self.album.disc(1).target_dir, self.album.disc(1).track(1).new_file) assert os.path.exists(track_file) track_file = os.path.join(self.album.target_dir, self.album.disc(1).target_dir, self.album.disc(1).track(20).new_file) assert os.path.exists(track_file) track_file = os.path.join(self.album.target_dir, self.album.disc(2).target_dir, self.album.disc(2).track(1).new_file) assert os.path.exists(track_file) track_file = os.path.join(self.album.target_dir, self.album.disc(2).target_dir, self.album.disc(2).track(20).new_file) assert os.path.exists(track_file)
def test_create_file_from_template(self): self.ogsrelid = "3083" # 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() taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) create_file = os.path.join(self.target_dir, "info.nfo") assert taggerutils.create_file_from_template("/info.txt", create_file) assert os.path.exists(create_file) assert taggerutils.create_nfo(self.target_dir) # copy file to source directory and rename it self.copy_files_single_album(17) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.discs[0].tracks[ 0].new_file == "01-yonderboi-intro.flac" assert taggerutils.create_m3u(self.target_dir)
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_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 test_dest_dir_name(self): taggerutils = TaggerUtils("dummy_source_dir", "./dummy_dest_dir", self.tagger_config, self.album) assert taggerutils.dest_dir_name == "dummy_dest_dir/various-megahits_2001_die_erste-(560_938-2)-2001" taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) assert taggerutils.dest_dir_name == "dummy_dest_dir/various-megahits_2001_die_erste-(560_938-2)-2001" taggerutils = TaggerUtils("dummy_source_dir", "/dummy_dest_dir", self.tagger_config, self.album) assert taggerutils.dest_dir_name == "/dummy_dest_dir/various-megahits_2001_die_erste-(560_938-2)-2001" taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) taggerutils.dir_format = "%GENRE%/%ALBARTIST%/%ALBTITLE%-(%CATNO%)-%YEAR%" assert taggerutils.dest_dir_name == "dummy_dest_dir/electronic/various/megahits_2001_die_erste-(560_938-2)-2001"
def test_get_target_list_multi_disc(self): # copy file to source directory and rename it self.copy_files(self.album) target_file_name = "id.txt" shutil.copyfile(self.source_copy_file, os.path.join(self.source_dir, target_file_name)) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.copy_files[0] == "a1.txt" assert self.album.copy_files[1] == "a2.txt" assert self.album.copy_files[2] == "id.txt" assert self.album.sourcedir == self.source_dir assert self.album.discs[0].sourcedir == "disc1" assert self.album.discs[1].sourcedir == "disc2" assert self.album.target_dir == os.path.join( self.target_dir, "various-megahits_2001_die_erste-(560_938-2)-2001") assert self.album.discs[ 0].target_dir == "megahits_2001_die_erste-disc1" assert self.album.discs[ 1].target_dir == "megahits_2001_die_erste-disc2" assert self.album.discs[0].copy_files[0] == "album.cue" assert self.album.discs[0].copy_files[1] == "album.m3u" assert self.album.discs[1].copy_files[0] == "album.cue" assert self.album.discs[1].copy_files[1] == "album.m3u" assert self.album.discs[0].tracks[0].orig_file == "01-song.flac" assert self.album.discs[0].tracks[ 0].new_file == "01-gigi_dagostino-la_passion_(radio_cut).flac" assert self.album.discs[0].tracks[19].orig_file == "20-song.flac" assert self.album.discs[0].tracks[ 19].new_file == "20-papa_roach-last_resort_(album_version_explizit).flac" assert self.album.discs[1].tracks[0].orig_file == "01-song.flac" assert self.album.discs[1].tracks[ 0].new_file == "01-die_3_generation-ich_will_dass_du_mich_liebst_(radio_edit).flac" assert self.album.discs[1].tracks[19].orig_file == "20-song.flac" assert self.album.discs[1].tracks[ 19].new_file == "20-jay-z-i_just_wanna_love_u_(give_it_2_me)_(radio_edit).flac"
def test_value_from_tag(self): taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) format = taggerutils._value_from_tag("%ALBARTIST%-%ALBTITLE%") assert format == "various-megahits_2001_die_erste" format = taggerutils._value_from_tag("%ALBARTIST%-%ALBTITLE%-(%CATNO%)-%YEAR%") assert format == "various-megahits_2001_die_erste-(560_938-2)-2001" format = taggerutils._value_from_tag("%TRACKNO%-%ARTIST%-%TITLE%%TYPE%") assert format == "01-gigi_dagostino-la_passion_(radio_cut).mp3" format = taggerutils._value_from_tag("%TRACKNO%-%ARTIST%-%TITLE%", 1, 1, ".flac") assert format == "01-gigi_dagostino-la_passion_(radio_cut)"
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_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 test_get_images_wo_folderjpg(self): """ Downloads several images from discogs, using authentication This test needs network connection, as well as authentication support """ # construct config with only default values config = TaggerConfig(os.path.join(parentdir, "test/test_values.conf")) testTagUtils = TaggerUtils(self.source_dir, self.target_dir, config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() # the following stuff is only needed in the test, since we cannot use # a config option for these values ;-( # we are unfortunately treated to login every time this method is called ;-( if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_KEY"): consumer_key = os.environ.get('TRAVIS_DISCOGS_CONSUMER_KEY') if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_SECRET"): consumer_secret = os.environ.get("TRAVIS_DISCOGS_CONSUMER_SECRET") config.set("discogs", "consumer_key", consumer_key) config.set("discogs", "consumer_secret", consumer_secret) discogs_connection = DiscogsConnector(config) testFileHandler = FileHandler(self.album, config) testFileHandler.get_images(discogs_connection) onlyfiles = [ f for f in listdir(self.album.target_dir) if isfile(join(self.album.target_dir, f)) ] logger.debug("files: %s " % onlyfiles) logger.debug('checking %s' % self.album.target_dir) assert os.path.exists( os.path.join(self.album.target_dir, "XXIMGXX-01.jpg")) assert os.path.exists( os.path.join(self.album.target_dir, "XXIMGXX-02.jpg")) assert os.path.exists( os.path.join(self.album.target_dir, "XXIMGXX-03.jpg")) assert os.path.exists( os.path.join(self.album.target_dir, "XXIMGXX-04.jpg"))
def test_create_file_from_template(self): self.ogsrelid = "3083" # 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() taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) create_file = os.path.join(self.target_dir, "info.nfo") assert taggerutils.create_file_from_template("/info.txt", create_file) assert os.path.exists(create_file) assert taggerutils.create_nfo(self.target_dir) # copy file to source directory and rename it self.copy_files_single_album(17) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.discs[0].tracks[0].new_file == "01-yonderboi-intro.flac" assert taggerutils.create_m3u(self.target_dir)
def test_copy_other_files(self): assert not self.tagger_config.getboolean("details", "copy_other_files") testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_other_files() album_target_dir = self.album.target_dir assert not os.path.exists(os.path.join(album_target_dir, "id.txt")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(1).target_dir) assert not os.path.exists(os.path.join(disc_target_dir, "album.cue")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(2).target_dir) assert not os.path.exists(os.path.join(disc_target_dir, "album.cue")) # construct config with only default values self.tagger_config = TaggerConfig(os.path.join(parentdir, "test/test_values.conf")) assert self.tagger_config.getboolean("details", "copy_other_files") 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) testFileHandler.copy_other_files() album_target_dir = self.album.target_dir logger.debug("album_target_dir: %s" % album_target_dir) assert os.path.exists(os.path.join(album_target_dir, "id.txt")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(1).target_dir) assert os.path.exists(os.path.join(disc_target_dir, "album.cue")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(2).target_dir) assert os.path.exists(os.path.join(disc_target_dir, "album.cue"))
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 test_get_images(self): """ It downloads only one image, since this is the default configuration This test needs network connection, as well as authentication support """ testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() # the following stuff is only needed in the test, since we cannot use # a config option for these values ;-( # we are unfortunately treated to login every time this method is called ;-( consumer_key = None consumer_secret = None if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_KEY"): consumer_key = os.environ.get('TRAVIS_DISCOGS_CONSUMER_KEY') if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_SECRET"): consumer_secret = os.environ.get("TRAVIS_DISCOGS_CONSUMER_SECRET") config = self.tagger_config config.set("discogs", "consumer_key", consumer_key) config.set("discogs", "consumer_secret", consumer_secret) discogs_connection = DiscogsConnector(config) testFileHandler = FileHandler(self.album, config) testFileHandler.get_images(discogs_connection) onlyfiles = [ f for f in listdir(self.album.target_dir) if isfile(join(self.album.target_dir, f)) ] logger.debug("files: %s " % onlyfiles) assert os.path.exists(os.path.join(self.album.target_dir, "folder.jpg")) assert not os.path.exists( os.path.join(self.album.target_dir, "image-01.jpg"))
def test_get_target_list_multi_disc(self): # copy file to source directory and rename it self.copy_files(self.album) target_file_name = "id.txt" shutil.copyfile(self.source_copy_file, os.path.join(self.source_dir, target_file_name)) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.copy_files[0] == "a1.txt" assert self.album.copy_files[1] == "a2.txt" assert self.album.copy_files[2] == "id.txt" assert self.album.sourcedir == self.source_dir assert self.album.discs[0].sourcedir == "disc1" assert self.album.discs[1].sourcedir == "disc2" assert self.album.target_dir == os.path.join(self.target_dir, "various-megahits_2001_die_erste-(560_938-2)-2001") assert self.album.discs[0].target_dir == "megahits_2001_die_erste-disc1" assert self.album.discs[1].target_dir == "megahits_2001_die_erste-disc2" assert self.album.discs[0].copy_files[0] == "album.cue" assert self.album.discs[0].copy_files[1] == "album.m3u" assert self.album.discs[1].copy_files[0] == "album.cue" assert self.album.discs[1].copy_files[1] == "album.m3u" assert self.album.discs[0].tracks[0].orig_file == "01-song.flac" assert self.album.discs[0].tracks[0].new_file == "01-gigi_dagostino-la_passion_(radio_cut).flac" assert self.album.discs[0].tracks[19].orig_file == "20-song.flac" assert self.album.discs[0].tracks[19].new_file == "20-papa_roach-last_resort_(album_version_explizit).flac" assert self.album.discs[1].tracks[0].orig_file == "01-song.flac" assert self.album.discs[1].tracks[0].new_file == "01-die_3_generation-ich_will_dass_du_mich_liebst_(radio_edit).flac" assert self.album.discs[1].tracks[19].orig_file == "20-song.flac" assert self.album.discs[1].tracks[19].new_file == "20-jay-z-i_just_wanna_love_u_(give_it_2_me)_(radio_edit).flac"
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 test_get_target_list_single_disc(self): self.ogsrelid = "3083" # 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(17) taggerutils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) taggerutils._get_target_list() assert self.album.sourcedir == self.source_dir assert self.album.discs[0].sourcedir == None assert self.album.target_dir == os.path.join( self.target_dir, "yonderboi-shallow_and_profound-(molecd023-2)-2000") assert self.album.discs[0].target_dir == None assert self.album.discs[0].tracks[0].orig_file == "01-song.flac" assert self.album.discs[0].tracks[ 0].new_file == "01-yonderboi-intro.flac" assert self.album.discs[0].tracks[16].orig_file == "17-song.flac" assert self.album.discs[0].tracks[ 16].new_file == "17-yonderboi-outro.flac" assert self.album.discs[0].copy_files[0] == "album.cue" assert self.album.discs[0].copy_files[1] == "album.m3u" assert self.album.discs[0].copy_files[2] == "id.txt"
def test_get_images_wo_folderjpg(self): """ Downloads several images from discogs, using authentication This test needs network connection, as well as authentication support """ # construct config with only default values config = TaggerConfig(os.path.join(parentdir, "test/test_values.conf")) testTagUtils = TaggerUtils(self.source_dir, self.target_dir, config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() # the following stuff is only needed in the test, since we cannot use # a config option for these values ;-( # we are unfortunately treated to login every time this method is called ;-( if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_KEY"): consumer_key = os.environ.get('TRAVIS_DISCOGS_CONSUMER_KEY') if os.environ.has_key("TRAVIS_DISCOGS_CONSUMER_SECRET"): consumer_secret = os.environ.get("TRAVIS_DISCOGS_CONSUMER_SECRET") config.set("discogs", "consumer_key", consumer_key) config.set("discogs", "consumer_secret", consumer_secret) discogs_connection = DiscogsConnector(config) testFileHandler = FileHandler(self.album, config) testFileHandler.get_images(discogs_connection) onlyfiles = [ f for f in listdir(self.album.target_dir) if isfile(join(self.album.target_dir, f))] logger.debug("files: %s " % onlyfiles) logger.debug('checking %s' % self.album.target_dir) assert os.path.exists(os.path.join(self.album.target_dir, "XXIMGXX-01.jpg")) assert os.path.exists(os.path.join(self.album.target_dir, "XXIMGXX-02.jpg")) assert os.path.exists(os.path.join(self.album.target_dir, "XXIMGXX-03.jpg")) assert os.path.exists(os.path.join(self.album.target_dir, "XXIMGXX-04.jpg"))
def test_create_done_file(self): testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) self.copy_files(self.album) testFileHandler = FileHandler(self.album, self.tagger_config) assert not os.path.exists(os.path.join(self.album.sourcedir, "dt.done")) testFileHandler.create_done_file() assert os.path.exists(os.path.join(self.album.sourcedir, "dt.done"))
def test_copy_other_files(self): assert not self.tagger_config.getboolean("details", "copy_other_files") testTagUtils = TaggerUtils(self.source_dir, self.target_dir, self.tagger_config, self.album) self.copy_files(self.album) testTagUtils._get_target_list() testFileHandler = FileHandler(self.album, self.tagger_config) testFileHandler.copy_other_files() album_target_dir = self.album.target_dir assert not os.path.exists(os.path.join(album_target_dir, "id.txt")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(1).target_dir) assert not os.path.exists(os.path.join(disc_target_dir, "album.cue")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(2).target_dir) assert not os.path.exists(os.path.join(disc_target_dir, "album.cue")) # construct config with only default values self.tagger_config = TaggerConfig( os.path.join(parentdir, "test/test_values.conf")) assert self.tagger_config.getboolean("details", "copy_other_files") 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) testFileHandler.copy_other_files() album_target_dir = self.album.target_dir logger.debug("album_target_dir: %s" % album_target_dir) assert os.path.exists(os.path.join(album_target_dir, "id.txt")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(1).target_dir) assert os.path.exists(os.path.join(disc_target_dir, "album.cue")) disc_target_dir = os.path.join(album_target_dir, self.album.disc(2).target_dir) assert os.path.exists(os.path.join(disc_target_dir, "album.cue"))
def test_value_from_tag_format(self): taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) format = taggerutils._value_from_tag_format("%DISCNO%", 1, 1, ".mp3") assert format == "1" format = taggerutils._value_from_tag_format("%ALBARTIST%-%ALBTITLE%") assert format == "Various-Megahits 2001 Die Erste" format = taggerutils._value_from_tag_format("%ALBARTIST%-%ALBTITLE%-(%CATNO%)-%YEAR%") assert format == "Various-Megahits 2001 Die Erste-(560 938-2)-2001" format = taggerutils._value_from_tag_format("%TRACKNO%-%ARTIST%-%TITLE%%TYPE%") assert format == "01-Gigi D'Agostino-La Passion (Radio Cut).mp3" format = taggerutils._value_from_tag_format("%TRACKNO%-%ARTIST%-%TITLE%", 1, 1, ".flac") assert format == "01-Gigi D'Agostino-La Passion (Radio Cut)"
def test_value_from_tag_format(self): taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) format = taggerutils._value_from_tag_format("%DISCNO%", 1, 1, ".mp3") assert format == "1" format = taggerutils._value_from_tag_format("%ALBARTIST%-%ALBTITLE%") assert format == "Various-Megahits 2001 Die Erste" format = taggerutils._value_from_tag_format( "%ALBARTIST%-%ALBTITLE%-(%CATNO%)-%YEAR%") assert format == "Various-Megahits 2001 Die Erste-(560 938-2)-2001" format = taggerutils._value_from_tag_format( "%TRACKNO%-%ARTIST%-%TITLE%%TYPE%") assert format == "01-Gigi D'Agostino-La Passion (Radio Cut).mp3" format = taggerutils._value_from_tag_format( "%TRACKNO%-%ARTIST%-%TITLE%", 1, 1, ".flac") assert format == "01-Gigi D'Agostino-La Passion (Radio Cut)"
def test_value_from_tag(self): taggerutils = TaggerUtils("dummy_source_dir", "dummy_dest_dir", self.tagger_config, self.album) format = taggerutils._value_from_tag("%ALBARTIST%-%ALBTITLE%") assert format == "various-megahits_2001_die_erste" format = taggerutils._value_from_tag( "%ALBARTIST%-%ALBTITLE%-(%CATNO%)-%YEAR%") assert format == "various-megahits_2001_die_erste-(560_938-2)-2001" format = taggerutils._value_from_tag( "%TRACKNO%-%ARTIST%-%TITLE%%TYPE%") assert format == "01-gigi_dagostino-la_passion_(radio_cut).mp3" format = taggerutils._value_from_tag("%TRACKNO%-%ARTIST%-%TITLE%", 1, 1, ".flac") assert format == "01-gigi_dagostino-la_passion_(radio_cut)"
if not options.releaseid: p.error("Please specify the discogs.com releaseid ('-r')") if not options.sdir or not os.path.exists(options.sdir): p.error("Please specify a valid source directory ('-s')") config = ConfigParser.ConfigParser() config.read(options.conffile) logging.basicConfig(level=config.getint("logging", "level")) keep_original = config.getboolean("details", "keep_original") embed_coverart = config.getboolean("details", "embed_coverart") release = TaggerUtils(options.sdir, options.releaseid) release.nfo_format = config.get("file-formatting", "nfo") release.m3u_format = config.get("file-formatting", "m3u") release.dir_format = config.get("file-formatting", "dir") release.song_format = config.get("file-formatting", "song") release.group_name = config.get("details", "group") # ensure we were able to map the release appropriately. if not release.tag_map: logging.error("Unable to match file list to discogs release '%s'" % options.releaseid) sys.exit() # # start tagging actions. #
connector = discogs_connector discogs_album = DiscogsAlbum(release) try: album = discogs_album.map() except AlbumError as ae: msg = "Error during mapping ({0}), {1}: {2}".format( releaseid, source_dir, ae) logger.error(msg) discs_with_errors.append(msg) continue logger.info("Tagging album '%s - %s'" % (album.artist, album.title)) taggerUtils = TaggerUtils(source_dir, destdir, tagger_config, album) tagHandler = TagHandler(album, tagger_config) fileHandler = FileHandler(album, tagger_config) try: taggerUtils._get_target_list() except TaggerError as te: msg = "Error during Tagging ({0}), {1}: {2}".format( releaseid, source_dir, te) logger.error(msg) discs_with_errors.append(msg) continue fileHandler.copy_files()
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
release = discogs_connector.fetch_release(releaseid) connector = discogs_connector discogs_album = DiscogsAlbum(release) try: album = discogs_album.map() except AlbumError as ae: msg = "Error during mapping ({0}), {1}: {2}".format(releaseid, source_dir, ae) logger.error(msg) discs_with_errors.append(msg) continue logger.info("Tagging album '%s - %s'" % (album.artist, album.title)) taggerUtils = TaggerUtils(source_dir, destdir, tagger_config, album) tagHandler = TagHandler(album, tagger_config) fileHandler = FileHandler(album, tagger_config) try: taggerUtils._get_target_list() except TaggerError as te: msg = "Error during Tagging ({0}), {1}: {2}".format(releaseid, source_dir, te) logger.error(msg) discs_with_errors.append(msg) continue fileHandler.copy_files() logger.debug("Tagging files")
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
if not options.destdir or not os.path.exists(options.destdir): destdir = options.sdir else: destdir = options.destdir config = ConfigParser.ConfigParser() config.read(options.conffile) logging.basicConfig(level=config.getint("logging", "level")) keep_original = config.getboolean("details", "keep_original") embed_coverart = config.getboolean("details", "embed_coverart") use_style = config.getboolean("details", "use_style") release = TaggerUtils(options.sdir, destdir, options.releaseid) release.nfo_format = config.get("file-formatting", "nfo") release.m3u_format = config.get("file-formatting", "m3u") release.dir_format = config.get("file-formatting", "dir") release.song_format = config.get("file-formatting", "song") release.group_name = config.get("details", "group") # ensure we were able to map the release appropriately. if not release.tag_map: logging.error("Unable to match file list to discogs release '%s'" % options.releaseid) sys.exit() # # start tagging actions. #
split_discs_folder = config_boolean_value("details", "split_discs_folder", config, release_tags) split_discs = config_boolean_value("details", "split_discs", config, release_tags) if split_discs: split_discs_extension = config_value("details", "split_discs_extension", config, release_tags).strip('"') split_artists = config_value("details", "split_artists", config, release_tags).strip('"') split_genres_and_styles = config.get("details", "split_genres_and_styles", config, release_tags).strip('"') char_exceptions = config_char_exceptions( config._sections["character_exceptions"]) release = TaggerUtils(options.sdir, destdir, use_lower_filenames, releaseid, split_artists, split_genres_and_styles, copy_other_files, char_exceptions) release.nfo_format = nfo_format release.m3u_format = m3u_format release.dir_format = dir_format release.song_format = song_format release.va_song_format = va_song_format release.disc_folder_name = disc_folder_name release.group_name = group_name first_image_name = "folder.jpg" if not use_folder_jpg: first_image_name = "{0}-01.jpg".format(images_format) # ensure we were able to map the release appropriately.
encoder_tag = config_value("tags", "encoder", config, release_tags) use_style = config_boolean_value("details", "use_style", config, release_tags) split_discs_folder = config_boolean_value("details", "split_discs_folder", config, release_tags) split_discs = config_boolean_value("details", "split_discs", config, release_tags) if split_discs: split_discs_extension = config_value("details", "split_discs_extension", config, release_tags).strip('"') split_artists = config_value("details", "split_artists", config, release_tags).strip('"') split_genres_and_styles = config.get("details", "split_genres_and_styles", config, release_tags).strip('"') char_exceptions = config_char_exceptions(config._sections["character_exceptions"]) release = TaggerUtils(options.sdir, destdir, use_lower_filenames, releaseid, split_artists, split_genres_and_styles, copy_other_files, char_exceptions) release.nfo_format = nfo_format release.m3u_format = m3u_format release.dir_format = dir_format release.song_format = song_format release.va_song_format = va_song_format release.disc_folder_name = disc_folder_name release.group_name = group_name first_image_name = "folder.jpg" if not use_folder_jpg: first_image_name = "{0}-01.jpg".format(images_format) # ensure we were able to map the release appropriately. if not release.tag_map:
def processSourceDirs(source_dirs, tagger_config): # initialize connection (could be a problem if using multiple sources...) discogs_connector = DiscogsConnector(tagger_config) local_discogs_connector = LocalDiscogsConnector(discogs_connector) # try to re-use search, may be useful if working with several releases by the same artist discogsSearch = DiscogsSearch(tagger_config) logger.info("start tagging") discs_with_errors = [] converted_discs = 0 for source_dir in source_dirs: releaseid = None release = None connector = None try: done_file = tagger_config.get("details", "done_file") done_file_path = os.path.join(source_dir, done_file) if os.path.exists(done_file_path) and not options.forceUpdate: logger.warn( 'Do not read {}, because {} exists and forceUpdate is false' .format(source_dir, done_file)) continue # reread config to make sure, that the album specific options are reset for each # album tagger_config = TaggerConfig(options.conffile) if options.releaseid is not None: releaseid = options.releaseid else: releaseid = file_utils.read_id_file(source_dir, id_file, options) if not releaseid: searchParams = discogsSearch.getSearchParams(source_dir) # release = discogsSearch.search_discogs(searchParams) release = discogsSearch.search_discogs() # reuse the Discogs Release class, it saves re-fetching later if release is not None and type(release).__name__ in ( 'Release', 'Version'): releaseid = release.id connector = discogs_connector if not releaseid: logger.warn('No releaseid for {}'.format(source_dir)) continue # if not releaseid: # p.error("Please specify the discogs.com releaseid ('-r')") logger.info('Found release ID: {} for source dir: {}'.format( releaseid, source_dir)) # read destination directory # !TODO if both are the same, we are not copying anything, # this should be "configurable" if not options.destdir: destdir = source_dir else: destdir = options.destdir logger.debug('destdir set to {}'.format(options.destdir)) logger.info('Using destination directory: {}'.format(destdir)) logger.debug("starting tagging...") if releaseid is not None and release is None: #! TODO this is dirty, refactor it to be able to reuse it for later enhancements if tagger_config.get("source", "name") == "local": release = local_discogs_connector.fetch_release( releaseid, source_dir) connector = local_discogs_connector else: release = discogs_connector.fetch_release(releaseid) connector = discogs_connector discogs_album = DiscogsAlbum(release) try: album = discogs_album.map() except AlbumError as ae: msg = "Error during mapping ({0}), {1}: {2}".format( releaseid, source_dir, ae) logger.error(msg) discs_with_errors.append(msg) continue logger.info('Tagging album "{} - {}"'.format( album.artist, album.title)) tagHandler = TagHandler(album, tagger_config) taggerUtils = TaggerUtils(source_dir, destdir, tagger_config, album) fileHandler = FileHandler(album, tagger_config) try: taggerUtils._get_target_list() except TaggerError as te: msg = "Error during Tagging ({0}), {1}: {2}".format( releaseid, source_dir, te) logger.error(msg) discs_with_errors.append(msg) continue tagHandler.tag_album() taggerUtils.gather_addional_properties() # reset the target directory now that we have discogs metadata and # filedata - otherwise this is declared too early in the process album.target_dir = taggerUtils.dest_dir_name fileHandler.copy_files() logger.debug("Tagging files") # Do replaygain analysis before copying other files, the directory # contents are cleaner, less prone to mistakes if options.replaygain: logger.debug("Add ReplayGain tags (if requested)") fileHandler.add_replay_gain_tags() logger.debug("Copy other interesting files (on request)") fileHandler.copy_other_files() logger.debug("Downloading and storing images") fileHandler.get_images(connector) logger.debug("Embedding Albumart") fileHandler.embed_coverart_album() # !TODO make this more generic to use different templates and files, # furthermore adopt to reflect multi-disc-albums logger.debug("Generate m3u") taggerUtils.create_m3u(album.target_dir) logger.debug("Generate nfo") taggerUtils.create_nfo(album.target_dir) fileHandler.create_done_file() except Exception as ex: if releaseid: msg = "Error during tagging ({0}), {1}: {2}".format( releaseid, source_dir, ex) else: msg = "Error during tagging (no relid) {0}: {1}".format( source_dir, ex) logger.error(msg) discs_with_errors.append(msg) continue # !TODO - make this a check during the taggerutils run # ensure we were able to map the release appropriately. #if not release.tag_map: # logger.error("Unable to match file list to discogs release '%s'" % # releaseid) # sys.exit() converted_discs = converted_discs + 1 logger.info("Converted %d/%d" % (converted_discs, len(source_dirs))) logger.info("Tagging complete.") logger.info("converted successful: %d" % converted_discs) logger.info("converted with Errors %d" % len(discs_with_errors)) logger.info("releases touched: %s" % len(source_dirs)) if discs_with_errors: logger.error("The following discs could not get converted.") for msg in discs_with_errors: logger.error(msg)
def tagger(conf, destination, releaseid, source): _log = init_logging(conf) if not destination: destination = source cfg = TaggerConfig(source, destination, conf) if cfg.id_tag in cfg.release_tags: releaseid = cfg.release_tags[cfg.id_tag].strip() if releaseid: release_id = releaseid if not releaseid: click.echo('Please specify the discogs.com releaseid ("-r")') sys.exit(1) _log.info('Attempting to tag files from target destination={0}'.format(destination)) discogs_release = DiscogsAlbum(DiscogsWrapper().discogs, release_id, cfg.split_artists, cfg.split_genres_and_styles) release = TaggerUtils(discogs_release, cfg) # ensure we were able to map the release appropriately. if not release.tag_map: _log.fatal("Unable to map available audio files to the number of tracks in the Discogs release '{0}'. Exiting".format( release_id)) sys.exit(1) artist = cfg.split_artists.join(release.album.artists) artist = release.album.clean_name(artist) _log.info("Tagging album '{0} - {1}'".format(artist, release.album.title)) dest_dir_name = release.dest_dir_name if os.path.exists(dest_dir_name): _log.fatal('Destination directory already exists. directory={0}. Aborting operation'.format(dest_dir_name)) sys.exit(1) else: _log.info("Creating destination directory '{0}'".format(dest_dir_name)) mkdir_p(dest_dir_name) _log.info("Downloading and storing images") release.album.get_images(dest_dir_name, cfg.images_format, cfg.first_image_name) disc_names = dict() folder_names = dict() if release.album.disctotal > 1 and cfg.split_discs_folder: _log.debug("Creating disc structure") for i in range(1, release.album.disctotal + 1): folder_name = "%s%.d" % (release.album_folder_name, i) disc_dir_name = os.path.join(dest_dir_name, folder_name) mkdir_p(disc_dir_name) # This is duplicate, remove one of the following statements disc_names[i] = disc_dir_name folder_names[i] = folder_name else: folder_names[1] = "" for track in release.tag_map: # copy old file into new location if release.album.disctotal > 1 and cfg.split_discs_folder: target_folder = disc_names[int(track['discnumber'])] else: target_folder = dest_dir_name _log.debug("Source file {0}".format(os.path.join(source, track['orig_file']))) _log.info("Writing file {0}".format(os.path.join(target_folder, track['new_file']))) _log.debug("metadata -> {0:2d} {1} - {2}".format(track['tracknumber'], track['artist'], track['title'])) _log.debug("----------> {0}".format(track['new_file'])) shutil.copyfile(track['orig_file'], os.path.join(target_folder, track['new_file'])) # load metadata information metadata = MediaFile(os.path.join(target_folder, track['new_file'])) # read already existing (and still wanted) properties keep_tags = {} if cfg.keep_tags: for name in cfg.keep_tags.split(","): try: getattr(metadata, name) except AttributeError: _log.warn('Unable to keep_tag. tag={0}'.format(name)) continue keep_tags[name] = getattr(metadata, name) # remove current metadata metadata.delete() # set album metadata metadata.album = release.album.title if cfg.split_discs_folder and release.album.disctotal > 1: # the fileext should be stored on the album/track as well fileext = os.path.splitext(track['orig_file'])[1] disc_title_extension = release._value_from_tag_format(cfg.split_discs_extension, track['tracknumber'], track['position'] - 1, fileext) metadata.album = "{0}{1}".format(metadata.album, disc_title_extension) metadata.composer = artist metadata.albumartist = artist metadata.albumartist_sort = release.album.sort_artist metadata.label = release.album.label metadata.year = release.album.year metadata.country = release.album.country metadata.url = release.album.url # add styles to the grouping tag (right now, we can just use one) metadata.grouping = release.album.styles # adding two as there is no standard. discogstagger pre v1 # used (TXXX desc="Catalog #") # mediafile uses TXXX desc="CATALOGNUMBER" metadata.catalognum = release.album.catno metadata.catalognumber = release.album.catno # use the correct genre field, on config use the first style genre = release.album.genres if cfg.use_style: genre = release.album.style metadata.genre = genre metadata.discogs_id = release_id if release.album.disctotal and release.album.disctotal > 1 and track['discnumber']: _log.debug("writing disctotal and discnumber") metadata.disc = track['discnumber'] metadata.disctotal = release.album.disctotal if release.album.is_compilation: metadata.comp = True metadata.comments = release.album.note # encoder if cfg.encoder_tag is not None: metadata.encoder = cfg.encoder_tag # if track.discsubtotal: # metadata.discsubtotal = track.discsubtotal # set track metadata metadata.title = track['title'] metadata.artist = track['artist'] metadata.artist_sort = track['sortartist'] metadata.track = track['tracknumber'] # the following value will be wrong, if the disc has a name or is a multi # disc release --> fix it metadata.tracktotal = release.album.tracktotal_on_disc(track['discnumber']) # it does not make sense to store this in the "common" configuration, but only in the # id.txt. we use a special naming convention --> most probably we should reuse the # configuration parser for this one as well, no? for name, value in list(cfg.release_tags.items()): if name.startswith("tag:"): name = name.split(":") name = name[1] setattr(metadata, name, value) first_image_name = cfg.first_image_name # this should be done in a cleaner way to avoid multiple images in different # folders (use the dest_dir again....) if cfg.embed_coverart and os.path.exists(os.path.join(dest_dir_name, first_image_name)): imgdata = open(os.path.join(dest_dir_name, first_image_name), 'rb').read() imgtype = imghdr.what(None, imgdata) if imgtype in ("jpeg", "png"): _log.debug("Embedding album art.") metadata.art = imgdata if keep_tags is not None: for name in keep_tags: setattr(metadata, name, keep_tags[name]) metadata.save() # start supplementary actions if cfg.write_nfo: _log.info("Generating .nfo file") release.create_nfo() # adopt for multi disc support if cfg.write_m3u: _log.info("Generating .m3u file") release.create_m3u(folder_names) # copy "other files" on request if cfg.copy_other_files and len(release.copy_files) > 0: _log.info("copying files from source directory") copy_files = release.copy_files dir_list = os.listdir(source) _log.debug("start_dir: {0}".format(source)) _log.debug("dir list: {0}".format(dir_list)) file_list = [os.path.join(source, x) for x in dir_list if not x.lower().endswith(TaggerUtils.FILE_TYPE) and os.path.isfile(os.path.join(source, x))] copy_files.extend(file_list) for fname in copy_files: if not fname.endswith(".m3u"): _log.debug("source: {0}".format(fname)) _log.debug("target: {0}".format(os.path.join(dest_dir_name, os.path.basename(fname)))) shutil.copyfile(fname, os.path.join(dest_dir_name, os.path.basename(fname))) # remove source directory, if configured as such. if not cfg.keep_original: _log.info("Deleting source directory '{0}'".format(source)) shutil.rmtree(source) _log.info("Tagging complete.")
def tagger(conf, destination, releaseid, source): _log = init_logging(conf) if not destination: destination = source cfg = TaggerConfig(source, destination, conf) if cfg.id_tag in cfg.release_tags: releaseid = cfg.release_tags[cfg.id_tag].strip() if releaseid: release_id = releaseid if not releaseid: click.echo('Please specify the discogs.com releaseid ("-r")') sys.exit(1) _log.info('Attempting to tag files from target destination={0}'.format( destination)) discogs_release = DiscogsAlbum(DiscogsWrapper().discogs, release_id, cfg.split_artists, cfg.split_genres_and_styles) release = TaggerUtils(discogs_release, cfg) # ensure we were able to map the release appropriately. if not release.tag_map: _log.fatal( "Unable to map available audio files to the number of tracks in the Discogs release '{0}'. Exiting" .format(release_id)) sys.exit(1) artist = cfg.split_artists.join(release.album.artists) artist = release.album.clean_name(artist) _log.info("Tagging album '{0} - {1}'".format(artist, release.album.title)) dest_dir_name = release.dest_dir_name if os.path.exists(dest_dir_name): _log.fatal( 'Destination directory already exists. directory={0}. Aborting operation' .format(dest_dir_name)) sys.exit(1) else: _log.info("Creating destination directory '{0}'".format(dest_dir_name)) mkdir_p(dest_dir_name) _log.info("Downloading and storing images") release.album.get_images(dest_dir_name, cfg.images_format, cfg.first_image_name) disc_names = dict() folder_names = dict() if release.album.disctotal > 1 and cfg.split_discs_folder: _log.debug("Creating disc structure") for i in range(1, release.album.disctotal + 1): folder_name = "%s%.d" % (release.album_folder_name, i) disc_dir_name = os.path.join(dest_dir_name, folder_name) mkdir_p(disc_dir_name) # This is duplicate, remove one of the following statements disc_names[i] = disc_dir_name folder_names[i] = folder_name else: folder_names[1] = "" for track in release.tag_map: # copy old file into new location if release.album.disctotal > 1 and cfg.split_discs_folder: target_folder = disc_names[int(track['discnumber'])] else: target_folder = dest_dir_name _log.debug("Source file {0}".format( os.path.join(source, track['orig_file']))) _log.info("Writing file {0}".format( os.path.join(target_folder, track['new_file']))) _log.debug("metadata -> {0:2d} {1} - {2}".format( track['tracknumber'], track['artist'], track['title'])) _log.debug("----------> {0}".format(track['new_file'])) shutil.copyfile(track['orig_file'], os.path.join(target_folder, track['new_file'])) # load metadata information metadata = MediaFile(os.path.join(target_folder, track['new_file'])) # read already existing (and still wanted) properties keep_tags = {} if cfg.keep_tags: for name in cfg.keep_tags.split(","): try: getattr(metadata, name) except AttributeError: _log.warn('Unable to keep_tag. tag={0}'.format(name)) continue keep_tags[name] = getattr(metadata, name) # remove current metadata metadata.delete() # set album metadata metadata.album = release.album.title if cfg.split_discs_folder and release.album.disctotal > 1: # the fileext should be stored on the album/track as well fileext = os.path.splitext(track['orig_file'])[1] disc_title_extension = release._value_from_tag_format( cfg.split_discs_extension, track['tracknumber'], track['position'] - 1, fileext) metadata.album = "{0}{1}".format(metadata.album, disc_title_extension) metadata.composer = artist metadata.albumartist = artist metadata.albumartist_sort = release.album.sort_artist metadata.label = release.album.label metadata.year = release.album.year metadata.country = release.album.country metadata.url = release.album.url # add styles to the grouping tag (right now, we can just use one) metadata.grouping = release.album.styles # adding two as there is no standard. discogstagger pre v1 # used (TXXX desc="Catalog #") # mediafile uses TXXX desc="CATALOGNUMBER" metadata.catalognum = release.album.catno metadata.catalognumber = release.album.catno # use the correct genre field, on config use the first style genre = release.album.genres if cfg.use_style: genre = release.album.style metadata.genre = genre metadata.discogs_id = release_id if release.album.disctotal and release.album.disctotal > 1 and track[ 'discnumber']: _log.debug("writing disctotal and discnumber") metadata.disc = track['discnumber'] metadata.disctotal = release.album.disctotal if release.album.is_compilation: metadata.comp = True metadata.comments = release.album.note # encoder if cfg.encoder_tag is not None: metadata.encoder = cfg.encoder_tag # if track.discsubtotal: # metadata.discsubtotal = track.discsubtotal # set track metadata metadata.title = track['title'] metadata.artist = track['artist'] metadata.artist_sort = track['sortartist'] metadata.track = track['tracknumber'] # the following value will be wrong, if the disc has a name or is a multi # disc release --> fix it metadata.tracktotal = release.album.tracktotal_on_disc( track['discnumber']) # it does not make sense to store this in the "common" configuration, but only in the # id.txt. we use a special naming convention --> most probably we should reuse the # configuration parser for this one as well, no? for name, value in list(cfg.release_tags.items()): if name.startswith("tag:"): name = name.split(":") name = name[1] setattr(metadata, name, value) first_image_name = cfg.first_image_name # this should be done in a cleaner way to avoid multiple images in different # folders (use the dest_dir again....) if cfg.embed_coverart and os.path.exists( os.path.join(dest_dir_name, first_image_name)): imgdata = open(os.path.join(dest_dir_name, first_image_name), 'rb').read() imgtype = imghdr.what(None, imgdata) if imgtype in ("jpeg", "png"): _log.debug("Embedding album art.") metadata.art = imgdata if keep_tags is not None: for name in keep_tags: setattr(metadata, name, keep_tags[name]) metadata.save() # start supplementary actions if cfg.write_nfo: _log.info("Generating .nfo file") release.create_nfo() # adopt for multi disc support if cfg.write_m3u: _log.info("Generating .m3u file") release.create_m3u(folder_names) # copy "other files" on request if cfg.copy_other_files and len(release.copy_files) > 0: _log.info("copying files from source directory") copy_files = release.copy_files dir_list = os.listdir(source) _log.debug("start_dir: {0}".format(source)) _log.debug("dir list: {0}".format(dir_list)) file_list = [ os.path.join(source, x) for x in dir_list if not x.lower().endswith(TaggerUtils.FILE_TYPE) and os.path.isfile(os.path.join(source, x)) ] copy_files.extend(file_list) for fname in copy_files: if not fname.endswith(".m3u"): _log.debug("source: {0}".format(fname)) _log.debug("target: {0}".format( os.path.join(dest_dir_name, os.path.basename(fname)))) shutil.copyfile( fname, os.path.join(dest_dir_name, os.path.basename(fname))) # remove source directory, if configured as such. if not cfg.keep_original: _log.info("Deleting source directory '{0}'".format(source)) shutil.rmtree(source) _log.info("Tagging complete.")