check token expiry

This commit is contained in:
oskvr37
2024-07-22 14:56:14 +02:00
parent 571f825eb7
commit ffcc0a3042
2 changed files with 39 additions and 9 deletions
+32 -8
View File
@@ -1,25 +1,49 @@
import argparse
import time
from .api import TidalApi
from .auth import getDeviceAuth, getToken
from .auth import getDeviceAuth, getToken, refreshToken
from .config import Config
def main():
parser = argparse.ArgumentParser(description="TIDDL, the Tidal Downloader")
print("✅ TIDDL installed!")
config = Config()
if not config["token"]:
auth = getDeviceAuth()
print(f"Go to https://{auth['verificationUriComplete']} and add device!")
input("Hit enter when you are ready")
print(
f"⚙️ Go to https://{auth['verificationUriComplete']} and add device!\nHit enter when you are ready..."
)
token = getToken(auth["deviceCode"])
print(token)
config.update({"token": token["access_token"], "refresh_token": token["refresh_token"]})
config.update(
{
"token": token["access_token"],
"refresh_token": token["refresh_token"],
"token_expires_at": int(time.time()) + token["expires_in"],
}
)
print("✅ Got token!")
else:
print(config)
t_now = int(time.time())
token_expired = t_now > config["token_expires_at"]
if token_expired:
token = refreshToken(config["refresh_token"])
config.update(
{
"token": token["access_token"],
"token_expires_at": int(time.time()) + token["expires_in"],
}
)
print("✅ Refreshed token!")
else:
# TODO: format time to days and hours ✨
time_to_expire = config["token_expires_at"] - t_now
print(f"✅ Token good for {time_to_expire}s")
api = TidalApi(config["token"])
playlists = api.getPlaylists()
print(f"You have got {playlists['totalNumberOfItems']} playlists.")
if __name__ == "__main__":
+7 -1
View File
@@ -9,11 +9,17 @@ class Settings(TypedDict, total=False):
class ConfigData(TypedDict, total=False):
token: str
refresh_token: str
token_expires_at: int
settings: Settings
FILENAME = ".tiddl.json"
DEFAULT_CONFIG: ConfigData = {"token": "", "settings": {"download_path": "tiddl"}}
DEFAULT_CONFIG: ConfigData = {
"token": "",
"refresh_token": "",
"token_expires_at": 0,
"settings": {"download_path": "tiddl"},
}
class Config: