rewrite spotify stored creds login, bump librespot

This commit is contained in:
uh wot 2024-09-06 16:58:05 +02:00
parent 6d0612a6eb
commit c9918250c8
No known key found for this signature in database
GPG Key ID: CB2454984587B781
4 changed files with 32 additions and 11 deletions

View File

@ -36,7 +36,7 @@
"dependencies": { "dependencies": {
"blowfish-cbc": "^1.0.1", "blowfish-cbc": "^1.0.1",
"image-size": "^1.1.1", "image-size": "^1.1.1",
"librespot": "^0.2.15", "librespot": "^0.2.16",
"undici": "^6.19.4", "undici": "^6.19.4",
"xmldom-qsa": "^1.1.3" "xmldom-qsa": "^1.1.3"
}, },

10
pnpm-lock.yaml generated
View File

@ -15,8 +15,8 @@ importers:
specifier: ^1.1.1 specifier: ^1.1.1
version: 1.1.1 version: 1.1.1
librespot: librespot:
specifier: ^0.2.15 specifier: ^0.2.16
version: 0.2.15 version: 0.2.16
undici: undici:
specifier: ^6.19.4 specifier: ^6.19.4
version: 6.19.4 version: 6.19.4
@ -464,8 +464,8 @@ packages:
resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==}
engines: {node: '>= 0.8.0'} engines: {node: '>= 0.8.0'}
librespot@0.2.15: librespot@0.2.16:
resolution: {integrity: sha512-k8/qd7xWZ+eOGUmX4jevRS79+uMjisIeXzfeXc0roxUhJgRDzGYbGVblovy6378NR9OdoQtBhXts7k7uvgmF5g==} resolution: {integrity: sha512-fvVZO1bouQr3qtU7h+AHOrUqiEwKEUZkjH1LWp35MOnG193K+SVcFHx6y8QmcX7sxn9/ODKGT1XET/uJE5z4Mw==}
locate-path@6.0.0: locate-path@6.0.0:
resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==}
@ -1138,7 +1138,7 @@ snapshots:
prelude-ls: 1.2.1 prelude-ls: 1.2.1
type-check: 0.4.0 type-check: 0.4.0
librespot@0.2.15: librespot@0.2.16:
dependencies: dependencies:
fast-xml-parser: 4.3.4 fast-xml-parser: 4.3.4
protobufjs: 7.2.6 protobufjs: 7.2.6

View File

@ -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()

View File

@ -17,7 +17,8 @@ import {
} from '../../types.js' } from '../../types.js'
interface SpotifyOptions extends LibrespotOptions { interface SpotifyOptions extends LibrespotOptions {
storedCredential?: boolean username?: string
storedCredential?: string
} }
class Spotify implements StreamerWithLogin { class Spotify implements StreamerWithLogin {
@ -38,15 +39,26 @@ class Spotify implements StreamerWithLogin {
} }
} as const } as const
storedCredential: boolean loggedIn = false
constructor(options: SpotifyOptions) { constructor(options: SpotifyOptions) {
this.client = new Librespot(options) 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) { async login(username: string, password: string) {
if (this.storedCredential) return await this.client.loginWithStoredCreds(username, password) if (!this.loggedIn) {
else return await this.client.login(username, password) const result = await this.client.login(username, password)
this.loggedIn = true
return result
}
}
getStoredCredentials() {
return this.client.getStoredCredentials()
} }
#getUrlParts( #getUrlParts(
url: string url: string