discord-player-deezer 2.3.1-dev.2 → 2.5.0-beta.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/.yarnrc.yml +1 -1
- package/LICENSE +121 -121
- package/dist/index.d.ts +23 -1
- package/dist/index.js +86 -15
- package/package.json +36 -35
- package/readme.md +34 -34
- package/src/Crypto/BaseDecryptor.ts +12 -0
- package/src/Crypto/JSDecryptor.ts +18 -0
- package/src/Crypto/NodeDecryptor.ts +17 -0
- package/src/Crypto/index.ts +3 -0
- package/src/DeezerExtractor.ts +198 -171
- package/src/index.ts +3 -2
- package/src/utils/util.ts +218 -215
- package/tsconfig.json +24 -24
- package/tslint.json +2 -2
- package/tsup.config.ts +8 -8
package/src/utils/util.ts
CHANGED
|
@@ -1,215 +1,218 @@
|
|
|
1
|
-
import { type Player, Track, Util } from "discord-player";
|
|
2
|
-
import type { DeezerExtractor } from "../DeezerExtractor";
|
|
3
|
-
import { Readable, PassThrough } from 'stream'
|
|
4
|
-
import Blowfish from "blowfish-node";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
cipher: 'BF_CBC_STRIPE',
|
|
142
|
-
format: '
|
|
143
|
-
}, {
|
|
144
|
-
cipher: 'BF_CBC_STRIPE',
|
|
145
|
-
format: '
|
|
146
|
-
}, {
|
|
147
|
-
cipher: 'BF_CBC_STRIPE',
|
|
148
|
-
format: '
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
1
|
+
import { type Player, Track, Util } from "discord-player";
|
|
2
|
+
import type { DeezerExtractor } from "../DeezerExtractor";
|
|
3
|
+
import { Readable, PassThrough } from 'stream'
|
|
4
|
+
import Blowfish from "blowfish-node";
|
|
5
|
+
import { JSDecryptor } from "../Crypto/JSDecryptor";
|
|
6
|
+
|
|
7
|
+
const IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x))
|
|
8
|
+
|
|
9
|
+
let rawCrypto: typeof import("crypto")
|
|
10
|
+
|
|
11
|
+
export async function getCrypto() {
|
|
12
|
+
if (!rawCrypto) {
|
|
13
|
+
rawCrypto = await import("crypto")
|
|
14
|
+
return rawCrypto
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return rawCrypto
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isUrl(query: string) {
|
|
21
|
+
try {
|
|
22
|
+
const url = new URL(query)
|
|
23
|
+
return ['https:', 'http:'].includes(url.protocol)
|
|
24
|
+
} catch {
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DeezerAPIRoutes = {
|
|
30
|
+
searchTrack(query: string, limit?: number) {
|
|
31
|
+
const url = `https://api.deezer.com/search/track?q=${encodeURIComponent(query)}&${limit ? `limit=${limit}` : ""}`
|
|
32
|
+
|
|
33
|
+
return url
|
|
34
|
+
}
|
|
35
|
+
} as const
|
|
36
|
+
|
|
37
|
+
export async function search(query: string, limit: number = 10) {
|
|
38
|
+
const route = DeezerAPIRoutes.searchTrack(query, limit);
|
|
39
|
+
|
|
40
|
+
const response = await fetch(route)
|
|
41
|
+
|
|
42
|
+
if(!response.status.toString().startsWith("2")) throw new Error("Server responded with a non 2xx status.")
|
|
43
|
+
|
|
44
|
+
const jsonTrack = await response.json()
|
|
45
|
+
|
|
46
|
+
if(!jsonTrack.data || !Array.isArray(jsonTrack.data) || jsonTrack.data.length === 0) throw new Error("Unable get tracks from Deezer for the query '" + query + "'")
|
|
47
|
+
|
|
48
|
+
return jsonTrack as DeezerSearchTrackResponse
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type ArrayOrObject<T> = T | T[]
|
|
52
|
+
|
|
53
|
+
export interface DeezerSearchTrackResponse {
|
|
54
|
+
data: {
|
|
55
|
+
link: string,
|
|
56
|
+
title: string,
|
|
57
|
+
album: {
|
|
58
|
+
cover_xl?: string,
|
|
59
|
+
cover_medium?: string,
|
|
60
|
+
cover_small?: string,
|
|
61
|
+
cover?: string
|
|
62
|
+
},
|
|
63
|
+
artist: {
|
|
64
|
+
name: string
|
|
65
|
+
},
|
|
66
|
+
duration: number, // in seconds
|
|
67
|
+
type: "track"
|
|
68
|
+
}[]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function buildTrackFromSearch(track: DeezerSearchTrackResponse, player: Player, requestedBy?: Track['requestedBy']) {
|
|
72
|
+
const tracks = track.data.map((v) => new Track(player, {
|
|
73
|
+
source: "arbitrary",
|
|
74
|
+
duration: Util.buildTimeCode(Util.parseMS(v.duration * 1000)),
|
|
75
|
+
author: Array.isArray(v.artist) ? v.artist.map(a => a.name).join(", ") : v.artist.name,
|
|
76
|
+
url: v.link,
|
|
77
|
+
title: v.title,
|
|
78
|
+
thumbnail: v.album.cover_xl || v.album.cover_medium || v.album.cover || v.album.cover_small || "https://play-lh.googleusercontent.com/xtIOV8iPeAY4GrldTDo3CbLAEpbea2DqnfxaB4Nn2p5qz6KAiumY74qH86pgu1HA1A",
|
|
79
|
+
live: false,
|
|
80
|
+
requestedBy
|
|
81
|
+
}))
|
|
82
|
+
|
|
83
|
+
return tracks
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function extractTrackId(query: string) {
|
|
87
|
+
if (!validate(query)) throw new Error("Invalid track url")
|
|
88
|
+
const trackIdWithUrlParams = query.split("/").at(-1)
|
|
89
|
+
if (!trackIdWithUrlParams) throw new Error("Cannot find track ID")
|
|
90
|
+
return trackIdWithUrlParams.split("?")[0]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// used for bridging
|
|
94
|
+
export async function searchOneTrack(query: string) {
|
|
95
|
+
const url = DeezerAPIRoutes.searchTrack(query, 1)
|
|
96
|
+
|
|
97
|
+
const trackRes = await fetch(url)
|
|
98
|
+
if (trackRes.status !== 200) throw new Error("Fetch failed")
|
|
99
|
+
|
|
100
|
+
const trackData = await trackRes.json() as DeezerSearchTrackResponse | undefined
|
|
101
|
+
return trackData
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function streamTrack(track: Track, ext: DeezerExtractor) {
|
|
105
|
+
const decryptionKey = ext.options.decryptionKey
|
|
106
|
+
if (!decryptionKey) throw new Error("MISSING DEEZER DECRYPTION KEY") // cant decrypt due to missing key
|
|
107
|
+
|
|
108
|
+
const crypto = await getCrypto()
|
|
109
|
+
const trackId = extractTrackId(track.url)
|
|
110
|
+
|
|
111
|
+
const trackHash = crypto.createHash("md5").update(trackId, "ascii").digest("hex")
|
|
112
|
+
const trackKey = Buffer.alloc(16)
|
|
113
|
+
|
|
114
|
+
const Decryptor = ext.options.decryptor || JSDecryptor
|
|
115
|
+
|
|
116
|
+
for (let iter = 0; iter < 16; iter++)
|
|
117
|
+
/* tslint:disable-next-line no-bitwise */
|
|
118
|
+
trackKey.writeInt8(trackHash[iter].charCodeAt(0) ^ trackHash[iter + 16].charCodeAt(0) ^ decryptionKey[iter].charCodeAt(0), iter)
|
|
119
|
+
|
|
120
|
+
const trackInfoRes = await ext.fetch(`https://www.deezer.com/ajax/gw-light.php?method=song.getListData&input=3&api_version=1.0&api_token=${ext.userInfo.csrfToken}`, {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: {
|
|
123
|
+
Cookie: ext.userInfo.cookie,
|
|
124
|
+
'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0",
|
|
125
|
+
'DNT': '1'
|
|
126
|
+
},
|
|
127
|
+
body: JSON.stringify({
|
|
128
|
+
sng_ids: [trackId]
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const songInfo = (await trackInfoRes.json()).results.data[0]
|
|
133
|
+
|
|
134
|
+
const streamInfoRes = await fetch('https://media.deezer.com/v1/get_url', {
|
|
135
|
+
method: "POST",
|
|
136
|
+
body: JSON.stringify({
|
|
137
|
+
license_token: ext.userInfo.licenseToken,
|
|
138
|
+
media: [{
|
|
139
|
+
type: 'FULL',
|
|
140
|
+
formats: [{
|
|
141
|
+
cipher: 'BF_CBC_STRIPE',
|
|
142
|
+
format: 'FLAC'
|
|
143
|
+
}, {
|
|
144
|
+
cipher: 'BF_CBC_STRIPE',
|
|
145
|
+
format: 'MP3_256'
|
|
146
|
+
}, {
|
|
147
|
+
cipher: 'BF_CBC_STRIPE',
|
|
148
|
+
format: 'MP3_128'
|
|
149
|
+
}, {
|
|
150
|
+
cipher: 'BF_CBC_STRIPE',
|
|
151
|
+
format: 'MP3_MISC'
|
|
152
|
+
}]
|
|
153
|
+
}],
|
|
154
|
+
track_tokens: [songInfo.TRACK_TOKEN]
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
const streamInfo = await streamInfoRes.json()
|
|
158
|
+
|
|
159
|
+
const trackMediaUrl = streamInfo.data[0].media[0].sources[0].url
|
|
160
|
+
|
|
161
|
+
ext.context.player.debug("DEEZER FOUND MEDIA URL " + trackMediaUrl)
|
|
162
|
+
|
|
163
|
+
const trackReq = await ext.fetch(trackMediaUrl)
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
const readable = Readable.fromWeb(trackReq.body)
|
|
166
|
+
let buffer = Buffer.alloc(0)
|
|
167
|
+
let i = 0
|
|
168
|
+
|
|
169
|
+
const bufferSize = 2048 // 2mb
|
|
170
|
+
|
|
171
|
+
const passThrough = new PassThrough()
|
|
172
|
+
|
|
173
|
+
const decryptor = new Decryptor(trackKey, IV)
|
|
174
|
+
|
|
175
|
+
readable.on("readable", async () => {
|
|
176
|
+
let chunk = null;
|
|
177
|
+
while (true) {
|
|
178
|
+
chunk = readable.read(bufferSize)
|
|
179
|
+
|
|
180
|
+
if (!chunk) {
|
|
181
|
+
if (readable.readableLength) {
|
|
182
|
+
chunk = readable.read(readable.readableLength)
|
|
183
|
+
buffer = Buffer.concat([buffer, chunk])
|
|
184
|
+
}
|
|
185
|
+
break;
|
|
186
|
+
} else {
|
|
187
|
+
buffer = Buffer.concat([buffer, chunk])
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
while (buffer.length >= bufferSize) {
|
|
191
|
+
const bufferSized = buffer.subarray(0, bufferSize)
|
|
192
|
+
|
|
193
|
+
if (i % 3 === 0) {
|
|
194
|
+
const decipher = await decryptor.decrypt(bufferSized)
|
|
195
|
+
passThrough.write(decipher)
|
|
196
|
+
} else {
|
|
197
|
+
passThrough.write(bufferSized)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
i++
|
|
201
|
+
|
|
202
|
+
buffer = buffer.subarray(bufferSize)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
return passThrough
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export const deezerRegex = {
|
|
211
|
+
track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
|
|
212
|
+
playlistNalbums: /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/,
|
|
213
|
+
share: /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/
|
|
214
|
+
} as const;
|
|
215
|
+
|
|
216
|
+
export function validate(query: string) {
|
|
217
|
+
return deezerRegex.track.test(query) ?? deezerRegex.playlistNalbums.test(query) ?? deezerRegex.share.test(query)
|
|
218
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
-
"display": "Default",
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"target": "ES2020",
|
|
6
|
-
"moduleResolution": "node",
|
|
7
|
-
"module": "commonjs",
|
|
8
|
-
"declaration": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"strict": true,
|
|
11
|
-
"strictNullChecks": true,
|
|
12
|
-
"esModuleInterop": true,
|
|
13
|
-
"pretty": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"skipDefaultLibCheck": true,
|
|
16
|
-
"forceConsistentCasingInFileNames": true
|
|
17
|
-
},
|
|
18
|
-
"exclude": [
|
|
19
|
-
"node_modules",
|
|
20
|
-
"dist/**/*.ts"
|
|
21
|
-
],
|
|
22
|
-
"include": [
|
|
23
|
-
"./src/**/*.ts"
|
|
24
|
-
]
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"display": "Default",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"target": "ES2020",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"module": "commonjs",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"strictNullChecks": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"pretty": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"skipDefaultLibCheck": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true
|
|
17
|
+
},
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist/**/*.ts"
|
|
21
|
+
],
|
|
22
|
+
"include": [
|
|
23
|
+
"./src/**/*.ts"
|
|
24
|
+
]
|
|
25
25
|
}
|
package/tslint.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["tslint:recommended", "tslint-config-prettier"]
|
|
1
|
+
{
|
|
2
|
+
"extends": ["tslint:recommended", "tslint-config-prettier"]
|
|
3
3
|
}
|
package/tsup.config.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { defineConfig } from "tsup"
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
format: "cjs",
|
|
5
|
-
entry: ['./src/index.ts'],
|
|
6
|
-
outDir: "./dist",
|
|
7
|
-
skipNodeModulesBundle: true,
|
|
8
|
-
dts: true
|
|
1
|
+
import { defineConfig } from "tsup"
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
format: "cjs",
|
|
5
|
+
entry: ['./src/index.ts'],
|
|
6
|
+
outDir: "./dist",
|
|
7
|
+
skipNodeModulesBundle: true,
|
|
8
|
+
dts: true
|
|
9
9
|
})
|