Cleaned up main and taggers

This commit is contained in:
RemixDev 2020-09-24 17:46:08 +02:00
parent 09331516c7
commit 975fdf95dd
2 changed files with 58 additions and 28 deletions

View File

@ -10,11 +10,9 @@ import os.path
@click.option('-p', '--path', type=str, help='Downloads in the given folder') @click.option('-p', '--path', type=str, help='Downloads in the given folder')
@click.argument('url', nargs=-1, required=True) @click.argument('url', nargs=-1, required=True)
def download(url, bitrate, portable, path): def download(url, bitrate, portable, path):
localpath = os.path.realpath('.')
configFolder = None localpath = os.path.realpath('.')
if portable: configFolder = os.path.join(localpath, 'config') if portable else None
configFolder = os.path.join(localpath, 'config')
if path is not None: if path is not None:
if path == '': path = '.' if path == '': path = '.'
path = os.path.realpath(path) path = os.path.realpath(path)
@ -22,10 +20,12 @@ def download(url, bitrate, portable, path):
app = cli(path, configFolder) app = cli(path, configFolder)
app.login() app.login()
url = list(url) url = list(url)
if os.path.isfile(url[0]): if os.path.isfile(url[0]):
filename = url[0] filename = url[0]
with open(filename) as f: with open(filename) as f:
url = f.readlines() url = f.readlines()
app.downloadLink(url, bitrate) app.downloadLink(url, bitrate)
click.echo("All done!") click.echo("All done!")

View File

@ -1,6 +1,7 @@
from mutagen.flac import FLAC, Picture from mutagen.flac import FLAC, Picture
from mutagen.id3 import ID3, ID3NoHeaderError, TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \ from mutagen.id3 import ID3, ID3NoHeaderError, \
TPUB, TSRC, USLT, SYLT, APIC, IPLS, TCOM, TCOP, TCMP, Encoding TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \
TPUB, TSRC, USLT, SYLT, APIC, IPLS, TCOM, TCOP, TCMP, Encoding, PictureType
# Adds tags to a MP3 file # Adds tags to a MP3 file
def tagID3(stream, track, save): def tagID3(stream, track, save):
@ -15,14 +16,14 @@ def tagID3(stream, track, save):
tag.add(TIT2(text=track.title)) tag.add(TIT2(text=track.title))
if save['artist'] and len(track.artists): if save['artist'] and len(track.artists):
if save['multiArtistSeparator'] != "default": if save['multiArtistSeparator'] == "default":
tag.add(TPE1(text=track.artists))
else:
if save['multiArtistSeparator'] == "nothing": if save['multiArtistSeparator'] == "nothing":
tag.add(TPE1(text=track.mainArtist['name'])) tag.add(TPE1(text=track.mainArtist['name']))
else: else:
tag.add(TPE1(text=track.artistsString)) tag.add(TPE1(text=track.artistsString))
tag.add(TXXX(desc="ARTISTS", text=track.artists)) tag.add(TXXX(desc="ARTISTS", text=track.artists))
else:
tag.add(TPE1(text=track.artists))
if save['album']: if save['album']:
tag.add(TALB(text=track.album['title'])) tag.add(TALB(text=track.album['title']))
@ -34,16 +35,24 @@ def tagID3(stream, track, save):
tag.add(TPE2(text=track.album['artists'])) tag.add(TPE2(text=track.album['artists']))
if save['trackNumber']: if save['trackNumber']:
tag.add(TRCK( trackNumber = str(track.trackNumber)
text=str(track.trackNumber) + ("/" + str(track.album['trackTotal']) if save['trackTotal'] else ""))) if save['trackTotal']:
trackNumber += "/" + str(track.album['trackTotal'])
tag.add(TRCK(text=trackNumber))
if save['discNumber']: if save['discNumber']:
tag.add( discNumber = str(track.discNumber)
TPOS(text=str(track.discNumber) + ("/" + str(track.album['discTotal']) if save['discTotal'] else ""))) if save['discTotal']:
discNumber += "/" + str(track.album['discTotal'])
tag.add(TPOS(text=discNumber))
if save['genre']: if save['genre']:
tag.add(TCON(text=track.album['genre'])) tag.add(TCON(text=track.album['genre']))
if save['year']: if save['year']:
tag.add(TYER(text=str(track.date['year']))) tag.add(TYER(text=str(track.date['year'])))
if save['date']: if save['date']:
# Referencing ID3 standard
# https://id3.org/id3v2.3.0#TDAT
# The 'Date' frame is a numeric string in the DDMM format.
tag.add(TDAT(text=str(track.date['day']) + str(track.date['month']))) tag.add(TDAT(text=str(track.date['day']) + str(track.date['month'])))
if save['length']: if save['length']:
tag.add(TLEN(text=str(int(track.duration)*1000))) tag.add(TLEN(text=str(int(track.duration)*1000)))
@ -56,13 +65,17 @@ def tagID3(stream, track, save):
if save['barcode']: if save['barcode']:
tag.add(TXXX(desc="BARCODE", text=track.album['barcode'])) tag.add(TXXX(desc="BARCODE", text=track.album['barcode']))
if save['explicit']: if save['explicit']:
tag.add(TXXX(desc="ITUNESADVISORY", text="1" if track.explicit else "0")) tag.add(TXXX(desc="ITUNESADVISORY", text= "1" if track.explicit else "0" ))
if save['replayGain']: if save['replayGain']:
tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track.replayGain)) tag.add(TXXX(desc="REPLAYGAIN_TRACK_GAIN", text=track.replayGain))
if track.lyrics['unsync'] and save['lyrics']: if track.lyrics['unsync'] and save['lyrics']:
tag.add(USLT(text=track.lyrics['unsync'])) tag.add(USLT(text=track.lyrics['unsync']))
if track.lyrics['syncID3'] and save['syncedLyrics']: if track.lyrics['syncID3'] and save['syncedLyrics']:
tag.add(SYLT(Encoding.UTF8, format=2, type=1, text=track.lyrics['syncID3'])) # Referencing ID3 standard
# https://id3.org/id3v2.3.0#sec4.10
# Type: 1 => is lyrics
# Format: 2 => Absolute time, 32 bit sized, using milliseconds as unit
tag.add(SYLT(Encoding.UTF8, type=1, format=2, text=track.lyrics['syncID3']))
involved_people = [] involved_people = []
for role in track.contributors: for role in track.contributors:
@ -80,12 +93,22 @@ def tagID3(stream, track, save):
tag.add(TCMP(text="1")) tag.add(TCMP(text="1"))
if save['cover'] and track.album['picPath']: if save['cover'] and track.album['picPath']:
with open(track.album['picPath'], 'rb') as f:
tag.add(
APIC(Encoding.UTF8 if save['coverDescriptionUTF8'] else Encoding.LATIN1, 'image/jpeg' if track.album['picPath'].endswith('jpg') else 'image/png', 3, desc='cover', data=f.read()))
tag.save(stream, v1=2 if save['saveID3v1'] else 0, v2_version=3, descEncoding = Encoding.LATIN1
v23_sep=None if save['useNullSeparator'] else '/') if save['coverDescriptionUTF8']:
descEncoding = Encoding.UTF8
mimeType = 'image/jpeg'
if track.album['picPath'].endswith('png'):
mimeType = 'image/png'
with open(track.album['picPath'], 'rb') as f:
tag.add(APIC(descEncoding, mimeType, PictureType.COVER_FRONT, desc='cover', data=f.read()))
tag.save( stream,
v1=2 if save['saveID3v1'] else 0,
v2_version=3,
v23_sep=None if save['useNullSeparator'] else '/' )
# Adds tags to a FLAC file # Adds tags to a FLAC file
def tagFLAC(stream, track, save): def tagFLAC(stream, track, save):
@ -98,20 +121,20 @@ def tagFLAC(stream, track, save):
tag["TITLE"] = track.title tag["TITLE"] = track.title
if save['artist'] and len(track.artists): if save['artist'] and len(track.artists):
if save['multiArtistSeparator'] != "default": if save['multiArtistSeparator'] == "default":
tag["ARTIST"] = track.artists
else:
if save['multiArtistSeparator'] == "nothing": if save['multiArtistSeparator'] == "nothing":
tag["ARTIST"] = track.mainArtist['name'] tag["ARTIST"] = track.mainArtist['name']
else: else:
tag["ARTIST"] = track.artistsString tag["ARTIST"] = track.artistsString
tag["ARTISTS"] = track.artists tag["ARTISTS"] = track.artists
else:
tag["ARTIST"] = track.artists
if save['album']: if save['album']:
tag["ALBUM"] = track.album['title'] tag["ALBUM"] = track.album['title']
if save['albumArtist'] and len(track.album['artists']): if save['albumArtist'] and len(track.album['artists']):
if save['singleAlbumArtist']: if save['singleAlbumArtist'] and track.album['mainArtist']['save']:
tag["ALBUMARTIST"] = track.album['mainArtist']['name'] tag["ALBUMARTIST"] = track.album['mainArtist']['name']
else: else:
tag["ALBUMARTIST"] = track.album['artists'] tag["ALBUMARTIST"] = track.album['artists']
@ -126,12 +149,17 @@ def tagFLAC(stream, track, save):
tag["DISCTOTAL"] = str(track.album['discTotal']) tag["DISCTOTAL"] = str(track.album['discTotal'])
if save['genre']: if save['genre']:
tag["GENRE"] = track.album['genre'] tag["GENRE"] = track.album['genre']
# YEAR tag is not suggested as a standard tag
# Being YEAR already contained in DATE will only use DATE instead
# Reference: https://www.xiph.org/vorbis/doc/v-comment.html#fieldnames
if save['date']: if save['date']:
tag["DATE"] = track.dateString tag["DATE"] = track.dateString
elif save['year']: elif save['year']:
tag["DATE"] = str(track.date['year']) tag["DATE"] = str(track.date['year'])
if save['length']: if save['length']:
tag["LENGTH"] = str(track.duration) tag["LENGTH"] = str(int(track.duration)*1000)
if save['bpm']: if save['bpm']:
tag["BPM"] = str(track.bpm) tag["BPM"] = str(track.bpm)
if save['label']: if save['label']:
@ -149,7 +177,7 @@ def tagFLAC(stream, track, save):
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']:
if save['involvedPeople'] and role != 'composer' or role == 'composer' and save['composer']: if save['involvedPeople'] and role != 'composer' or save['composer'] and role == 'composer':
tag[role] = track.contributors[role] tag[role] = track.contributors[role]
elif role == 'musicpublisher' and save['involvedPeople']: elif role == 'musicpublisher' and save['involvedPeople']:
tag["ORGANIZATION"] = track.contributors['musicpublisher'] tag["ORGANIZATION"] = track.contributors['musicpublisher']
@ -161,8 +189,10 @@ def tagFLAC(stream, track, save):
if save['cover'] and track.album['picPath']: if save['cover'] and track.album['picPath']:
image = Picture() image = Picture()
image.type = 3 image.type = PictureType.COVER_FRONT
image.mime = 'image/jpeg' if track.album['picPath'].endswith('jpg') else 'image/png' image.mime = 'image/jpeg'
if track.album['picPath'].endswith('png'):
image.mime = 'image/png'
with open(track.album['picPath'], 'rb') as f: with open(track.album['picPath'], 'rb') as f:
image.data = f.read() image.data = f.read()
tag.add_picture(image) tag.add_picture(image)