From 13b71a6f855a76ad9ad8b74c1320502f0a891efa Mon Sep 17 00:00:00 2001 From: oskvr37 Date: Sun, 21 Jul 2024 20:41:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20basic=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tiddl/__init__.py | 16 +++++++++++----- tiddl/config.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tiddl/config.py diff --git a/tiddl/__init__.py b/tiddl/__init__.py index 10a0b8a..6714aea 100644 --- a/tiddl/__init__.py +++ b/tiddl/__init__.py @@ -1,17 +1,23 @@ import argparse + from .api import TidalApi from .auth import getDeviceAuth, getToken +from .config import Config def main(): parser = argparse.ArgumentParser(description="TIDDL, the Tidal Downloader") print("✅ TIDDL installed!") - auth = getDeviceAuth() - print(f"Go to https://{auth['verificationUriComplete']} and add device!") - input("Hit enter when you are ready") - token = getToken(auth["deviceCode"]) - print(token) + config = Config() + if not config.config["token"]: + auth = getDeviceAuth() + print(f"Go to https://{auth['verificationUriComplete']} and add device!") + input("Hit enter when you are ready") + token = getToken(auth["deviceCode"]) + print(token) + config.config.update({"token": token["access_token"]}) + config.save() if __name__ == "__main__": diff --git a/tiddl/config.py b/tiddl/config.py new file mode 100644 index 0000000..bb9513a --- /dev/null +++ b/tiddl/config.py @@ -0,0 +1,31 @@ +import json +from typing import TypedDict + + +class Settings(TypedDict): + download_path: str + + +class ConfigData(TypedDict): + token: str + settings: Settings + + +FILENAME = ".tiddl.json" +DEFAULT_CONFIG: ConfigData = {"token": "", "settings": {"download_path": "tiddl"}} + + +class Config: + def __init__(self) -> None: + self.config: ConfigData = DEFAULT_CONFIG + + # load config from file or create new + try: + with open(FILENAME, "r") as f: + self.config = json.load(f) + except FileNotFoundError: + self.save() + + def save(self): + with open(FILENAME, "w") as f: + json.dump(self.config, f)