discord-player-deezer 2.1.0 → 2.2.1-dev.1

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.
@@ -1,131 +1,138 @@
1
- import { BaseExtractor, ExtractorSearchContext, ExtractorStreamable, Track, Playlist, Util as DPUtil, ExtractorInfo } from "discord-player"
2
- import { Playlist as DeezerPlaylist, Track as DeezerTrack, getData } from "@mithron/deezer-music-metadata";
3
- import { buildTrackFromSearch, deezerRegex, getCrypto, searchOneTrack, streamTrack, validate } from "./utils/util";
4
-
5
- /**
6
- * -------------------------------NOTICE-------------------------------------
7
- * STREAMING CODE IS MAINLY TAKEN FROM https://github.com/PerformanC/NodeLink
8
- * WHICH HAS THE "BSD 2-Clause "Simplified" License"
9
- *
10
- * PLEASE SHOW THE ORIGINAL AUTHOR SUPPORT BY STARING HIS GITHUB REPO
11
- * --------------------------------------------------------------------------
12
- */
13
-
14
- export type DeezerExtractorOptions = {
15
- decryptionKey?: string; // needed for decrypting deezer songs
16
- createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>
17
- }
18
-
19
- export type DeezerUserInfo = {
20
- cookie: string;
21
- licenseToken: string;
22
- csrfToken: string;
23
- mediaUrl: string;
24
- }
25
-
26
- export const Warnings = {
27
- MissingDecryption: "Decryption Key missing! This is needed for extracting streams."
28
- } as const
29
- export type Warnings = (typeof Warnings)[keyof typeof Warnings]
30
-
31
- export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
32
- static identifier: string = "com.retrouser955.discord-player.deezr-ext"
33
- userInfo!: DeezerUserInfo
34
-
35
- async activate(): Promise<void> {
36
- if(!this.options.decryptionKey) process.emitWarning(Warnings.MissingDecryption)
37
- else {
38
- // extract deezer username
39
- // dynamically load crypto because some might not want streaming
40
- const crypto = await getCrypto()
41
- const randomToken = crypto.randomBytes(16).toString("hex")
42
- const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`
43
-
44
- const usrDataRes = await fetch(deezerUsrDataUrl)
45
- const usrData = await usrDataRes.json()
46
-
47
- const licenseToken = usrData.results.USER.OPTIONS.license_token
48
- if(typeof licenseToken !== "string") throw new Error("Unable to get licenseToken")
49
-
50
- const csrfToken = usrData.results.checkForm
51
- if(typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption")
52
-
53
- this.userInfo = {
54
- mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com",
55
- cookie: usrDataRes.headers.get("set-cookie")!,
56
- licenseToken,
57
- csrfToken
58
- }
59
-
60
- this.context.player.debug('USER INFO EXT: ' + JSON.stringify(this.userInfo))
61
- }
62
- }
63
-
64
- async validate(query: string): Promise<boolean> {
65
- return validate(query)
66
- }
67
-
68
- buildPlaylistData(data: DeezerPlaylist, handleContext: ExtractorSearchContext) {
69
- const raw: string[] = data.url.split("/")
70
- const identifier: string = raw[raw.length - 1].split("?")[0]
71
-
72
- return new Playlist(this.context.player, {
73
- title: data.name,
74
- thumbnail: data.thumbnail[0].url,
75
- author: {
76
- name: data.artist.name,
77
- url: data.artist.url
78
- },
79
- type: data.type,
80
- tracks: data.tracks.map((v) => this.buildTrack(v, handleContext)),
81
- description: "",
82
- source: "arbitrary",
83
- id: identifier,
84
- url: data.url
85
- })
86
- }
87
-
88
- async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
89
- if (deezerRegex.share.test(query)) {
90
- const redirect = await fetch(query);
91
- query = redirect.url; // follow the redirect of deezer page links
92
- }
93
- const metadata = await getData(query)
94
-
95
- if(metadata?.type === "song") return this.createResponse(null, [this.buildTrack(metadata, context)])
96
- if(metadata?.type === "playlist" || metadata?.type === "album") {
97
- const playlist = this.buildPlaylistData(metadata, context)
98
-
99
- return this.createResponse(playlist, playlist.tracks)
100
- }
101
-
102
- throw new Error("Unable to get data from Deezer")
103
- }
104
-
105
- buildTrack(track: DeezerTrack, { requestedBy }: ExtractorSearchContext) {
106
- return new Track(this.context.player, {
107
- title: track.name,
108
- author: track.author.map((v) => v.name).join(", "),
109
- url: track.url,
110
- source: "arbitrary",
111
- thumbnail: track.thumbnail[0].url,
112
- duration: DPUtil.buildTimeCode(DPUtil.parseMS(track.duration)),
113
- requestedBy
114
- })
115
- }
116
-
117
- async bridge(track: Track): Promise<ExtractorStreamable | null> {
118
- const deezerTrack = await searchOneTrack(`${track.author} ${track.source === "youtube" ? track.cleanTitle : track.title}`)
119
-
120
- if(!deezerTrack) return null
121
- const dpTrack = buildTrackFromSearch(deezerTrack, this.context.player, track.requestedBy)
122
-
123
- const stream = this.stream(dpTrack[0])
124
-
125
- return stream
126
- }
127
-
128
- stream(info: Track): Promise<ExtractorStreamable> {
129
- return streamTrack(info, this)
130
- }
131
- }
1
+ import { BaseExtractor, ExtractorSearchContext, ExtractorStreamable, Track, Playlist, Util as DPUtil, ExtractorInfo, SearchQueryType } from "discord-player"
2
+ import { Playlist as DeezerPlaylist, Track as DeezerTrack, getData } from "@mithron/deezer-music-metadata";
3
+ import { buildTrackFromSearch, deezerRegex, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate } from "./utils/util";
4
+
5
+ /**
6
+ * -------------------------------NOTICE-------------------------------------
7
+ * STREAMING CODE IS MAINLY TAKEN FROM https://github.com/PerformanC/NodeLink
8
+ * WHICH HAS THE "BSD 2-Clause "Simplified" License"
9
+ *
10
+ * PLEASE SHOW THE ORIGINAL AUTHOR SUPPORT BY STARING HIS GITHUB REPO
11
+ * --------------------------------------------------------------------------
12
+ */
13
+
14
+ export type DeezerExtractorOptions = {
15
+ decryptionKey?: string; // needed for decrypting deezer songs
16
+ createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>;
17
+ searchLimit?: number;
18
+ }
19
+
20
+ export type DeezerUserInfo = {
21
+ cookie: string;
22
+ licenseToken: string;
23
+ csrfToken: string;
24
+ mediaUrl: string;
25
+ }
26
+
27
+ export const Warnings = {
28
+ MissingDecryption: "Decryption Key missing! This is needed for extracting streams."
29
+ } as const
30
+ export type Warnings = (typeof Warnings)[keyof typeof Warnings]
31
+
32
+ export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
33
+ static identifier: string = "com.retrouser955.discord-player.deezr-ext"
34
+ userInfo!: DeezerUserInfo
35
+
36
+ async activate(): Promise<void> {
37
+ if(!this.options.decryptionKey) process.emitWarning(Warnings.MissingDecryption)
38
+ else {
39
+ // extract deezer username
40
+ // dynamically load crypto because some might not want streaming
41
+ const crypto = await getCrypto()
42
+ const randomToken = crypto.randomBytes(16).toString("hex")
43
+ const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`
44
+
45
+ const usrDataRes = await fetch(deezerUsrDataUrl)
46
+ const usrData = await usrDataRes.json()
47
+
48
+ const licenseToken = usrData.results.USER.OPTIONS.license_token
49
+ if(typeof licenseToken !== "string") throw new Error("Unable to get licenseToken")
50
+
51
+ const csrfToken = usrData.results.checkForm
52
+ if(typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption")
53
+
54
+ this.userInfo = {
55
+ mediaUrl: usrData.results.URL_MEDIA || "https://media.deezer.com",
56
+ cookie: usrDataRes.headers.get("set-cookie")!,
57
+ licenseToken,
58
+ csrfToken
59
+ }
60
+
61
+ this.context.player.debug('USER INFO EXT: ' + JSON.stringify(this.userInfo))
62
+ }
63
+ }
64
+
65
+ async validate(query: string, type: SearchQueryType & "deezer"): Promise<boolean> {
66
+ return validate(query) || type === "deezer"
67
+ }
68
+
69
+ buildPlaylistData(data: DeezerPlaylist, handleContext: ExtractorSearchContext) {
70
+ const raw: string[] = data.url.split("/")
71
+ const identifier: string = raw[raw.length - 1].split("?")[0]
72
+
73
+ return new Playlist(this.context.player, {
74
+ title: data.name,
75
+ thumbnail: data.thumbnail[0].url,
76
+ author: {
77
+ name: data.artist.name,
78
+ url: data.artist.url
79
+ },
80
+ type: data.type,
81
+ tracks: data.tracks.map((v) => this.buildTrack(v, handleContext)),
82
+ description: "",
83
+ source: "arbitrary",
84
+ id: identifier,
85
+ url: data.url
86
+ })
87
+ }
88
+
89
+ async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
90
+ if(context.protocol === "deezer" && !isUrl(query)) {
91
+ const rawSearch = await search(query)
92
+ const tracks = buildTrackFromSearch(rawSearch, this.context.player, context.requestedBy)
93
+ return this.createResponse(null, tracks)
94
+ }
95
+
96
+ if (deezerRegex.share.test(query)) {
97
+ const redirect = await fetch(query);
98
+ query = redirect.url; // follow the redirect of deezer page links
99
+ }
100
+ const metadata = await getData(query)
101
+
102
+ if(metadata?.type === "song") return this.createResponse(null, [this.buildTrack(metadata, context)])
103
+ if(metadata?.type === "playlist" || metadata?.type === "album") {
104
+ const playlist = this.buildPlaylistData(metadata, context)
105
+
106
+ return this.createResponse(playlist, playlist.tracks)
107
+ }
108
+
109
+ throw new Error("Unable to get data from Deezer")
110
+ }
111
+
112
+ buildTrack(track: DeezerTrack, { requestedBy }: ExtractorSearchContext) {
113
+ return new Track(this.context.player, {
114
+ title: track.name,
115
+ author: track.author.map((v) => v.name).join(", "),
116
+ url: track.url,
117
+ source: "arbitrary",
118
+ thumbnail: track.thumbnail[0].url,
119
+ duration: DPUtil.buildTimeCode(DPUtil.parseMS(track.duration)),
120
+ requestedBy
121
+ })
122
+ }
123
+
124
+ async bridge(track: Track): Promise<ExtractorStreamable | null> {
125
+ const deezerTrack = await searchOneTrack(`${track.author} ${track.source === "youtube" ? track.cleanTitle : track.title}`)
126
+
127
+ if(!deezerTrack) return null
128
+ const dpTrack = buildTrackFromSearch(deezerTrack, this.context.player, track.requestedBy)
129
+
130
+ const stream = this.stream(dpTrack[0])
131
+
132
+ return stream
133
+ }
134
+
135
+ stream(info: Track): Promise<ExtractorStreamable> {
136
+ return streamTrack(info, this)
137
+ }
138
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./DeezerExtractor"
1
+ export * from "./DeezerExtractor"
2
2
  export * from "./utils/util"