discord-player-deezer 2.0.0-beta.1 → 2.0.0-beta.2
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/package.json +1 -1
- package/.github/workflows/publish-npm.yml +0 -17
- package/src/DeezerExtractor.ts +0 -107
- package/src/index.ts +0 -2
- package/src/utils/util.ts +0 -144
package/package.json
CHANGED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
name: publish dp deezer
|
|
2
|
-
on:
|
|
3
|
-
release:
|
|
4
|
-
types: [published]
|
|
5
|
-
|
|
6
|
-
jobs:
|
|
7
|
-
publish-npm:
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
steps:
|
|
10
|
-
- uses: actions/checkout@v2
|
|
11
|
-
- uses: actions/setup-node@v1
|
|
12
|
-
with:
|
|
13
|
-
node-version: 20
|
|
14
|
-
registry-url: https://registry.npmjs.org/
|
|
15
|
-
- run: yarn npm:publish
|
|
16
|
-
env:
|
|
17
|
-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/src/DeezerExtractor.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
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 { getCrypto, 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 class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
|
|
27
|
-
static identifier: string = "com.retrouser955.discord-player.deezr-ext"
|
|
28
|
-
userInfo!: DeezerUserInfo
|
|
29
|
-
|
|
30
|
-
async activate(): Promise<void> {
|
|
31
|
-
if(!this.options.decryptionKey) process.emitWarning("Decryption Key missing! This is needed for extracting streams.")
|
|
32
|
-
else {
|
|
33
|
-
// extract deezer username
|
|
34
|
-
// dynamically load crypto because some might not want streaming
|
|
35
|
-
const crypto = await getCrypto()
|
|
36
|
-
const randomToken = crypto.randomBytes(16).toString("hex")
|
|
37
|
-
const deezerUsrDataUrl = `https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token=${randomToken}`
|
|
38
|
-
|
|
39
|
-
const usrDataRes = await fetch(deezerUsrDataUrl)
|
|
40
|
-
const cookie = usrDataRes.headers.get("set-cookie")!
|
|
41
|
-
this.userInfo.cookie = cookie
|
|
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
|
-
this.userInfo.licenseToken = licenseToken
|
|
47
|
-
|
|
48
|
-
const csrfToken = usrData.results.checkForm
|
|
49
|
-
if(typeof csrfToken !== "string") throw new Error("Unable to get csrf token which is required for decryption")
|
|
50
|
-
|
|
51
|
-
this.userInfo.mediaUrl = usrData.results.URL_MEDIA || "https://media.deezer.com"
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async validate(query: string): Promise<boolean> {
|
|
56
|
-
return validate(query)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
buildPlaylistData(data: DeezerPlaylist, handleContext: ExtractorSearchContext) {
|
|
60
|
-
const raw: string[] = data.url.split("/")
|
|
61
|
-
const identifier: string = raw[raw.length - 1].split("?")[0]
|
|
62
|
-
|
|
63
|
-
return new Playlist(this.context.player, {
|
|
64
|
-
title: data.name,
|
|
65
|
-
thumbnail: data.thumbnail[0].url,
|
|
66
|
-
author: {
|
|
67
|
-
name: data.artist.name,
|
|
68
|
-
url: data.artist.url
|
|
69
|
-
},
|
|
70
|
-
type: data.type,
|
|
71
|
-
tracks: data.tracks.map((v) => this.buildTrack(v, handleContext)),
|
|
72
|
-
description: "",
|
|
73
|
-
source: "arbitrary",
|
|
74
|
-
id: identifier,
|
|
75
|
-
url: data.url
|
|
76
|
-
})
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
|
|
80
|
-
const metadata = await getData(query)
|
|
81
|
-
|
|
82
|
-
if(metadata?.type === "song") return this.createResponse(null, [this.buildTrack(metadata, context)])
|
|
83
|
-
if(metadata?.type === "playlist" || metadata?.type === "album") {
|
|
84
|
-
const playlist = this.buildPlaylistData(metadata, context)
|
|
85
|
-
|
|
86
|
-
return this.createResponse(playlist, playlist.tracks)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
throw new Error("Unable to get data from Deezer")
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
buildTrack(track: DeezerTrack, handleContext: ExtractorSearchContext) {
|
|
93
|
-
return new Track(this.context.player, {
|
|
94
|
-
title: track.name,
|
|
95
|
-
author: track.author.map((v) => v.name).join(", "),
|
|
96
|
-
url: track.url,
|
|
97
|
-
source: "arbitrary",
|
|
98
|
-
thumbnail: track.thumbnail[0].url,
|
|
99
|
-
duration: DPUtil.buildTimeCode(DPUtil.parseMS(track.duration)),
|
|
100
|
-
requestedBy: handleContext.requestedBy
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
stream(info: Track): Promise<ExtractorStreamable> {
|
|
105
|
-
return streamTrack(info, this)
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/index.ts
DELETED
package/src/utils/util.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { Track } from "discord-player";
|
|
2
|
-
import type { DeezerExtractor } from "../DeezerExtractor";
|
|
3
|
-
import { Readable, PassThrough } from 'stream'
|
|
4
|
-
import type { BinaryLike, CipherGCMTypes, CipherKey, Decipher } from "crypto";
|
|
5
|
-
|
|
6
|
-
const IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x))
|
|
7
|
-
|
|
8
|
-
let rawCrypto: typeof import("crypto")
|
|
9
|
-
|
|
10
|
-
export async function getCrypto() {
|
|
11
|
-
if (!rawCrypto) {
|
|
12
|
-
rawCrypto = await import("crypto")
|
|
13
|
-
return rawCrypto
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return rawCrypto
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export async function streamTrack(track: Track, ext: DeezerExtractor) {
|
|
20
|
-
const trackIdWithUrlParams = track.url.split("/").at(-1)
|
|
21
|
-
if (!trackIdWithUrlParams || !validate(track.url)) throw new Error("INVALID TRACK") // not a deezer track
|
|
22
|
-
|
|
23
|
-
const decryptionKey = ext.options.decryptionKey
|
|
24
|
-
if (!decryptionKey) throw new Error("MISSING DEEZER DECRYPTION KEY") // cant decrypt due to missing key
|
|
25
|
-
|
|
26
|
-
const crypto = await getCrypto()
|
|
27
|
-
const trackId = trackIdWithUrlParams.split("?")[0]
|
|
28
|
-
|
|
29
|
-
const trackHash = crypto.createHash("md5").update(trackId, "ascii").digest("hex")
|
|
30
|
-
const trackKey = Buffer.alloc(16)
|
|
31
|
-
|
|
32
|
-
for (let iter = 0; iter < 16; iter++)
|
|
33
|
-
/* tslint:disable-next-line no-bitwise */
|
|
34
|
-
trackKey.writeInt8(trackHash[iter].charCodeAt(0) ^ trackHash[iter + 16].charCodeAt(0) ^ decryptionKey[iter].charCodeAt(0), iter)
|
|
35
|
-
|
|
36
|
-
const trackInfoRes = await fetch(`https://www.deezer.com/ajax/gw-light.php?method=song.getListData&input=3&api_version=1.0&api_token=${ext.userInfo.csrfToken}`, {
|
|
37
|
-
method: "POST",
|
|
38
|
-
headers: {
|
|
39
|
-
Cookie: ext.userInfo.cookie,
|
|
40
|
-
'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0",
|
|
41
|
-
'DNT': '1'
|
|
42
|
-
},
|
|
43
|
-
body: JSON.stringify({
|
|
44
|
-
sng_ids: [trackId]
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
const trackInfo = await trackInfoRes.json()
|
|
49
|
-
|
|
50
|
-
// no streaming data found
|
|
51
|
-
if (Array.isArray(trackInfo.error) && (trackInfo.error as unknown[]).length !== 0) throw new Error("Unable to extract track\n" + JSON.stringify(trackInfo.error))
|
|
52
|
-
if (typeof trackInfo.error === "object" && Object.keys(trackInfo.error).length !== 0) throw new Error("Unable to extract track\n" + JSON.stringify(trackInfo.error))
|
|
53
|
-
|
|
54
|
-
const info = trackInfo.data[0]
|
|
55
|
-
|
|
56
|
-
const streamInfoRes = await fetch(`${ext.userInfo.mediaUrl}/v1/get-url`, {
|
|
57
|
-
method: "POST",
|
|
58
|
-
body: JSON.stringify({
|
|
59
|
-
license_token: ext.userInfo.licenseToken,
|
|
60
|
-
media: [{
|
|
61
|
-
type: 'FULL',
|
|
62
|
-
formats: [{
|
|
63
|
-
cipher: 'BF_CBC_STRIPE',
|
|
64
|
-
format: 'FLAC'
|
|
65
|
-
}, {
|
|
66
|
-
cipher: 'BF_CBC_STRIPE',
|
|
67
|
-
format: 'MP3_256'
|
|
68
|
-
}, {
|
|
69
|
-
cipher: 'BF_CBC_STRIPE',
|
|
70
|
-
format: 'MP3_128'
|
|
71
|
-
}, {
|
|
72
|
-
cipher: 'BF_CBC_STRIPE',
|
|
73
|
-
format: 'MP3_MISC'
|
|
74
|
-
}]
|
|
75
|
-
}],
|
|
76
|
-
track_tokens: [info.TRACK_TOKEN]
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
const streamInfo = await streamInfoRes.json()
|
|
81
|
-
|
|
82
|
-
const trackMediaUrl = streamInfo.data[0].media[0].sources[0].url
|
|
83
|
-
|
|
84
|
-
const trackReq = await fetch(trackMediaUrl)
|
|
85
|
-
// @ts-ignore
|
|
86
|
-
const readable = Readable.fromWeb(trackReq.body)
|
|
87
|
-
let buffer = Buffer.alloc(0)
|
|
88
|
-
let i = 0
|
|
89
|
-
|
|
90
|
-
const bufferSize = 2048 // 2mb
|
|
91
|
-
|
|
92
|
-
const passThrough = new PassThrough()
|
|
93
|
-
|
|
94
|
-
readable.on("readable", () => {
|
|
95
|
-
let chunk = null;
|
|
96
|
-
while (true) {
|
|
97
|
-
chunk = readable.read(bufferSize)
|
|
98
|
-
|
|
99
|
-
if (!chunk) {
|
|
100
|
-
if (readable.readableLength) {
|
|
101
|
-
chunk = readable.read(readable.readableLength)
|
|
102
|
-
buffer = Buffer.concat([buffer, chunk])
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
} else {
|
|
106
|
-
buffer = Buffer.concat([buffer, chunk])
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
while (buffer.length >= bufferSize) {
|
|
110
|
-
const bufferSized = buffer.subarray(0, bufferSize)
|
|
111
|
-
|
|
112
|
-
if (i % 3 === 0) {
|
|
113
|
-
const decipher = crypto.createDecipheriv(
|
|
114
|
-
'bf-cbc' as unknown as CipherGCMTypes,
|
|
115
|
-
trackKey as unknown as CipherKey,
|
|
116
|
-
IV as unknown as BinaryLike
|
|
117
|
-
).setAutoPadding(false) as unknown as Decipher
|
|
118
|
-
|
|
119
|
-
// @ts-ignore
|
|
120
|
-
passThrough.write(decipher.update(bufferSized as unknown as ArrayBufferView))
|
|
121
|
-
passThrough.write(decipher.final())
|
|
122
|
-
} else {
|
|
123
|
-
passThrough.write(bufferSized)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
i++
|
|
127
|
-
|
|
128
|
-
buffer = buffer.subarray(bufferSize)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
return passThrough
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export const deezerRegex = {
|
|
137
|
-
track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
|
|
138
|
-
playlistNalbums: /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/,
|
|
139
|
-
share: /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/
|
|
140
|
-
} as const;
|
|
141
|
-
|
|
142
|
-
export function validate(query: string) {
|
|
143
|
-
return deezerRegex.track.test(query) ?? deezerRegex.playlistNalbums.test(query) ?? deezerRegex.share.test(query)
|
|
144
|
-
}
|