System Gen94 @ 2025-08-01-00:58:27 by jonas@monolith
This commit is contained in:
parent
9a056ce6b0
commit
1616d78385
4
pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py
Normal file
4
pkgs/spotify-shortcuts/spotify_shortcuts/__main__.py
Normal file
@ -0,0 +1,4 @@
|
||||
from spotify_shortcuts.run import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
7
pkgs/spotify-shortcuts/spotify_shortcuts/registry.py
Normal file
7
pkgs/spotify-shortcuts/spotify_shortcuts/registry.py
Normal file
@ -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(),
|
||||
}
|
||||
47
pkgs/spotify-shortcuts/spotify_shortcuts/run.py
Normal file
47
pkgs/spotify-shortcuts/spotify_shortcuts/run.py
Normal file
@ -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]} <shortcut_name>")
|
||||
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()
|
||||
21
pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py
Normal file
21
pkgs/spotify-shortcuts/spotify_shortcuts/shortcut.py
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user