Update spotify scripts to call onepassword
This commit is contained in:
parent
b65e60ff0d
commit
382edd2ea5
@ -1,3 +1,4 @@
|
|||||||
requests
|
requests
|
||||||
spotipy
|
spotipy
|
||||||
|
pyonepassword
|
||||||
git+https://github.com/1Password/onepassword-sdk-python.git@2a3c1eb82a6e0d7fbfa32f29dc0fe344f18a054a
|
git+https://github.com/1Password/onepassword-sdk-python.git@2a3c1eb82a6e0d7fbfa32f29dc0fe344f18a054a
|
||||||
|
@ -4,12 +4,10 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import spotipy
|
import spotipy
|
||||||
import subprocess
|
import pyonepassword
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from spotipy.oauth2 import SpotifyOAuth
|
from spotipy.oauth2 import SpotifyOAuth
|
||||||
|
|
||||||
SPOTIFY_CLIENT_ID = "bb39d914c6884316ac09cf0c928d975b"
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -31,38 +29,22 @@ def main() -> int:
|
|||||||
format="%(levelname)s: %(message)s",
|
format="%(levelname)s: %(message)s",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Call `ewp-secrets` to read the Spotify client secret
|
# Connect to 1Password
|
||||||
secrets_proc = subprocess.run(
|
try:
|
||||||
[
|
op = pyonepassword.OP()
|
||||||
(Path(__file__).parent / "ewp-secrets").as_posix(),
|
except Exception as e:
|
||||||
"load",
|
print(f"Failed to connect to 1Password: {e}", file=sys.stderr)
|
||||||
"-n",
|
|
||||||
"spotify",
|
|
||||||
"-k",
|
|
||||||
"client-secret",
|
|
||||||
],
|
|
||||||
capture_output=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# If the secret manager failed, exit
|
|
||||||
if secrets_proc.returncode != 0:
|
|
||||||
print("Failed to load Spotify Client Secret", file=sys.stderr)
|
|
||||||
print(
|
|
||||||
"Please run `ewp-secrets store -n spotify -k client-secret` and pass it the secret from:",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
f"https://developer.spotify.com/dashboard/{SPOTIFY_CLIENT_ID}/settings",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# # Read API credentials
|
||||||
|
spotify_creds = op.item_get("rio7e6skp6bhkkcdo5w3kmabgq", vault="ewconfig")
|
||||||
|
|
||||||
# Connect to Spotify
|
# Connect to Spotify
|
||||||
cache_path = Path("~/.cache/spotipy/spotify-current-track-id.oauth").expanduser()
|
cache_path = Path("~/.cache/spotipy/spotify-current-track-id.oauth").expanduser()
|
||||||
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
oauth = SpotifyOAuth(
|
oauth = SpotifyOAuth(
|
||||||
client_id=SPOTIFY_CLIENT_ID,
|
client_id=spotify_creds.first_field_by_label("Client ID").value,
|
||||||
client_secret=secrets_proc.stdout.decode().strip(),
|
client_secret=spotify_creds.first_field_by_label("credential").value,
|
||||||
redirect_uri="http://localhost:8933",
|
redirect_uri="http://localhost:8933",
|
||||||
scope="user-read-currently-playing",
|
scope="user-read-currently-playing",
|
||||||
cache_path=cache_path.as_posix(),
|
cache_path=cache_path.as_posix(),
|
||||||
|
@ -4,13 +4,11 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import spotipy
|
import spotipy
|
||||||
import subprocess
|
import pyonepassword
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from spotipy.oauth2 import SpotifyOAuth
|
from spotipy.oauth2 import SpotifyOAuth
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
SPOTIFY_CLIENT_ID = "bb39d914c6884316ac09cf0c928d975b"
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -31,42 +29,26 @@ def main() -> int:
|
|||||||
format="%(levelname)s: %(message)s",
|
format="%(levelname)s: %(message)s",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Call `ewp-secrets` to read the Spotify client secret
|
# Connect to 1Password
|
||||||
secrets_proc = subprocess.run(
|
try:
|
||||||
[
|
op = pyonepassword.OP()
|
||||||
(Path(__file__).parent / "ewp-secrets").as_posix(),
|
except Exception as e:
|
||||||
"load",
|
print(f"Failed to connect to 1Password: {e}", file=sys.stderr)
|
||||||
"-n",
|
|
||||||
"spotify",
|
|
||||||
"-k",
|
|
||||||
"client-secret",
|
|
||||||
],
|
|
||||||
capture_output=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# If the secret manager failed, exit
|
|
||||||
if secrets_proc.returncode != 0:
|
|
||||||
print("Failed to load Spotify Client Secret", file=sys.stderr)
|
|
||||||
print(
|
|
||||||
"Please run `ewp-secrets store -n spotify -k client-secret` and pass it the secret from:",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
f"https://developer.spotify.com/dashboard/{SPOTIFY_CLIENT_ID}/settings",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# Read API credentials
|
||||||
|
spotify_creds = op.item_get("rio7e6skp6bhkkcdo5w3kmabgq", vault="ewconfig")
|
||||||
|
|
||||||
# Connect to Spotify
|
# Connect to Spotify
|
||||||
cache_path = Path("~/.cache/spotipy/spotify-now-playing.oauth").expanduser()
|
cache_path = Path("~/.cache/spotipy/spotify-now-playing.oauth").expanduser()
|
||||||
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
oauth = SpotifyOAuth(
|
oauth = SpotifyOAuth(
|
||||||
client_id=SPOTIFY_CLIENT_ID,
|
client_id=spotify_creds.first_field_by_label("Client ID").value,
|
||||||
client_secret=secrets_proc.stdout.decode().strip(),
|
client_secret=spotify_creds.first_field_by_label("credential").value,
|
||||||
redirect_uri="http://localhost:8933",
|
redirect_uri="http://localhost:8933",
|
||||||
scope="user-read-playback-state,user-read-currently-playing",
|
scope="user-read-playback-state,user-read-currently-playing",
|
||||||
cache_path=cache_path.as_posix(),
|
cache_path=cache_path.as_posix(),
|
||||||
open_browser=True
|
open_browser=True,
|
||||||
)
|
)
|
||||||
spotify = spotipy.Spotify(auth_manager=oauth)
|
spotify = spotipy.Spotify(auth_manager=oauth)
|
||||||
|
|
||||||
@ -82,13 +64,19 @@ def main() -> int:
|
|||||||
console = Console(highlight=False)
|
console = Console(highlight=False)
|
||||||
|
|
||||||
# Display basic info
|
# Display basic info
|
||||||
console.print(f"[bold]{current_track['item']['name']}[/bold] by [bold]{' & '.join([artist['name'] for artist in current_track['item']['artists']])}[/bold]")
|
console.print(
|
||||||
console.print(f"Album: [bold]{current_track['item']['album']['name']}[/bold] ({current_track['item']['album']['album_type'].capitalize()}, {current_track['item']['album']['release_date'][:4]})")
|
f"[bold]{current_track['item']['name']}[/bold] by [bold]{' & '.join([artist['name'] for artist in current_track['item']['artists']])}[/bold]"
|
||||||
|
)
|
||||||
|
console.print(
|
||||||
|
f"Album: [bold]{current_track['item']['album']['name']}[/bold] ({current_track['item']['album']['album_type'].capitalize()}, {current_track['item']['album']['release_date'][:4]})"
|
||||||
|
)
|
||||||
console.print(f"Popularity: [bold]{current_track['item']['popularity']}%[/bold]")
|
console.print(f"Popularity: [bold]{current_track['item']['popularity']}%[/bold]")
|
||||||
|
|
||||||
# Get this track's audio features
|
# Get this track's audio features
|
||||||
audio_features = spotify.audio_features([current_track['item']['id']])[0]
|
audio_features = spotify.audio_features([current_track["item"]["id"]])[0]
|
||||||
console.print(f"Tempo: [bold]{audio_features['tempo']} BPM[/bold] ({audio_features['time_signature']}/4)")
|
console.print(
|
||||||
|
f"Tempo: [bold]{audio_features['tempo']} BPM[/bold] ({audio_features['time_signature']}/4)"
|
||||||
|
)
|
||||||
key = {
|
key = {
|
||||||
-1: "???",
|
-1: "???",
|
||||||
0: "C",
|
0: "C",
|
||||||
@ -102,12 +90,13 @@ def main() -> int:
|
|||||||
8: "G♯/A♭",
|
8: "G♯/A♭",
|
||||||
9: "A",
|
9: "A",
|
||||||
10: "A♯/B♭",
|
10: "A♯/B♭",
|
||||||
11: "B"
|
11: "B",
|
||||||
}[audio_features['key']]
|
}[audio_features["key"]]
|
||||||
console.print(f"Key: [bold]{key} {'Major' if audio_features['mode'] == 1 else 'Minor'}[/bold]")
|
console.print(
|
||||||
|
f"Key: [bold]{key} {'Major' if audio_features['mode'] == 1 else 'Minor'}[/bold]"
|
||||||
|
)
|
||||||
console.print(f"Energy: [bold]{int(audio_features['energy'] * 100)}%[/bold]")
|
console.print(f"Energy: [bold]{int(audio_features['energy'] * 100)}%[/bold]")
|
||||||
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user