2020-08-10 04:47:51 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
2020-10-17 18:52:35 +02:00
|
|
|
import re
|
2020-08-10 04:47:51 +02:00
|
|
|
import sys
|
2020-10-17 18:52:35 +02:00
|
|
|
|
|
|
|
from pick import pick
|
|
|
|
|
2020-11-03 00:48:48 +01:00
|
|
|
import config
|
|
|
|
from qo_utils import downloader, qopy
|
2020-10-17 18:52:35 +02:00
|
|
|
from qo_utils.search import Search
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getArgs():
|
2020-10-17 18:52:35 +02:00
|
|
|
parser = argparse.ArgumentParser(prog="python3 main.py")
|
|
|
|
parser.add_argument("-a", action="store_true", help="enable albums-only search")
|
|
|
|
parser.add_argument(
|
2020-11-03 00:48:48 +01:00
|
|
|
"-i",
|
|
|
|
metavar="Album/track URL",
|
|
|
|
help="run Qobuz-Dl on URL input mode (download by url)",
|
2020-10-17 18:52:35 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-11-03 00:48:48 +01:00
|
|
|
"-q",
|
|
|
|
metavar="int",
|
|
|
|
default=config.default_quality,
|
|
|
|
help="quality for url input mode (5, 6, 7, 27) (default: 6)",
|
2020-10-17 18:52:35 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
metavar="int",
|
2020-11-03 00:48:48 +01:00
|
|
|
default=config.default_limit,
|
2020-10-17 18:52:35 +02:00
|
|
|
help="limit of search results by type (default: 10)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
metavar="PATH",
|
2020-11-03 00:48:48 +01:00
|
|
|
default=config.default_folder,
|
|
|
|
help="custom directory for downloads (default: '{}')".format(
|
|
|
|
config.default_folder
|
|
|
|
),
|
2020-10-17 18:52:35 +02:00
|
|
|
)
|
2020-08-10 04:47:51 +02:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def musicDir(dir):
|
|
|
|
fix = os.path.normpath(dir)
|
|
|
|
if not os.path.isdir(fix):
|
|
|
|
os.mkdir(fix)
|
|
|
|
return fix
|
|
|
|
|
|
|
|
|
|
|
|
def get_id(url):
|
2020-10-17 18:52:35 +02:00
|
|
|
return re.match(
|
2020-11-20 00:53:23 +01:00
|
|
|
r"https?://(?:w{0,3}|play|open)\.qobuz\.com/(?:(?:album|track|artist"
|
|
|
|
"|playlist|label)/|[a-z]{2}-[a-z]{2}/album/-?\w+(?:-\w+)*-?/|user/library/favorites/)(\w+)",
|
2020-10-17 18:52:35 +02:00
|
|
|
url,
|
|
|
|
).group(1)
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
2020-12-05 19:14:11 +01:00
|
|
|
def processSelected(Qz, path, albums, ids, types, quality):
|
2020-10-17 18:52:35 +02:00
|
|
|
q = ["5", "6", "7", "27"]
|
2020-08-13 06:43:52 +02:00
|
|
|
quality = q[quality[1]]
|
|
|
|
for alb, id_, type_ in zip(albums, ids, types):
|
2020-08-13 08:08:03 +02:00
|
|
|
for al in alb:
|
2020-11-03 00:48:48 +01:00
|
|
|
downloader.iterateIDs(
|
|
|
|
Qz, id_[al[1]], path, quality, True if type_[al[1]] else False
|
|
|
|
)
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
2020-11-20 00:53:23 +01:00
|
|
|
def fromUrl(Qz, id, path, quality, album=True):
|
|
|
|
downloader.iterateIDs(Qz, id, path, str(quality), album)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_urls(url, client, path, quality):
|
|
|
|
possibles = {
|
|
|
|
"playlist": {"func": client.get_plist_meta, "iterable_key": "tracks"},
|
|
|
|
"artist": {"func": client.get_artist_meta, "iterable_key": "albums"},
|
|
|
|
"label": {"func": client.get_label_meta, "iterable_key": "albums"},
|
|
|
|
"album": {"album": True, "func": None, "iterable_key": None},
|
|
|
|
"track": {"album": False, "func": None, "iterable_key": None},
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
url_type = url.split("/")[3]
|
|
|
|
type_dict = possibles[url_type]
|
|
|
|
item_id = get_id(url)
|
|
|
|
print("Downloading {}...".format(url_type))
|
|
|
|
except KeyError:
|
|
|
|
print("Invalid url. Use urls from https://play.qobuz.com!")
|
|
|
|
return
|
|
|
|
if type_dict["func"]:
|
|
|
|
items = [
|
|
|
|
item[type_dict["iterable_key"]]["items"]
|
|
|
|
for item in type_dict["func"](item_id)
|
|
|
|
][0]
|
|
|
|
for item in items:
|
|
|
|
fromUrl(
|
|
|
|
client,
|
|
|
|
item["id"],
|
|
|
|
path,
|
|
|
|
quality,
|
|
|
|
True if type_dict["iterable_key"] == "albums" else False,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
fromUrl(client, item_id, path, quality, type_dict["album"])
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def interactive(Qz, path, limit, tracks=True):
|
|
|
|
while True:
|
2020-08-13 06:43:52 +02:00
|
|
|
Albums, Types, IDs = [], [], []
|
2020-08-10 04:47:51 +02:00
|
|
|
try:
|
2020-08-13 06:43:52 +02:00
|
|
|
while True:
|
|
|
|
query = input("\nEnter your search: [Ctrl + c to quit]\n- ")
|
2020-10-17 18:52:35 +02:00
|
|
|
print("Searching...")
|
2020-12-07 16:32:37 +01:00
|
|
|
if len(query.strip()) == 0:
|
2020-12-05 19:14:11 +01:00
|
|
|
break
|
2020-08-13 06:43:52 +02:00
|
|
|
start = Search(Qz, query, limit)
|
|
|
|
start.getResults(tracks)
|
2020-12-07 16:32:37 +01:00
|
|
|
if len(start.Total) == 0:
|
|
|
|
break
|
2020-08-13 06:43:52 +02:00
|
|
|
Types.append(start.Types)
|
|
|
|
IDs.append(start.IDs)
|
|
|
|
|
2020-10-17 18:52:35 +02:00
|
|
|
title = (
|
|
|
|
"Select [space] the item(s) you want to download "
|
2020-12-05 19:00:30 +01:00
|
|
|
"(zero or more)\nPress Ctrl + c to quit\n"
|
2020-10-17 18:52:35 +02:00
|
|
|
)
|
|
|
|
Selected = pick(
|
2020-12-05 19:00:30 +01:00
|
|
|
start.Total, title, multiselect=True, min_selection_count=0
|
2020-10-17 18:52:35 +02:00
|
|
|
)
|
2020-12-05 19:00:30 +01:00
|
|
|
if len(Selected) > 0:
|
|
|
|
Albums.append(Selected)
|
|
|
|
|
|
|
|
y_n = pick(
|
|
|
|
["Yes", "No"],
|
|
|
|
"Items were added to queue to be downloaded. Keep searching?",
|
|
|
|
)
|
|
|
|
if y_n[0][0] == "N":
|
|
|
|
break
|
|
|
|
else:
|
2020-08-13 06:43:52 +02:00
|
|
|
break
|
|
|
|
|
2020-12-07 16:32:37 +01:00
|
|
|
if len(Albums) > 0:
|
2020-12-05 19:14:11 +01:00
|
|
|
desc = (
|
|
|
|
"Select [intro] the quality (the quality will be automat"
|
|
|
|
"ically\ndowngraded if the selected is not found)"
|
|
|
|
)
|
|
|
|
Qualits = ["320", "Lossless", "Hi-res =< 96kHz", "Hi-Res > 96 kHz"]
|
|
|
|
quality = pick(Qualits, desc, default_index=1)
|
|
|
|
processSelected(Qz, path, Albums, IDs, Types, quality)
|
2020-08-10 04:47:51 +02:00
|
|
|
except KeyboardInterrupt:
|
2020-10-17 18:52:35 +02:00
|
|
|
sys.exit("\nBye")
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
arguments = getArgs()
|
2020-10-17 18:52:35 +02:00
|
|
|
directory = musicDir(arguments.d) + "/"
|
2020-11-03 00:48:48 +01:00
|
|
|
Qz = qopy.Client(config.email, config.password)
|
2020-08-10 04:47:51 +02:00
|
|
|
if not arguments.i:
|
2020-10-17 18:52:35 +02:00
|
|
|
interactive(Qz, directory, arguments.l, not arguments.a)
|
2020-08-10 04:47:51 +02:00
|
|
|
else:
|
2020-11-20 00:53:23 +01:00
|
|
|
handle_urls(arguments.i, Qz, directory, arguments.q)
|
2020-08-10 04:47:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|