System Gen84 @ 2025-07-24-02:00:39 by jonas@monolith
This commit is contained in:
1
pkgs/spotify-shortcuts/.envrc
Normal file
1
pkgs/spotify-shortcuts/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake ../../#spotify-shortcuts --show-trace
|
||||
2
pkgs/spotify-shortcuts/default.nix
Normal file
2
pkgs/spotify-shortcuts/default.nix
Normal file
@@ -0,0 +1,2 @@
|
||||
{pkgs ? import <nixpkgs> {}}:
|
||||
pkgs.callPackage ./derivation.nix {}
|
||||
7
pkgs/spotify-shortcuts/derivation.nix
Normal file
7
pkgs/spotify-shortcuts/derivation.nix
Normal file
@@ -0,0 +1,7 @@
|
||||
{python3Packages}:
|
||||
with python3Packages;
|
||||
buildPythonApplication {
|
||||
name = "spotify-shortcuts";
|
||||
propagatedBuildInputs = [spotipy pyxdg];
|
||||
src = ./.;
|
||||
}
|
||||
14
pkgs/spotify-shortcuts/setup.py
Normal file
14
pkgs/spotify-shortcuts/setup.py
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name="spotify_shortcuts",
|
||||
version="1.0",
|
||||
packages=find_packages(),
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"spotify-like=spotify_shortcuts.spotify_like:main",
|
||||
],
|
||||
},
|
||||
)
|
||||
8
pkgs/spotify-shortcuts/shell.nix
Normal file
8
pkgs/spotify-shortcuts/shell.nix
Normal file
@@ -0,0 +1,8 @@
|
||||
{pkgs ? import <nixpkgs> {}}:
|
||||
pkgs.mkShell {
|
||||
packages = [
|
||||
pkgs.pyright
|
||||
pkgs.black
|
||||
];
|
||||
inputsFrom = [(pkgs.callPackage ./derivation.nix {})];
|
||||
}
|
||||
50
pkgs/spotify-shortcuts/spotify_shortcuts/config.py
Normal file
50
pkgs/spotify-shortcuts/spotify_shortcuts/config.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
from argparse import ArgumentParser
|
||||
from typing import List
|
||||
from os import getenv
|
||||
from sys import argv
|
||||
from xdg.BaseDirectory import xdg_cache_home
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
cache_file: Path = Path(xdg_cache_home) / Path("spotify-shortcuts.json")
|
||||
client_id: str | None = None
|
||||
client_secret: str | None = None
|
||||
|
||||
|
||||
def load_config(args: List[str] = argv[1:]) -> Config:
|
||||
parser = ArgumentParser(description="Spotify CLI Tool")
|
||||
parser.add_argument(
|
||||
"--cache-file",
|
||||
type=Path,
|
||||
default=Config.cache_file,
|
||||
help="Path to the cache file for Spotify authentication",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--client-id",
|
||||
type=str,
|
||||
default=Config.client_id,
|
||||
help="Spotify API Client ID",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--client-secret",
|
||||
type=str,
|
||||
default=Config.client_secret,
|
||||
help="Spotify API Client Secret",
|
||||
)
|
||||
|
||||
ns = parser.parse_args(args)
|
||||
|
||||
cfg = Config(
|
||||
cache_file=ns.cache_file,
|
||||
client_id=ns.client_id,
|
||||
client_secret=ns.client_secret,
|
||||
)
|
||||
|
||||
return Config(
|
||||
cache_file=Path(getenv("SPOTIFY_SHORTCUTS_CACHE_FILE", cfg.cache_file)),
|
||||
client_id=getenv("SPOTIFY_SHORTCUTS_CLIENT_ID", cfg.client_id),
|
||||
client_secret=getenv("SPOTIFY_SHORTCUTS_CLIENT_SECRET", cfg.client_secret),
|
||||
)
|
||||
18
pkgs/spotify-shortcuts/spotify_shortcuts/spotify_auth.py
Normal file
18
pkgs/spotify-shortcuts/spotify_shortcuts/spotify_auth.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
from spotipy import Spotify
|
||||
from spotify_shortcuts.config import Config
|
||||
|
||||
|
||||
def authenticated_session(cfg: Config) -> Spotify:
|
||||
assert cfg.client_id, "Spotify client ID is required"
|
||||
assert cfg.client_secret, "Spotify client secret is required"
|
||||
|
||||
return Spotify(
|
||||
auth_manager=SpotifyOAuth(
|
||||
client_id=cfg.client_id,
|
||||
client_secret=cfg.client_secret,
|
||||
redirect_uri="http://127.0.0.1:45632/callback",
|
||||
scope="user-library-read user-library-modify user-read-playback-state",
|
||||
cache_path=cfg.cache_file,
|
||||
)
|
||||
)
|
||||
21
pkgs/spotify-shortcuts/spotify_shortcuts/spotify_like.py
Normal file
21
pkgs/spotify-shortcuts/spotify_shortcuts/spotify_like.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from spotify_shortcuts.spotify_auth import authenticated_session
|
||||
from spotify_shortcuts.config import load_config
|
||||
|
||||
|
||||
def main():
|
||||
cfg = load_config()
|
||||
sp = authenticated_session(cfg)
|
||||
|
||||
if (playback := sp.current_playback()) is None:
|
||||
print("No current playback found.")
|
||||
return
|
||||
|
||||
if (uri := playback.get("item", {}).get("uri", None)) is None:
|
||||
print("No track URI found in current playback.")
|
||||
return
|
||||
|
||||
sp.current_user_saved_tracks_add(tracks=[uri])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user