deemix-py/deemix/__main__.py

77 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-02-17 15:46:18 +00:00
#!/usr/bin/env python3
import click
from pathlib import Path
2021-03-19 13:31:32 +00:00
from deezer import Deezer
from deezer import TrackFormats
2021-03-24 16:41:03 +00:00
from deemix import generateDownloadObject
2021-06-07 18:25:51 +00:00
from deemix.settings import load as loadSettings
2021-03-19 13:31:32 +00:00
from deemix.utils import getBitrateNumberFromText
import deemix.utils.localpaths as localpaths
from deemix.downloader import Downloader
@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):
2021-03-19 13:31:32 +00:00
# Check for local configFolder
localpath = Path('.')
2021-03-19 13:31:32 +00:00
configFolder = localpath / 'config' if portable else localpaths.getConfigFolder()
settings = loadSettings(configFolder)
dz = Deezer(settings.get('tagsLanguage', ""))
2021-03-19 13:31:32 +00:00
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)
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:
2021-03-24 16:41:03 +00:00
downloadObject = generateDownloadObject(dz, link, bitrate)
Downloader(dz, downloadObject, settings).start()
2021-03-19 13:31:32 +00:00
2020-09-03 14:13:57 +00:00
if path is not None:
if path == '': path = '.'
path = Path(path)
2021-03-19 13:31:32 +00:00
settings['downloadLocation'] = str(path)
url = list(url)
2021-03-19 13:31:32 +00:00
if bitrate: bitrate = getBitrateNumberFromText(bitrate)
2020-09-24 15:46:08 +00:00
2021-03-19 13:31:32 +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()
2021-06-07 18:25:51 +00:00
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()
2021-03-19 13:31:32 +00:00
downloadLinks(url, bitrate)
click.echo("All done!")
2020-02-17 15:46:18 +00:00
if __name__ == '__main__':
2021-04-10 09:53:52 +00:00
download() # pylint: disable=E1120