plugin.audio.librespot/resources/lib/librespot_auth.py

53 lines
1.7 KiB
Python
Raw Normal View History

2024-02-21 06:32:58 +00:00
import os, sys
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "deps"))
2024-02-21 05:35:31 +00:00
import time
from typing import Dict
from librespot.core import Session
from utils import log_msg, log_exception, LOGDEBUG
2024-02-21 06:17:59 +00:00
# CLIENT_ID = "2eb96f9b37494be1824999d58028a305"
2024-02-21 05:35:31 +00:00
class LibrespotAuth:
def __init__(self, session: Session):
self.__session = session
def get_token(self) -> Dict[str, str]:
token_info = None
try:
tokenprovider = self.__session.tokens()
result = tokenprovider.get_token(
"user-read-playback-state",
"user-read-currently-playing",
"user-modify-playback-state",
"playlist-read-private",
"playlist-read-collaborative",
"playlist-modify-public",
"playlist-modify-private",
"user-follow-modify",
"user-follow-read",
"user-library-read",
"user-library-modify",
"user-read-private",
"user-read-email",
"user-read-birthdate",
"user-top-read",
)
# Transform token info to spotipy compatible format.
if result is not None:
token_info = {
"access_token": result.access_token,
"expires_in": result.expires_in,
"expires_at": int(time.time()) + result.expires_in,
"refresh_token": result.access_token,
}
2024-02-21 06:17:59 +00:00
2024-02-21 05:35:31 +00:00
except Exception as exc:
log_exception(exc, "Get Spotify token error")
2024-02-21 06:17:59 +00:00
log_msg(f"Token: {token_info}", LOGDEBUG)
2024-02-21 05:35:31 +00:00
return token_info