diff --git a/package.json b/package.json index dd16759..ff60e0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lucida", - "version": "2.0.0-17", + "version": "2.0.0-18", "description": "A modular music downloader tool", "main": "build/index.js", "type": "module", diff --git a/src/index.ts b/src/index.ts index 5b84ce2..039ecd1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,11 +30,11 @@ class Lucida { } async login() { if (!this.logins) throw new Error('No logins specified') - for (const i in this.logins) { + for (const i in this.modules) { const credentials = this.logins[i] const module = this.modules[i] if (module && 'login' in module) { - await module.login?.(credentials.username, credentials.password) + await module.login?.(credentials?.username, credentials?.password) } } } diff --git a/src/streamers/deezer/main.ts b/src/streamers/deezer/main.ts index 2e58a7f..fc0eef4 100644 --- a/src/streamers/deezer/main.ts +++ b/src/streamers/deezer/main.ts @@ -77,7 +77,7 @@ export default class Deezer implements StreamerWithLogin { availableFormats: Set = new Set() constructor(options?: DeezerOptions) { - if (options?.arl) this.#loginViaArl(options.arl) + if (options?.arl) this.arl = options.arl } async #apiCall( @@ -106,6 +106,7 @@ export default class Deezer implements StreamerWithLogin { if (error.constructor.name == 'Object') { const [type, msg] = Object.entries(error)[0] + if (type == 'VALID_TOKEN_REQUIRED') throw new Error('Invalid ARL') throw new Error(`API Error: ${type}\n${msg}`) } @@ -152,32 +153,31 @@ export default class Deezer implements StreamerWithLogin { async login(username: string, password: string): Promise { if (this.arl) { - return + await this.#loginViaArl(this.arl) + } else { + const resp = await fetch('https://www.deezer.com/', { headers: this.headers }) + const setCookie = resp.headers.get('Set-Cookie') ?? '' + const sid = setCookie.match(/sid=(fr[0-9a-f]+)/)![1] + this.headers['Cookie'] = `sid=${sid}` + + password = this.#md5(password) + + const loginReq = await fetch( + `https://connect.deezer.com/oauth/user_auth.php?${new URLSearchParams({ + app_id: CLIENT_ID, + login: username, + password, + hash: this.#md5(CLIENT_ID + username + password + CLIENT_SECRET) + })}`, + { headers: this.headers } + ) + const { error } = await loginReq.json() + + if (error) throw new Error('Error while getting access token, check your credentials') + + const arl = await this.#apiCall('user.getArl') + await this.#loginViaArl(arl) } - - const resp = await fetch('https://www.deezer.com/', { headers: this.headers }) - const setCookie = resp.headers.get('Set-Cookie') ?? '' - const sid = setCookie.match(/sid=(fr[0-9a-f]+)/)![1] - this.headers['Cookie'] = `sid=${sid}` - - password = this.#md5(password) - - const loginReq = await fetch( - `https://connect.deezer.com/oauth/user_auth.php?${new URLSearchParams({ - app_id: CLIENT_ID, - login: username, - password, - hash: this.#md5(CLIENT_ID + username + password + CLIENT_SECRET) - })}`, - { headers: this.headers } - ) - const { error } = await loginReq.json() - - if (error) throw new Error('Error while getting access token, check your credentials') - - const arl = await this.#apiCall('user.getArl') - - await this.#loginViaArl(arl) } /* ---------- SEARCH ---------- */