System Gen93 @ 2025-08-01-00:56:55 by jonas@monolith

This commit is contained in:
2025-08-01 00:56:55 +02:00
parent 0f8c6fc562
commit 9a056ce6b0
4 changed files with 91 additions and 56 deletions

View File

@@ -1,39 +1,56 @@
from spotify_shortcuts.spotify_auth import authenticated_session
from spotify_shortcuts.shortcut import Shortcut
from spotify_shortcuts.config import load_config
from desktop_notifier import DesktopNotifierSync
from spotify_shortcuts.run import run_shortcut
SCOPES = [
"playlist-modify-public",
"playlist-modify-private",
"user-read-playback-state",
]
class SpotifyPlAdd(Shortcut):
def execute(self, client, config):
if (playback := client.current_playback()) is None:
print("No current playback found.")
return
if (track_uri := playback.get("item", {}).get("uri", None)) is None:
print("No track URI found in current playback.")
return
if (context_uri := playback.get("context", {}).get("uri", None)) is None:
print("No context URI found in current playback.")
return
client.playlist_add_items(context_uri, items=[track_uri])
if config.notifications:
track_name = playback.get("item", {}).get("name", "<no-name>")
artists = ", ".join(
a.get("name", "<no-name>")
for a in playback.get("item", {}).get("artists", [])
)
playlist_name = (client.playlist(context_uri) or {}).get(
"name", "<no-name>"
)
dn = DesktopNotifierSync()
dn.send(
title="Track Added to Playlist",
message=f'Track "{track_name}" by "{artists}" has been added to {playlist_name}.',
)
def get_help(self) -> str:
return "Add the currently playing track to the current playlist."
def get_scopes(self) -> list[str]:
return SCOPES
def main():
cfg = load_config()
sp = authenticated_session(cfg)
if (playback := sp.current_playback()) is None:
print("No current playback found.")
return
if (track_uri := playback.get("item", {}).get("uri", None)) is None:
print("No track URI found in current playback.")
return
if (context_uri := playback.get("context", {}).get("uri", None)) is None:
print("No context URI found in current playback.")
return
sp.playlist_add_items(context_uri, items=[track_uri])
if cfg.notifications:
track_name = playback.get("item", {}).get("name", "<no-name>")
artists = ", ".join(
a.get("name", "<no-name>")
for a in playback.get("item", {}).get("artists", [])
)
playlist_name = (sp.playlist(context_uri) or {}).get("name", "<no-name>")
dn = DesktopNotifierSync()
dn.send(
title="Track Added to Playlist",
message=f'Track "{track_name}" by "{artists}" has been added to {playlist_name}.',
)
run_shortcut(SpotifyPlAdd(), load_config())
if __name__ == "__main__":