From c9918250c862b1130f7b9c5c71492ad5204a371b Mon Sep 17 00:00:00 2001 From: uh wot Date: Fri, 6 Sep 2024 16:58:05 +0200 Subject: [PATCH] rewrite spotify stored creds login, bump librespot --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- scripts/spotify-get-stored-creds.js | 9 +++++++++ src/streamers/spotify/main.ts | 22 +++++++++++++++++----- 4 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 scripts/spotify-get-stored-creds.js diff --git a/package.json b/package.json index bf6865e..54fd50c 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "dependencies": { "blowfish-cbc": "^1.0.1", "image-size": "^1.1.1", - "librespot": "^0.2.15", + "librespot": "^0.2.16", "undici": "^6.19.4", "xmldom-qsa": "^1.1.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f49703..d9115d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,8 +15,8 @@ importers: specifier: ^1.1.1 version: 1.1.1 librespot: - specifier: ^0.2.15 - version: 0.2.15 + specifier: ^0.2.16 + version: 0.2.16 undici: specifier: ^6.19.4 version: 6.19.4 @@ -464,8 +464,8 @@ packages: resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} engines: {node: '>= 0.8.0'} - librespot@0.2.15: - resolution: {integrity: sha512-k8/qd7xWZ+eOGUmX4jevRS79+uMjisIeXzfeXc0roxUhJgRDzGYbGVblovy6378NR9OdoQtBhXts7k7uvgmF5g==} + librespot@0.2.16: + resolution: {integrity: sha512-fvVZO1bouQr3qtU7h+AHOrUqiEwKEUZkjH1LWp35MOnG193K+SVcFHx6y8QmcX7sxn9/ODKGT1XET/uJE5z4Mw==} locate-path@6.0.0: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} @@ -1138,7 +1138,7 @@ snapshots: prelude-ls: 1.2.1 type-check: 0.4.0 - librespot@0.2.15: + librespot@0.2.16: dependencies: fast-xml-parser: 4.3.4 protobufjs: 7.2.6 diff --git a/scripts/spotify-get-stored-creds.js b/scripts/spotify-get-stored-creds.js new file mode 100644 index 0000000..850ed6a --- /dev/null +++ b/scripts/spotify-get-stored-creds.js @@ -0,0 +1,9 @@ +import Spotify from './../build/streamers/spotify/main.js' + +const client = new Spotify({}) +await client.login(process.env.SPOTIFY_USERNAME, process.env.SPOTIFY_PASSWORD) + +const storedCreds = client.getStoredCredentials() +console.log('[spotify] New config:', storedCreds) + +await client.disconnect() diff --git a/src/streamers/spotify/main.ts b/src/streamers/spotify/main.ts index ea69db1..49709be 100644 --- a/src/streamers/spotify/main.ts +++ b/src/streamers/spotify/main.ts @@ -17,7 +17,8 @@ import { } from '../../types.js' interface SpotifyOptions extends LibrespotOptions { - storedCredential?: boolean + username?: string + storedCredential?: string } class Spotify implements StreamerWithLogin { @@ -38,15 +39,26 @@ class Spotify implements StreamerWithLogin { } } as const - storedCredential: boolean + loggedIn = false constructor(options: SpotifyOptions) { this.client = new Librespot(options) - this.storedCredential = options.storedCredential == true + + const { username, storedCredential } = options + if (username && storedCredential) { + this.client.loginWithStoredCreds(username, storedCredential) + this.loggedIn = true + } } async login(username: string, password: string) { - if (this.storedCredential) return await this.client.loginWithStoredCreds(username, password) - else return await this.client.login(username, password) + if (!this.loggedIn) { + const result = await this.client.login(username, password) + this.loggedIn = true + return result + } + } + getStoredCredentials() { + return this.client.getStoredCredentials() } #getUrlParts( url: string