From 1616d78385eade1d5bee56296da82e2f256cda3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20R=C3=B6ger?= Date: Fri, 1 Aug 2025 00:58:27 +0200 Subject: [PATCH] System Gen94 @ 2025-08-01-00:58:27 by jonas@monolith --- .../spotify_shortcuts/__main__.py | 4 ++ .../spotify_shortcuts/registry.py | 7 +++ .../spotify_shortcuts/run.py | 47 +++++++++++++++++++ .../spotify_shortcuts/shortcut.py | 21 +++++++++ 4 files changed, 79 insertions(+) create mode 100644 pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py create mode 100644 pkgs/spotify-shortcuts/spotify_shortcuts/registry.py create mode 100644 pkgs/spotify-shortcuts/spotify_shortcuts/run.py create mode 100644 pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py diff --git a/pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py b/pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py new file mode 100644 index 0000000..8f3c09e --- /dev/null +++ b/pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py @@ -0,0 +1,4 @@ +from spotify_shortcuts.run import main + +if __name__ == "__main__": + main() diff --git a/pkgs/spotify-shortcuts/spotify_shortcuts/registry.py b/pkgs/spotify-shortcuts/spotify_shortcuts/registry.py new file mode 100644 index 0000000..7e6f694 --- /dev/null +++ b/pkgs/spotify-shortcuts/spotify_shortcuts/registry.py @@ -0,0 +1,7 @@ +from spotify_shortcuts.spotify_like import SpotifyLike +from spotify_shortcuts.spotify_pl_add import SpotifyPlAdd + +SHORTCUT_REGISTRY = { + "like": SpotifyLike(), + "pl_add": SpotifyPlAdd(), +} diff --git a/pkgs/spotify-shortcuts/spotify_shortcuts/run.py b/pkgs/spotify-shortcuts/spotify_shortcuts/run.py new file mode 100644 index 0000000..423ff6a --- /dev/null +++ b/pkgs/spotify-shortcuts/spotify_shortcuts/run.py @@ -0,0 +1,47 @@ +from spotify_shortcuts.config import Config, load_config +from spotify_shortcuts.shortcut import Shortcut +from spotify_shortcuts.spotify_auth import authenticated_session +from itertools import chain + + +def all_scopes() -> list[str]: + from spotify_shortcuts.registry import SHORTCUT_REGISTRY + + return list( + set( + chain.from_iterable( + shortcut.get_scopes() for shortcut in SHORTCUT_REGISTRY.values() + ) + ) + ) + + +def run_shortcut(shortcut: Shortcut, config: Config): + client = authenticated_session( + config, scopes=all_scopes() # use all scopes to avoid re-authentication + ) + + shortcut.execute(client, config) + + +def main(): + from spotify_shortcuts.registry import SHORTCUT_REGISTRY + import sys + + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} ") + sys.exit(1) + + shortcut_name = sys.argv[1] + if shortcut_name not in SHORTCUT_REGISTRY: + print(f"Shortcut '{shortcut_name}' not found.") + sys.exit(1) + + shortcut = SHORTCUT_REGISTRY[shortcut_name] + config = load_config(args=sys.argv[2:]) + + run_shortcut(shortcut, config) + + +if __name__ == "__main__": + main() diff --git a/pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py b/pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py new file mode 100644 index 0000000..e8d871b --- /dev/null +++ b/pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py @@ -0,0 +1,21 @@ +from abc import ABC, abstractmethod + +from spotify_shortcuts.config import Config +from spotipy import Spotify + + +class Shortcut(ABC): + @abstractmethod + def execute(self, client: Spotify, config: Config): + """Execute the shortcut action.""" + pass + + @abstractmethod + def get_help(self) -> str: + """Return a description of the shortcut.""" + pass + + @abstractmethod + def get_scopes(self) -> list[str]: + """Return the spotify API scopes required for the shortcut.""" + pass