discord-player-deezer 2.2.1-dev.2 → 2.3.1-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ type DeezerExtractorOptions = {
15
15
  decryptionKey?: string;
16
16
  createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>;
17
17
  searchLimit?: number;
18
+ reloadUserInterval?: number;
18
19
  };
19
20
  type DeezerUserInfo = {
20
21
  cookie: string;
@@ -27,8 +28,9 @@ declare const Warnings: {
27
28
  };
28
29
  type Warnings = (typeof Warnings)[keyof typeof Warnings];
29
30
  declare class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
30
- static identifier: string;
31
+ static readonly identifier: string;
31
32
  userInfo: DeezerUserInfo;
33
+ __interval?: ReturnType<typeof setInterval>;
32
34
  activate(): Promise<void>;
33
35
  deactivate(): Promise<void>;
34
36
  validate(query: string, type: SearchQueryType & "deezer"): Promise<boolean>;
@@ -40,7 +42,7 @@ declare class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
40
42
  }
41
43
 
42
44
  declare function getCrypto(): Promise<typeof crypto>;
43
- declare function isUrl(query: string): Promise<boolean>;
45
+ declare function isUrl(query: string): boolean;
44
46
  declare function search(query: string, limit?: number): Promise<DeezerSearchTrackResponse>;
45
47
  type ArrayOrObject<T> = T | T[];
46
48
  interface DeezerSearchTrackResponse {
package/dist/index.js CHANGED
@@ -61,10 +61,10 @@ async function getCrypto() {
61
61
  }
62
62
  return rawCrypto;
63
63
  }
64
- async function isUrl(query) {
64
+ function isUrl(query) {
65
65
  try {
66
- new URL(query);
67
- return true;
66
+ const url = new URL(query);
67
+ return url.protocol === "http" || url.protocol === "https";
68
68
  } catch {
69
69
  return false;
70
70
  }
@@ -202,37 +202,62 @@ function validate(query) {
202
202
  }
203
203
 
204
204
  // src/DeezerExtractor.ts
205
+ var import_promises = require("timers/promises");
205
206
  var Warnings = {
206
207
  MissingDecryption: "Decryption Key missing! This is needed for extracting streams."
207
208
  };
209
+ async function fetchDeezerAnonUser() {
210
+ const crypto = await getCrypto();
211
+ const randomToken = crypto.randomBytes(16).toString("hex");
212
+ const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`;
213
+ const usrDataRes = await fetch(deezerUsrDataUrl);
214
+ const usrData = await usrDataRes.json();
215
+ const licenseToken = usrData.results.USER.OPTIONS.license_token;
216
+ if (typeof licenseToken !== "string") throw new Error("Unable to get licenseToken");
217
+ const csrfToken = usrData.results.checkForm;
218
+ if (typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption");
219
+ return {
220
+ cookie: usrDataRes.headers.get("set-cookie"),
221
+ licenseToken,
222
+ csrfToken,
223
+ mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com"
224
+ };
225
+ }
208
226
  var DeezerExtractor = class extends import_discord_player2.BaseExtractor {
209
227
  async activate() {
210
228
  this.protocols = ["deezer"];
211
229
  if (!this.options.decryptionKey) process.emitWarning(Warnings.MissingDecryption);
212
230
  else {
213
- const crypto = await getCrypto();
214
- const randomToken = crypto.randomBytes(16).toString("hex");
215
- const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`;
216
- const usrDataRes = await fetch(deezerUsrDataUrl);
217
- const usrData = await usrDataRes.json();
218
- const licenseToken = usrData.results.USER.OPTIONS.license_token;
219
- if (typeof licenseToken !== "string") throw new Error("Unable to get licenseToken");
220
- const csrfToken = usrData.results.checkForm;
221
- if (typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption");
222
- this.userInfo = {
223
- mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com",
224
- cookie: usrDataRes.headers.get("set-cookie"),
225
- licenseToken,
226
- csrfToken
227
- };
231
+ this.userInfo = await fetchDeezerAnonUser();
228
232
  this.context.player.debug("USER INFO EXT: " + JSON.stringify(this.userInfo));
229
233
  }
234
+ this.__interval = setInterval(
235
+ async () => {
236
+ try {
237
+ this.userInfo = await fetchDeezerAnonUser();
238
+ } catch {
239
+ this.context.player.debug("[DEEZER_EXTRACTOR_ERR]: Unable to fetch user information from Deezer. Retrying in 3 seconds ...");
240
+ await (0, import_promises.setTimeout)(
241
+ 3e3
242
+ /* 3 seconds */
243
+ );
244
+ try {
245
+ this.userInfo = await fetchDeezerAnonUser();
246
+ } catch (err) {
247
+ this.context.player.debug("[DEEZER_EXTRACTOR_ERR]: Retry failed. Using old user ...");
248
+ }
249
+ }
250
+ },
251
+ this.options.reloadUserInterval || 864e5
252
+ /* one day */
253
+ ).unref();
230
254
  }
231
255
  async deactivate() {
256
+ if (this.__interval) clearInterval(this.__interval);
232
257
  this.protocols = [];
233
258
  }
234
259
  async validate(query, type) {
235
- return validate(query) || type === "deezer";
260
+ return validate(query) || type === "deezer" || !isUrl(query);
236
261
  }
237
262
  buildPlaylistData(data, handleContext) {
238
263
  const raw = data.url.split("/");
@@ -253,7 +278,7 @@ var DeezerExtractor = class extends import_discord_player2.BaseExtractor {
253
278
  });
254
279
  }
255
280
  async handle(query, context) {
256
- if (context.protocol === "deezer" && !isUrl(query)) {
281
+ if (!isUrl(query)) {
257
282
  const rawSearch = await search(query);
258
283
  const tracks = buildTrackFromSearch(rawSearch, this.context.player, context.requestedBy);
259
284
  return this.createResponse(null, tracks);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discord-player-deezer",
3
- "version": "2.2.1-dev.2",
3
+ "version": "2.3.1-dev.0",
4
4
  "description": "A custom extractor made for discord-player that enables you to extract from Deezer.",
5
5
  "main": "dist/index.js",
6
6
  "author": "retro_ig",
@@ -1,6 +1,7 @@
1
1
  import { BaseExtractor, ExtractorSearchContext, ExtractorStreamable, Track, Playlist, Util as DPUtil, ExtractorInfo, SearchQueryType } from "discord-player"
2
2
  import { Playlist as DeezerPlaylist, Track as DeezerTrack, getData } from "@mithron/deezer-music-metadata";
3
3
  import { buildTrackFromSearch, deezerRegex, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate } from "./utils/util";
4
+ import { setTimeout } from "node:timers/promises"
4
5
 
5
6
  /**
6
7
  * -------------------------------NOTICE-------------------------------------
@@ -15,6 +16,7 @@ export type DeezerExtractorOptions = {
15
16
  decryptionKey?: string; // needed for decrypting deezer songs
16
17
  createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>;
17
18
  searchLimit?: number;
19
+ reloadUserInterval?: number;
18
20
  }
19
21
 
20
22
  export type DeezerUserInfo = {
@@ -29,46 +31,67 @@ export const Warnings = {
29
31
  } as const
30
32
  export type Warnings = (typeof Warnings)[keyof typeof Warnings]
31
33
 
34
+ async function fetchDeezerAnonUser(): Promise<DeezerUserInfo> {
35
+ // extract deezer username
36
+ // dynamically load crypto because some might not want streaming
37
+ const crypto = await getCrypto()
38
+ const randomToken = crypto.randomBytes(16).toString("hex")
39
+ const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`
40
+
41
+ const usrDataRes = await fetch(deezerUsrDataUrl)
42
+ const usrData = await usrDataRes.json()
43
+
44
+ const licenseToken = usrData.results.USER.OPTIONS.license_token
45
+ if(typeof licenseToken !== "string") throw new Error("Unable to get licenseToken")
46
+
47
+ const csrfToken = usrData.results.checkForm
48
+ if(typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption")
49
+
50
+ return {
51
+ cookie: usrDataRes.headers.get("set-cookie")!,
52
+ licenseToken,
53
+ csrfToken,
54
+ mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com",
55
+ }
56
+ }
57
+
32
58
  export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
33
- static identifier: string = "com.retrouser955.discord-player.deezr-ext"
59
+ static readonly identifier: string = "com.retrouser955.discord-player.deezr-ext"
34
60
  userInfo!: DeezerUserInfo
61
+ /* tslint:disable-next-line */
62
+ __interval?: ReturnType<typeof setInterval>
35
63
 
36
64
  async activate(): Promise<void> {
37
65
  this.protocols = ["deezer"]
38
66
  if(!this.options.decryptionKey) process.emitWarning(Warnings.MissingDecryption)
39
67
  else {
40
- // extract deezer username
41
- // dynamically load crypto because some might not want streaming
42
- const crypto = await getCrypto()
43
- const randomToken = crypto.randomBytes(16).toString("hex")
44
- const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`
45
-
46
- const usrDataRes = await fetch(deezerUsrDataUrl)
47
- const usrData = await usrDataRes.json()
48
-
49
- const licenseToken = usrData.results.USER.OPTIONS.license_token
50
- if(typeof licenseToken !== "string") throw new Error("Unable to get licenseToken")
51
-
52
- const csrfToken = usrData.results.checkForm
53
- if(typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption")
54
-
55
- this.userInfo = {
56
- mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com",
57
- cookie: usrDataRes.headers.get("set-cookie")!,
58
- licenseToken,
59
- csrfToken
60
- }
68
+ this.userInfo = await fetchDeezerAnonUser()
61
69
 
62
70
  this.context.player.debug('USER INFO EXT: ' + JSON.stringify(this.userInfo))
63
71
  }
72
+
73
+ this.__interval = setInterval(async () => {
74
+ try {
75
+ this.userInfo = await fetchDeezerAnonUser()
76
+ } catch {
77
+ this.context.player.debug("[DEEZER_EXTRACTOR_ERR]: Unable to fetch user information from Deezer. Retrying in 3 seconds ...")
78
+ await setTimeout(3000/* 3 seconds */)
79
+ try {
80
+ this.userInfo = await fetchDeezerAnonUser()
81
+ } catch (err) {
82
+ this.context.player.debug("[DEEZER_EXTRACTOR_ERR]: Retry failed. Using old user ...")
83
+ }
84
+ }
85
+ }, this.options.reloadUserInterval || 8.64e+7/* one day */).unref()
64
86
  }
65
87
 
66
88
  async deactivate() {
89
+ if(this.__interval) clearInterval(this.__interval)
67
90
  this.protocols = []
68
91
  }
69
92
 
70
93
  async validate(query: string, type: SearchQueryType & "deezer"): Promise<boolean> {
71
- return validate(query) || type === "deezer"
94
+ return validate(query) || type === "deezer" || !isUrl(query)
72
95
  }
73
96
 
74
97
  buildPlaylistData(data: DeezerPlaylist, handleContext: ExtractorSearchContext) {
@@ -92,7 +115,7 @@ export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
92
115
  }
93
116
 
94
117
  async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
95
- if(context.protocol === "deezer" && !isUrl(query)) {
118
+ if(!isUrl(query)) {
96
119
  const rawSearch = await search(query)
97
120
  const tracks = buildTrackFromSearch(rawSearch, this.context.player, context.requestedBy)
98
121
  return this.createResponse(null, tracks)
package/src/utils/util.ts CHANGED
@@ -16,11 +16,10 @@ export async function getCrypto() {
16
16
  return rawCrypto
17
17
  }
18
18
 
19
- export async function isUrl(query: string) {
19
+ export function isUrl(query: string) {
20
20
  try {
21
- /* tslint:disable-next-line no-unused-expression */
22
- new URL(query)
23
- return true
21
+ const url = new URL(query)
22
+ return url.protocol === "http" || url.protocol === "https"
24
23
  } catch {
25
24
  return false
26
25
  }