Skip to content

Commit b11ecfa

Browse files
author
Zotify
committed
save login
1 parent 66e98dc commit b11ecfa

5 files changed

Lines changed: 49 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Changes
1212

13+
- Username and password login has been replaced with username and token
1314
- Genre metadata available for all tracks
1415
- Boolean command line options are now set like `--save-metadata` or `--no-save-metadata` for True or False
1516
- Setting `--config` (formerly `--config-location`) can be set to "None" to not use any config file

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = zotify
3-
version = 0.9.6
3+
version = 0.9.7
44
author = Zotify Contributors
55
description = A highly customizable music and podcast downloader
66
long_description = file: README.md

zotify/__init__.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class Session(LibrespotSession):
7070
def __init__(
7171
self,
7272
session_builder: LibrespotSession.Builder,
73-
oauth: OAuth,
7473
language: str = "en",
74+
oauth: OAuth | None = None,
7575
) -> None:
7676
"""
7777
Authenticates user, saves credentials to a file and generates api token.
@@ -96,7 +96,7 @@ def __init__(
9696
self.authenticate(session_builder.login_credentials)
9797

9898
@staticmethod
99-
def from_file(auth: OAuth, cred_file: Path | str, language: str = "en") -> Session:
99+
def from_file(cred_file: Path | str, language: str = "en") -> Session:
100100
"""
101101
Creates session using saved credentials file
102102
Args:
@@ -107,17 +107,17 @@ def from_file(auth: OAuth, cred_file: Path | str, language: str = "en") -> Sessi
107107
"""
108108
if not isinstance(cred_file, Path):
109109
cred_file = Path(cred_file).expanduser()
110-
conf = (
110+
config = (
111111
LibrespotSession.Configuration.Builder()
112112
.set_store_credentials(False)
113113
.build()
114114
)
115-
session = LibrespotSession.Builder(conf).stored_file(str(cred_file))
116-
return Session(session, auth, language) # TODO
115+
session = LibrespotSession.Builder(config).stored_file(str(cred_file))
116+
return Session(session, language)
117117

118118
@staticmethod
119119
def from_oauth(
120-
auth: OAuth,
120+
oauth: OAuth,
121121
save_file: Path | str | None = None,
122122
language: str = "en",
123123
) -> Session:
@@ -129,24 +129,24 @@ def from_oauth(
129129
Returns:
130130
Zotify session
131131
"""
132-
builder = LibrespotSession.Configuration.Builder()
132+
config = LibrespotSession.Configuration.Builder()
133133
if save_file:
134134
if not isinstance(save_file, Path):
135135
save_file = Path(save_file).expanduser()
136136
save_file.parent.mkdir(parents=True, exist_ok=True)
137-
builder.set_stored_credential_file(str(save_file))
137+
config.set_stored_credential_file(str(save_file))
138138
else:
139-
builder.set_store_credentials(False)
139+
config.set_store_credentials(False)
140140

141-
token = auth.await_token()
141+
token = oauth.await_token()
142142

143-
session = LibrespotSession.Builder(builder.build())
144-
session.login_credentials = Authentication.LoginCredentials(
145-
username=auth.username,
143+
builder = LibrespotSession.Builder(config.build())
144+
builder.login_credentials = Authentication.LoginCredentials(
145+
username=oauth.username,
146146
typ=Authentication.AuthenticationType.values()[3],
147147
auth_data=token.access_token.encode(),
148148
)
149-
return Session(session, auth, language)
149+
return Session(builder, language, oauth)
150150

151151
def __get_playable(
152152
self, playable_id: PlayableId, quality: Quality
@@ -186,7 +186,7 @@ def get_episode(self, episode_id: str) -> Episode:
186186
self.api(),
187187
)
188188

189-
def oauth(self) -> OAuth:
189+
def oauth(self) -> OAuth | None:
190190
"""Returns OAuth service"""
191191
return self.__oauth
192192

@@ -282,7 +282,10 @@ def __init__(self, session: Session):
282282
self._session = session
283283

284284
def get_token(self, *scopes) -> TokenProvider.StoredToken:
285-
return self._session.oauth().get_token()
285+
oauth = self._session.oauth()
286+
if oauth is None:
287+
return super().get_token(*scopes)
288+
return oauth.get_token()
286289

287290
class StoredToken(LibrespotTokenProvider.StoredToken):
288291
def __init__(self, obj):
@@ -301,15 +304,15 @@ class OAuth:
301304

302305
def __init__(self, username: str):
303306
self.username = username
304-
self.__server_thread = Thread(target=self.__run_server)
305-
self.__server_thread.start()
306307

307-
def get_authorization_url(self) -> str:
308+
def auth_interactive(self) -> str:
308309
"""
309-
Generate OAuth URL
310+
Starts local server for token callback
310311
Returns:
311312
OAuth URL
312313
"""
314+
self.__server_thread = Thread(target=self.__run_server)
315+
self.__server_thread.start()
313316
self.__code_verifier = generate_code_verifier()
314317
code_challenge = get_code_challenge(self.__code_verifier)
315318
params = {

zotify/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from zotify.config import CONFIG_PATHS, CONFIG_VALUES
88
from zotify.utils import OptionalOrFalse
99

10-
VERSION = "0.9.6"
10+
VERSION = "0.9.7"
1111

1212

1313
def main():
@@ -51,7 +51,7 @@ def main():
5151
help="Searches for only this type",
5252
)
5353
parser.add_argument("--username", type=str, default="", help="Account username")
54-
parser.add_argument("--password", type=str, default="", help="Account password")
54+
parser.add_argument("--token", type=str, default="", help="Account token")
5555
group = parser.add_mutually_exclusive_group(required=True)
5656
group.add_argument(
5757
"urls",
@@ -127,8 +127,7 @@ def main():
127127
args = parser.parse_args()
128128
if args.version:
129129
print(VERSION)
130-
return
131-
if args.debug:
130+
elif args.debug:
132131
args.func(args)
133132
else:
134133
try:

zotify/app.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,27 @@ def __init__(self, args: Namespace):
149149
Logger(self.__config)
150150

151151
# Create session
152-
# if args.username != "" and args.password != "":
153-
# self.__session = Session.from_userpass(
154-
# args.username,
155-
# args.password,
156-
# self.__config.credentials_path,
157-
# self.__config.language,
158-
# )
159-
# elif self.__config.credentials_path.is_file():
160-
# self.__session = Session.from_file(
161-
# self.__config.credentials_path, self.__config.language
162-
# )
163-
# else:
164-
# self.__session = Session.from_prompt(
165-
# self.__config.credentials_path, self.__config.language
166-
# )
167-
username = input("Username: ")
168-
auth = OAuth(username)
169-
auth_url = auth.get_authorization_url()
170-
print(f"\nClick on the following link to login:\n{auth_url}")
171-
self.__session = Session.from_oauth(
172-
auth, self.__config.credentials_path, self.__config.language
173-
)
152+
if args.username != "" and args.token != "":
153+
oauth = OAuth(args.username)
154+
oauth.set_token(args.token, OAuth.RequestType.REFRESH)
155+
self.__session = Session.from_oauth(
156+
oauth, self.__config.credentials_path, self.__config.language
157+
)
158+
elif self.__config.credentials_path.is_file():
159+
self.__session = Session.from_file(
160+
self.__config.credentials_path,
161+
self.__config.language,
162+
)
163+
else:
164+
username = args.username
165+
while username == "":
166+
username = input("Username: ")
167+
oauth = OAuth(username)
168+
auth_url = oauth.auth_interactive()
169+
print(f"\nClick on the following link to login:\n{auth_url}")
170+
self.__session = Session.from_oauth(
171+
oauth, self.__config.credentials_path, self.__config.language
172+
)
174173

175174
# Get items to download
176175
ids = self.get_selection(args)

0 commit comments

Comments
 (0)