1

Speed things up by directly calling op

This commit is contained in:
Evan Pratten 2024-05-16 10:44:47 -04:00
parent 6bd3f2c325
commit 8dce55c5ac
4 changed files with 39 additions and 41 deletions

View File

@ -1,4 +1,3 @@
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

View File

@ -2,9 +2,9 @@
import argparse import argparse
import sys import sys
import subprocess
import logging import logging
import spotipy import spotipy
import pyonepassword
from pathlib import Path from pathlib import Path
from spotipy.oauth2 import SpotifyOAuth from spotipy.oauth2 import SpotifyOAuth
@ -17,7 +17,11 @@ def main() -> int:
prog="spotify-current-track-id", prog="spotify-current-track-id",
description="Get the currently playing track ID from Spotify", description="Get the currently playing track ID from Spotify",
) )
ap.add_argument("--url", help="Print the URL of the currently playing track", action="store_true") ap.add_argument(
"--url",
help="Print the URL of the currently playing track",
action="store_true",
)
ap.add_argument( ap.add_argument(
"-v", "--verbose", help="Enable verbose logging", action="store_true" "-v", "--verbose", help="Enable verbose logging", action="store_true"
) )
@ -29,43 +33,41 @@ def main() -> int:
format="%(levelname)s: %(message)s", format="%(levelname)s: %(message)s",
) )
# Connect to 1Password # Read API credentials from 1Password
try: client_id = subprocess.check_output(
op = pyonepassword.OP() "op read -n 'op://ewconfig/rio7e6skp6bhkkcdo5w3kmabgq/Client ID'", shell=True
except Exception as e: ).decode("utf-8")
print(f"Failed to connect to 1Password: {e}", file=sys.stderr) client_secret = subprocess.check_output(
return 1 "op read -n 'op://ewconfig/rio7e6skp6bhkkcdo5w3kmabgq/credential'", shell=True
).decode("utf-8")
# 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_creds.first_field_by_label("Client ID").value, client_id=client_id,
client_secret=spotify_creds.first_field_by_label("credential").value, client_secret=client_secret,
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(),
open_browser=True open_browser=True,
) )
spotify = spotipy.Spotify(auth_manager=oauth) spotify = spotipy.Spotify(auth_manager=oauth)
# Read the currently playing track # Read the currently playing track
current_track = spotify.current_user_playing_track() current_track = spotify.current_user_playing_track()
# If nothing is playing # If nothing is playing
if not current_track: if not current_track:
print("Nothing is currently playing", file=sys.stderr) print("Nothing is currently playing", file=sys.stderr)
return 1 return 1
# Display info # Display info
if args.url: if args.url:
print(current_track["item"]["external_urls"]["spotify"]) print(current_track["item"]["external_urls"]["spotify"])
else: else:
print(current_track["item"]["id"]) print(current_track["item"]["id"])
return 0 return 0

View File

@ -4,7 +4,7 @@ import argparse
import sys import sys
import logging import logging
import spotipy import spotipy
import pyonepassword import subprocess
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
@ -29,22 +29,20 @@ def main() -> int:
format="%(levelname)s: %(message)s", format="%(levelname)s: %(message)s",
) )
# Connect to 1Password # Read API credentials from 1Password
try: client_id = subprocess.check_output(
op = pyonepassword.OP() "op read -n 'op://ewconfig/rio7e6skp6bhkkcdo5w3kmabgq/Client ID'", shell=True
except Exception as e: ).decode("utf-8")
print(f"Failed to connect to 1Password: {e}", file=sys.stderr) client_secret = subprocess.check_output(
return 1 "op read -n 'op://ewconfig/rio7e6skp6bhkkcdo5w3kmabgq/credential'", shell=True
).decode("utf-8")
# 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_creds.first_field_by_label("Client ID").value, client_id=client_id,
client_secret=spotify_creds.first_field_by_label("credential").value, client_secret=client_secret,
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(),

View File

@ -2,7 +2,7 @@
import argparse import argparse
import sys import sys
import logging import logging
import pyonepassword import subprocess
import requests import requests
import json import json
@ -27,22 +27,21 @@ def main() -> int:
format="%(levelname)s: %(message)s", format="%(levelname)s: %(message)s",
) )
# Connect to 1Password
try:
op = pyonepassword.OP()
except Exception as e:
print(f"Failed to connect to 1Password: {e}", file=sys.stderr)
return 1
# Read API credentials # Read API credentials
trello_creds = op.item_get("cbnd5vv3germmc4korkxx3nsim", vault="ewconfig") trello_api_key = subprocess.check_output(
"op read -n 'op://ewconfig/cbnd5vv3germmc4korkxx3nsim/api key'", shell=True
).decode()
trello_api_token = subprocess.check_output(
"op read -n 'op://ewconfig/cbnd5vv3germmc4korkxx3nsim/credential'",
shell=True,
).decode()
# Get all cards # Get all cards
response = requests.get( response = requests.get(
f"https://api.trello.com/1/boards/{args.board}/cards", f"https://api.trello.com/1/boards/{args.board}/cards",
params={ params={
"key": trello_creds.first_field_by_label("api key").value, "key": trello_api_key,
"token": trello_creds.first_field_by_label("credential").value, "token": trello_api_token,
}, },
) )
response.raise_for_status() response.raise_for_status()