Skip to content

Commit

Permalink
feat token based login
Browse files Browse the repository at this point in the history
  • Loading branch information
KMohZaid committed Jul 26, 2024
1 parent 8096e6c commit e779d8e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
20 changes: 16 additions & 4 deletions qobuz_dl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@
def _reset_config(config_file):
logging.info(f"{YELLOW}Creating config file: {config_file}")
config = configparser.ConfigParser()
config["DEFAULT"]["email"] = input("Enter your email:\n- ")
password = input("Enter your password\n- ")
config["DEFAULT"]["password"] = hashlib.md5(password.encode("utf-8")).hexdigest()

config["DEFAULT"]["email"] = ""
config["DEFAULT"]["password"] = ""
config["DEFAULT"]["token"] = ""
if input("Do you want to use a token? (y/n)\n- ").lower() == "y":
config["DEFAULT"]["token"] = input("Enter your token:\n- ")
else:
config["DEFAULT"]["email"] = input("Enter your email:\n- ")
password = input("Enter your password\n- ")
config["DEFAULT"]["password"] = hashlib.md5(password.encode("utf-8")).hexdigest()
config["DEFAULT"]["default_folder"] = (
input("Folder for downloads (leave empty for default 'Qobuz Downloads')\n- ")
or "Qobuz Downloads"
Expand Down Expand Up @@ -118,6 +125,7 @@ def main():
try:
email = config["DEFAULT"]["email"]
password = config["DEFAULT"]["password"]
token = config["DEFAULT"]["token"]
default_folder = config["DEFAULT"]["default_folder"]
default_limit = config["DEFAULT"]["default_limit"]
default_quality = config["DEFAULT"]["default_quality"]
Expand Down Expand Up @@ -177,7 +185,11 @@ def main():
track_format=arguments.track_format or track_format,
smart_discography=arguments.smart_discography or smart_discography,
)
qobuz.initialize_client(email, password, app_id, secrets)

if token:
qobuz.initialize_client_via_token(token,app_id, secrets)
else:
qobuz.initialize_client(email, password, app_id, secrets)

_handle_commands(qobuz, arguments)

Expand Down
4 changes: 4 additions & 0 deletions qobuz_dl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def __init__(
self.track_format = track_format
self.smart_discography = smart_discography

def initialize_client_via_token(self, token, app_id, secrets):
self.client = qopy.Client(None, None, app_id, secrets, token)
logger.info(f"{YELLOW}Set max quality: {QUALITIES[int(self.quality)]}\n")

def initialize_client(self, email, pwd, app_id, secrets):
self.client = qopy.Client(email, pwd, app_id, secrets)
logger.info(f"{YELLOW}Set max quality: {QUALITIES[int(self.quality)]}\n")
Expand Down
33 changes: 23 additions & 10 deletions qobuz_dl/qopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


class Client:
def __init__(self, email, pwd, app_id, secrets):
def __init__(self, email, pwd, app_id, secrets,token=None):
logger.info(f"{YELLOW}Logging...")
self.secrets = secrets
self.id = str(app_id)
Expand All @@ -38,16 +38,23 @@ def __init__(self, email, pwd, app_id, secrets):
)
self.base = "https://www.qobuz.com/api.json/0.2/"
self.sec = None
self.auth(email, pwd)
if token:
self.auth_via_token(token)
else:
self.auth(email, pwd)
self.cfg_setup()

def api_call(self, epoint, **kwargs):
if epoint == "user/login":
params = {
"email": kwargs["email"],
"password": kwargs["pwd"],
"app_id": self.id,
}
if kwargs.get("token") is not None:
self.session.headers.update({"X-User-Auth-Token": kwargs["token"]})
params = None
else:
params = {
"email": kwargs["email"],
"password": kwargs["pwd"],
"app_id": self.id,
}
elif epoint == "track/get":
params = {"track_id": kwargs["id"]}
elif epoint == "album/get":
Expand Down Expand Up @@ -121,9 +128,15 @@ def api_call(self, epoint, **kwargs):

r.raise_for_status()
return r.json()

def auth(self, email, pwd):
usr_info = self.api_call("user/login", email=email, pwd=pwd)

def auth_via_token(self, token):
self.auth(None, None, token=token)

def auth(self, email, pwd, token=None):
if token:
usr_info = self.api_call("user/login",token=token)
else:
usr_info = self.api_call("user/login", email=email, pwd=pwd)
if not usr_info["user"]["credential"]["parameters"]:
raise IneligibleError("Free accounts are not eligible to download tracks.")
self.uat = usr_info["user_auth_token"]
Expand Down

0 comments on commit e779d8e

Please sign in to comment.