deemix-py/deemix/__main__.py

101 lines
3.2 KiB
Python
Raw Normal View History

2020-02-17 15:46:18 +00:00
#!/usr/bin/env python3
import click
from pathlib import Path
from deezer import Deezer
from deezer import TrackFormats
from deemix import generateDownloadObject
from deemix.settings import load as loadSettings
2021-08-02 21:45:08 +00:00
from deemix.utils import getBitrateNumberFromText, formatListener
import deemix.utils.localpaths as localpaths
from deemix.downloader import Downloader
from deemix.itemgen import GenerationError
2021-07-05 16:18:57 +00:00
from deemix.plugins.spotify import Spotify
class LogListener:
@classmethod
2021-08-02 21:45:08 +00:00
def send(cls, key, value=None):
logString = formatListener(key, value)
if logString: print(logString)
2021-07-05 16:18:57 +00:00
@click.command()
2020-09-03 14:13:57 +00:00
@click.option('--portable', is_flag=True, help='Creates the config folder in the same directory where the script is launched')
@click.option('-b', '--bitrate', default=None, help='Overwrites the default bitrate selected')
@click.option('-p', '--path', type=str, help='Downloads in the given folder')
@click.argument('url', nargs=-1, required=True)
2020-09-03 14:13:57 +00:00
def download(url, bitrate, portable, path):
# Check for local configFolder
localpath = Path('.')
configFolder = localpath / 'config' if portable else localpaths.getConfigFolder()
settings = loadSettings(configFolder)
dz = Deezer(settings.get('tagsLanguage', ""))
2021-07-05 16:18:57 +00:00
listener = LogListener()
def requestValidArl():
while True:
arl = input("Paste here your arl:")
if dz.login_via_arl(arl.strip()): break
return arl
if (configFolder / '.arl').is_file():
with open(configFolder / '.arl', 'r') as f:
arl = f.readline().rstrip("\n").strip()
if not dz.login_via_arl(arl): arl = requestValidArl()
else: arl = requestValidArl()
with open(configFolder / '.arl', 'w') as f:
f.write(arl)
2021-07-05 16:18:57 +00:00
plugins = {
"spotify": Spotify()
}
plugins["spotify"].setup()
def downloadLinks(url, bitrate=None):
if not bitrate: bitrate = settings.get("maxBitrate", TrackFormats.MP3_320)
links = []
for link in url:
if ';' in link:
for l in link.split(";"):
links.append(l)
else:
links.append(link)
for link in links:
try:
downloadObject = generateDownloadObject(dz, link, bitrate, plugins, listener)
except GenerationError as e:
print(f"{e.link}: {e.message}")
continue
2021-07-05 16:11:17 +00:00
if isinstance(downloadObject, list):
for obj in downloadObject:
2021-07-05 16:18:57 +00:00
Downloader(dz, obj, settings, listener).start()
2021-07-05 16:11:17 +00:00
else:
2021-07-05 16:18:57 +00:00
Downloader(dz, downloadObject, settings, listener).start()
2021-07-05 16:11:17 +00:00
2020-09-03 14:13:57 +00:00
if path is not None:
if path == '': path = '.'
path = Path(path)
settings['downloadLocation'] = str(path)
url = list(url)
if bitrate: bitrate = getBitrateNumberFromText(bitrate)
2020-09-24 15:46:08 +00:00
# If first url is filepath readfile and use them as URLs
2020-09-29 06:57:22 +00:00
try:
isfile = Path(url[0]).is_file()
except Exception:
2020-09-29 06:57:22 +00:00
isfile = False
if isfile:
filename = url[0]
with open(filename) as f:
url = f.readlines()
downloadLinks(url, bitrate)
click.echo("All done!")
2020-02-17 15:46:18 +00:00
if __name__ == '__main__':
download() # pylint: disable=E1120