refactored a bit

This commit is contained in:
lars 2022-10-24 13:18:14 +02:00
parent 2792a7c041
commit 966f0c9602

View File

@ -14,70 +14,42 @@ import youtube_music
https://en.wikipedia.org/wiki/ID3
https://mutagen.readthedocs.io/en/latest/user/id3.html
>>> from mutagen.easyid3 import EasyID3
>>> print(EasyID3.valid_keys.keys())
dict_keys(
[
'album',
'bpm',
'compilation',
'composer',
'copyright',
'encodedby',
'lyricist',
'length',
'media',
'mood',
'grouping',
'title',
'version',
'artist',
'albumartist',
'conductor',
'arranger',
'discnumber',
'organization',
'tracknumber',
'author',
'albumartistsort',
'albumsort',
'composersort',
'artistsort',
'titlesort',
'isrc',
'discsubtitle',
'language',
'genre',
'date',
'originaldate',
'performer:*',
'musicbrainz_trackid',
'website',
'replaygain_*_gain',
'replaygain_*_peak',
'musicbrainz_artistid',
'musicbrainz_albumid',
'musicbrainz_albumartistid',
'musicbrainz_trmid',
'musicip_puid',
'musicip_fingerprint',
'musicbrainz_albumstatus',
'musicbrainz_albumtype', <----------
'releasecountry',
'musicbrainz_discid',
'asin',
'performer',
'barcode',
'catalognumber',
'musicbrainz_releasetrackid',
'musicbrainz_releasegroupid',
'musicbrainz_workid',
'acoustid_fingerprint',
'acoustid_id'
])
# to get all valid keys
from mutagen.easyid3 import EasyID3
print(EasyID3.valid_keys.keys())
"""
def write_metadata(row, file_path):
# only convert the file to the proper format if mutagen doesn't work with it due to time
try:
audiofile = EasyID3(file_path)
except mutagen.id3.ID3NoHeaderError:
AudioSegment.from_file(file_path).export(file_path, format="mp3")
audiofile = EasyID3(file_path)
valid_keys = list(EasyID3.valid_keys.keys())
for key in list(row.keys()):
if type(row[key]) == list or key in valid_keys and not pd.isna(row[key]):
# print(key)
if type(row[key]) == int or type(row[key]) == float:
row[key] = str(row[key])
audiofile[key] = row[key]
logging.info("saving")
audiofile.save(file_path, v1=2)
def path_stuff(path: str, file_: str):
# returns true if it shouldn't be downloaded
if os.path.exists(file_):
logging.info(f"'{file_}' does already exist, thus not downloading.")
return True
os.makedirs(path, exist_ok=True)
return False
class Download:
def __init__(self, session: requests.Session = requests.Session(), file: str = ".cache3.csv", temp: str = "temp",
base_path: str = ""):
@ -96,8 +68,8 @@ class Download:
row['file'] = os.path.join(base_path, row['file'])
row['path'] = os.path.join(base_path, row['path'])
if self.path_stuff(row['path'], row['file']):
self.write_metadata(row, row['file'])
if path_stuff(row['path'], row['file']):
write_metadata(row, row['file'])
continue
src = row['src']
@ -105,35 +77,7 @@ class Download:
musify.download(row)
elif src == 'youtube':
youtube_music.download(row)
self.write_metadata(row, row['file'])
def path_stuff(self, path: str, file_: str):
# returns true if it shouldn't be downloaded
if os.path.exists(file_):
logging.info(f"'{file_}' does already exist, thus not downloading.")
return True
os.makedirs(path, exist_ok=True)
return False
def write_metadata(self, row, file_path):
# only convert the file to the proper format if mutagen doesn't work with it due to time
try:
audiofile = EasyID3(file_path)
except mutagen.id3.ID3NoHeaderError:
AudioSegment.from_file(file_path).export(file_path, format="mp3")
audiofile = EasyID3(file_path)
valid_keys = list(EasyID3.valid_keys.keys())
for key in list(row.keys()):
if type(row[key]) == list or key in valid_keys and not pd.isna(row[key]):
# print(key)
if type(row[key]) == int or type(row[key]) == float:
row[key] = str(row[key])
audiofile[key] = row[key]
print("saving")
audiofile.save(file_path, v1=2)
write_metadata(row, row['file'])
if __name__ == "__main__":