Implemented tag selection and artwork size settings

This commit is contained in:
RemixDev 2020-02-20 16:05:09 +01:00
parent 25d322617f
commit c0f419e1be
2 changed files with 90 additions and 53 deletions

View File

@ -217,6 +217,7 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False):
track['selectedFormat'] = 8 track['selectedFormat'] = 8
track['selectedFilesize'] = track['filesize']['default'] track['selectedFilesize'] = track['filesize']['default']
track['album']['bitrate'] = track['selectedFormat'] track['album']['bitrate'] = track['selectedFormat']
track['album']['picUrl'] = "http://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.jpg".format(track['album']['pic'], settings['taggingSettings']['artworkSize'], settings['taggingSettings']['artworkSize'])
# Create the filename # Create the filename
filename = "{artist} - {title}".format(title=track['title'], artist=track['mainArtist']['name']) + extensions[ filename = "{artist} - {title}".format(title=track['title'], artist=track['mainArtist']['name']) + extensions[
@ -228,9 +229,9 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False):
with open(writepath, 'wb') as stream: with open(writepath, 'wb') as stream:
dz.stream_track(track['id'], track['downloadUrl'], stream) dz.stream_track(track['id'], track['downloadUrl'], stream)
if track['selectedFormat'] in [3, 1, 8]: if track['selectedFormat'] in [3, 1, 8]:
tagID3(writepath, track) tagID3(writepath, track, settings['taggingSettings']['tags'])
elif track['selectedFormat'] == 9: elif track['selectedFormat'] == 9:
tagFLAC(writepath, track) tagFLAC(writepath, track, settings['taggingSettings']['tags'])
def download_track(id, settings, overwriteBitrate=False): def download_track(id, settings, overwriteBitrate=False):
trackAPI = dz.get_track_gw(id) trackAPI = dz.get_track_gw(id)

View File

@ -6,81 +6,117 @@ from mutagen.id3 import ID3, ID3NoHeaderError, TXXX, TIT2, TPE1, TALB, TPE2, TRC
TPUB, TSRC, USLT, APIC, IPLS, TCOM, TCOP TPUB, TSRC, USLT, APIC, IPLS, TCOM, TCOP
def tagID3(stream, track): def tagID3(stream, track, save):
try: try:
tag = ID3(stream) tag = ID3(stream)
except ID3NoHeaderError: except ID3NoHeaderError:
tag = ID3() tag = ID3()
tag.add(TIT2(text=track['title'])) if save['title']:
tag.add(TPE1(text=track['artists'])) tag.add(TIT2(text=track['title']))
tag.add(TALB(text=track['album']['title'])) if save['artist']:
tag.add(TPE2(text=track['album']['artist']['name'])) tag.add(TPE1(text=track['artists']))
tag.add(TRCK(text=str(track['trackNumber']))) if save['album']:
tag.add(TPOS(text=str(track['discNumber']))) tag.add(TALB(text=track['album']['title']))
tag.add(TCON(text=track['album']['genre'])) if save['albumArtist']:
tag.add(TYER(text=str(track['date']['year']))) tag.add(TPE2(text=track['album']['artist']['name']))
tag.add(TDAT(text=str(track['date']['month']) + str(track['date']['day']))) if save['trackNumber']:
tag.add(TLEN(text=str(track['duration']))) tag.add(TRCK(text=str(track['trackNumber'])))
tag.add(TBPM(text=str(track['bpm']))) if save['discNumber']:
tag.add(TPUB(text=track['album']['label'])) tag.add(TPOS(text=str(track['discNumber'])))
tag.add(TSRC(text=track['ISRC'])) if save['genre']:
tag.add(TXXX(desc="BARCODE", text=track['album']['barcode'])) tag.add(TCON(text=track['album']['genre']))
if save['year']:
tag.add(TYER(text=str(track['date']['year'])))
if save['date']:
tag.add(TDAT(text=str(track['date']['month']) + str(track['date']['day'])))
if save['length']:
tag.add(TLEN(text=str(track['duration'])))
if save['bpm']:
tag.add(TBPM(text=str(track['bpm'])))
if save['label']:
tag.add(TPUB(text=track['album']['label']))
if save['isrc']:
tag.add(TSRC(text=track['ISRC']))
if save['barcode']:
tag.add(TXXX(desc="BARCODE", text=track['album']['barcode']))
tag.add(TXXX(desc="ITUNESADVISORY", text="1" if track['explicit'] else "0")) tag.add(TXXX(desc="ITUNESADVISORY", text="1" if track['explicit'] else "0"))
tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track['replayGain'])) if save['replayGain']:
if 'unsync' in track['lyrics']: tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track['replayGain']))
if 'unsync' in track['lyrics'] and save['lyrics']:
tag.add(USLT(text=track['lyrics']['unsync'])) tag.add(USLT(text=track['lyrics']['unsync']))
involved_people = [] involved_people = []
for role in track['contributors']: for role in track['contributors']:
if role in ['author', 'engineer', 'mixer', 'producer', 'writer']: if role in ['author', 'engineer', 'mixer', 'producer', 'writer']:
for person in track['contributors'][role]: for person in track['contributors'][role]:
involved_people.append([role, person]) involved_people.append([role, person])
elif role == 'composer': elif role == 'composer' and save['composer']:
tag.add(TCOM(text=track['contributors']['composer'])) tag.add(TCOM(text=track['contributors']['composer']))
if len(involved_people) > 0: if len(involved_people) > 0 and save['involvedPeople']:
tag.add(IPLS(people=involved_people)) tag.add(IPLS(people=involved_people))
tag.add(TCOP(text=track['copyright'])) if save['copyright']:
tag.add(TCOP(text=track['copyright']))
tag.add(APIC(3, 'image/jpeg', 3, data=urlopen( if save['cover']:
"http://e-cdn-images.deezer.com/images/cover/" + track["album"]['pic'] + "/800x800.jpg").read())) tag.add(APIC(3, 'image/jpeg', 3, data=urlopen(track['album']['picUrl']).read()))
tag.save(stream, v1=2, v2_version=3, v23_sep=None) tag.save(stream, v1=2, v2_version=3, v23_sep=None)
def tagFLAC(stream, track): def tagFLAC(stream, track, save):
tag = FLAC(stream) tag = FLAC(stream)
tag["TITLE"] = track['title'] if save['title']:
tag["ARTIST"] = track['artists'] tag["TITLE"] = track['title']
tag["ALBUM"] = track['album']['title'] if save['artist']:
tag["ALBUMARTIST"] = track['album']['artist']['name'] tag["ARTIST"] = track['artists']
tag["TRACKNUMBER"] = str(track['trackNumber']) if save['album']:
tag["TRACKTOTAL"] = str(track['album']['trackTotal']) tag["ALBUM"] = track['album']['title']
tag["DISCNUMBER"] = str(track['discNumber']) if save['albumArtist']:
tag["DISCTOTAL"] = str(track['album']['discTotal']) tag["ALBUMARTIST"] = track['album']['artist']['name']
tag["GENRE"] = track['album']['genre'] if save['trackNumber']:
tag["YEAR"] = str(track['date']['year']) tag["TRACKNUMBER"] = str(track['trackNumber'])
tag["DATE"] = "{}-{}-{}".format(str(track['date']['year']), str(track['date']['month']), str(track['date']['day'])) if save['trackTotal']:
tag["LENGTH"] = str(track['duration']) tag["TRACKTOTAL"] = str(track['album']['trackTotal'])
tag["BPM"] = str(track['bpm']) if save['discNumber']:
tag["PUBLISHER"] = track['album']['label'] tag["DISCNUMBER"] = str(track['discNumber'])
tag["ISRC"] = track['ISRC'] if save['discTotal']:
tag["BARCODE"] = track['album']['barcode'] tag["DISCTOTAL"] = str(track['album']['discTotal'])
tag["ITUNESADVISORY"] = "1" if track['explicit'] else "0" if save['genre']:
tag["REPLAYGAIN_TRACK_GAIN"] = track['replayGain'] tag["GENRE"] = track['album']['genre']
if 'unsync' in track['lyrics']: if save['year']:
tag["YEAR"] = str(track['date']['year'])
if save['date']:
tag["DATE"] = "{}-{}-{}".format(str(track['date']['year']), str(track['date']['month']), str(track['date']['day']))
if save['length']:
tag["LENGTH"] = str(track['duration'])
if save['bpm']:
tag["BPM"] = str(track['bpm'])
if save['label']:
tag["PUBLISHER"] = track['album']['label']
if save['isrc']:
tag["ISRC"] = track['ISRC']
if save['barcode']:
tag["BARCODE"] = track['album']['barcode']
if save['explicit']:
tag["ITUNESADVISORY"] = "1" if track['explicit'] else "0"
if save['replayGain']:
tag["REPLAYGAIN_TRACK_GAIN"] = track['replayGain']
if 'unsync' in track['lyrics'] and save['lyrics']:
tag["LYRICS"] = track['lyrics']['unsync'] tag["LYRICS"] = track['lyrics']['unsync']
for role in track['contributors']: for role in track['contributors']:
if role in ['author', 'engineer', 'mixer', 'producer', 'writer', 'composer']: if role in ['author', 'engineer', 'mixer', 'producer', 'writer', 'composer']:
tag[role.upper()] = track['contributors'][role] if save['involvedPeople'] and role != 'composer' or role == 'composer' and save['composer']:
elif role == 'musicpublisher': tag[role] = track['contributors'][role]
elif role == 'musicpublisher' and save['involvedPeople']:
tag["ORGANIZATION"] = track['contributors']['musicpublisher'] tag["ORGANIZATION"] = track['contributors']['musicpublisher']
tag["COPYRIGHT"] = track['copyright'] if save['copyright']:
tag["COPYRIGHT"] = track['copyright']
image = Picture() if save['cover']:
image.type = 3 image = Picture()
image.mime = 'image/jpeg' image.type = 3
image.data = urlopen("http://e-cdn-images.deezer.com/images/cover/" + track["album"]['pic'] + "/800x800.jpg").read() image.mime = 'image/jpeg'
tag.add_picture(image) image.data = urlopen(track['album']['picUrl']).read()
tag.add_picture(image)
tag.save(deleteid3=True) tag.save(deleteid3=True)