2020-02-17 15:46:18 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-02-23 21:51:16 +00:00
|
|
|
import click
|
2020-09-27 21:44:37 +00:00
|
|
|
from pathlib import Path
|
2020-04-17 10:31:47 +00:00
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
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
|
2021-06-27 20:29:41 +00:00
|
|
|
import deemix.utils.localpaths as localpaths
|
|
|
|
from deemix.downloader import Downloader
|
2021-07-16 12:55:11 +00:00
|
|
|
from deemix.itemgen import GenerationError
|
2021-09-21 16:32:20 +00:00
|
|
|
try:
|
|
|
|
from deemix.plugins.spotify import Spotify
|
|
|
|
except ImportError:
|
|
|
|
Spotify = None
|
2021-07-05 16:18:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
|
2020-02-23 21:51:16 +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')
|
2020-02-23 21:51:16 +00:00
|
|
|
@click.option('-b', '--bitrate', default=None, help='Overwrites the default bitrate selected')
|
2020-09-02 21:14:59 +00:00
|
|
|
@click.option('-p', '--path', type=str, help='Downloads in the given folder')
|
2020-05-14 22:06:40 +00:00
|
|
|
@click.argument('url', nargs=-1, required=True)
|
2020-09-03 14:13:57 +00:00
|
|
|
def download(url, bitrate, portable, path):
|
2021-06-27 20:29:41 +00:00
|
|
|
# Check for local configFolder
|
2020-09-27 21:44:37 +00:00
|
|
|
localpath = Path('.')
|
2021-06-27 20:29:41 +00:00
|
|
|
configFolder = localpath / 'config' if portable else localpaths.getConfigFolder()
|
|
|
|
|
|
|
|
settings = loadSettings(configFolder)
|
2021-09-23 21:15:30 +00:00
|
|
|
dz = Deezer()
|
2021-07-05 16:18:57 +00:00
|
|
|
listener = LogListener()
|
2021-06-27 20:29:41 +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)
|
|
|
|
|
2021-09-21 16:32:20 +00:00
|
|
|
plugins = {}
|
|
|
|
if Spotify:
|
|
|
|
plugins = {
|
|
|
|
"spotify": Spotify(configFolder=configFolder)
|
|
|
|
}
|
|
|
|
plugins["spotify"].setup()
|
2021-07-05 16:18:57 +00:00
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
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)
|
|
|
|
|
2021-08-04 19:36:55 +00:00
|
|
|
downloadObjects = []
|
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
for link in links:
|
2021-07-16 12:55:11 +00:00
|
|
|
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):
|
2021-08-04 19:36:55 +00:00
|
|
|
downloadObjects += downloadObject
|
2021-07-05 16:11:17 +00:00
|
|
|
else:
|
2021-08-04 19:36:55 +00:00
|
|
|
downloadObjects.append(downloadObject)
|
|
|
|
|
|
|
|
for obj in downloadObjects:
|
|
|
|
if obj.__type__ == "Convertable":
|
|
|
|
obj = plugins[obj.plugin].convert(dz, obj, settings, listener)
|
|
|
|
Downloader(dz, obj, settings, listener).start()
|
2021-07-05 16:11:17 +00:00
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
|
2020-09-03 14:13:57 +00:00
|
|
|
if path is not None:
|
|
|
|
if path == '': path = '.'
|
2020-09-27 21:44:37 +00:00
|
|
|
path = Path(path)
|
2021-06-27 20:29:41 +00:00
|
|
|
settings['downloadLocation'] = str(path)
|
2020-06-19 18:14:20 +00:00
|
|
|
url = list(url)
|
2021-06-27 20:29:41 +00:00
|
|
|
if bitrate: bitrate = getBitrateNumberFromText(bitrate)
|
2020-09-24 15:46:08 +00:00
|
|
|
|
2021-06-27 20:29:41 +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-27 20:29:41 +00:00
|
|
|
except Exception:
|
2020-09-29 06:57:22 +00:00
|
|
|
isfile = False
|
|
|
|
if isfile:
|
2020-05-15 15:46:50 +00:00
|
|
|
filename = url[0]
|
|
|
|
with open(filename) as f:
|
|
|
|
url = f.readlines()
|
2020-09-27 21:44:37 +00:00
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
downloadLinks(url, bitrate)
|
2020-04-17 10:31:47 +00:00
|
|
|
click.echo("All done!")
|
|
|
|
|
2020-02-17 15:46:18 +00:00
|
|
|
if __name__ == '__main__':
|
2021-06-27 20:29:41 +00:00
|
|
|
download() # pylint: disable=E1120
|