discord-player-deezer 0.0.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.
@@ -0,0 +1 @@
1
+ src/
package/.prettierrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "printWidth": 120,
3
+ "trailingComma": "all",
4
+ "singleQuote": true
5
+ }
@@ -0,0 +1,21 @@
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
+ ]
21
+ }
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import { BaseExtractor, ExtractorInfo, ExtractorSearchContext, SearchQueryType, Track } from "discord-player";
3
+ import { Readable } from "stream";
4
+ export default class DeezerExtractor extends BaseExtractor {
5
+ static identifier: string;
6
+ private _stream;
7
+ private deezerRegex;
8
+ activate(): Promise<void>;
9
+ validate(query: string, _type?: SearchQueryType | null | undefined): Promise<boolean>;
10
+ handle(query: string, _context: ExtractorSearchContext): Promise<ExtractorInfo>;
11
+ stream(info: Track): Promise<string | Readable>;
12
+ }
@@ -0,0 +1,103 @@
1
+ // THIS IS A COMPILED FILE! DO NOT MAKE ANY CHANGES TO THIS
2
+ 'use strict';
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+ const discord_player_1 = require('discord-player');
5
+ const deezer_music_metadata_1 = require('@mithron/deezer-music-metadata');
6
+ const youtube_sr_1 = require('youtube-sr');
7
+ const extractor_1 = require('@discord-player/extractor');
8
+ class DeezerExtractor extends discord_player_1.BaseExtractor {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.deezerRegex = {
12
+ track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
13
+ playlistNalbums: /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/,
14
+ share: /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/,
15
+ };
16
+ }
17
+ async activate() {
18
+ this.deezerRegex.track = /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/;
19
+ this.deezerRegex.playlistNalbums = /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/;
20
+ this.deezerRegex.share = /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/;
21
+ const { stream: ytdlStream } = await (0, extractor_1.loadYtdl)();
22
+ this._stream = ytdlStream;
23
+ }
24
+ async validate(query, _type) {
25
+ if (typeof query !== 'string') return false;
26
+ return this.deezerRegex.track.test(query) || this.deezerRegex.playlistNalbums.test(query);
27
+ }
28
+ async handle(query, _context) {
29
+ const data = await (0, deezer_music_metadata_1.getData)(query);
30
+ if (data?.type === 'song') {
31
+ const returnData = {
32
+ playlist: null,
33
+ tracks: [
34
+ new discord_player_1.Track(this.context.player, {
35
+ title: data?.name,
36
+ raw: data,
37
+ description: '',
38
+ author: data.author.map((artist) => artist.name).join(' '),
39
+ url: data.url,
40
+ source: 'arbitrary',
41
+ thumbnail: data.thumbnail[0].url,
42
+ duration: discord_player_1.Util.buildTimeCode(discord_player_1.Util.parseMS(data.duration * 1000)),
43
+ views: 0,
44
+ }),
45
+ ],
46
+ };
47
+ return returnData;
48
+ }
49
+ if (data?.type === 'playlist' || data?.type === 'album') {
50
+ const raw = data.url.split('/');
51
+ const identifier = raw[raw.length - 1];
52
+ const playlist = new discord_player_1.Playlist(this.context.player, {
53
+ title: data.name,
54
+ thumbnail: data.thumbnail[0].url,
55
+ author: {
56
+ name: data.artist.name,
57
+ url: data.artist.url,
58
+ },
59
+ type: data.type,
60
+ rawPlaylist: data,
61
+ tracks: [],
62
+ description: '',
63
+ source: 'arbitrary',
64
+ id: identifier,
65
+ url: data.url,
66
+ });
67
+ const tracks = data.tracks.map((track) => {
68
+ return new discord_player_1.Track(this.context.player, {
69
+ title: track.name,
70
+ raw: track,
71
+ description: '',
72
+ author: track.author.map((artist) => artist.name).join(' '),
73
+ url: track.url,
74
+ source: 'arbitrary',
75
+ thumbnail: track.thumbnail[0].url,
76
+ duration: discord_player_1.Util.buildTimeCode(discord_player_1.Util.parseMS(track.duration * 1000)),
77
+ views: 0,
78
+ });
79
+ });
80
+ playlist.tracks = tracks;
81
+ return {
82
+ playlist,
83
+ tracks,
84
+ };
85
+ }
86
+ return { playlist: null, tracks: [] };
87
+ }
88
+ async stream(info) {
89
+ try {
90
+ const searchQuery = `${info.author} - ${info.title} audio`;
91
+ const serachResults = await youtube_sr_1.YouTube.search(searchQuery, {
92
+ limit: 1,
93
+ type: 'video',
94
+ });
95
+ const stream = await this._stream(serachResults[0].url);
96
+ return stream;
97
+ } catch (error) {
98
+ throw error;
99
+ }
100
+ }
101
+ }
102
+ exports.default = DeezerExtractor;
103
+ DeezerExtractor.identifier = 'com.discord-player.deezerextractor';
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "discord-player-deezer",
3
+ "version": "0.0.2",
4
+ "description": "A custom extractor made for discord-player that enables you to extract from Deezer.",
5
+ "main": "dist/DeezerExtractor.js",
6
+ "author": "retro_ig",
7
+ "license": "MIT",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@discord-player/extractor": "^4.1.1",
11
+ "@mithron/deezer-music-metadata": "^1.0.3",
12
+ "discord-player": "^6.0.0",
13
+ "tslint-config-prettier": "^1.18.0",
14
+ "youtube-sr": "^4.3.4",
15
+ "ytdl-core": "^4.11.2"
16
+ },
17
+ "devDependencies": {
18
+ "prettier": "^2.8.4",
19
+ "tslint": "^6.1.3",
20
+ "typescript": "^4.9.5"
21
+ },
22
+ "scripts": {
23
+ "build": "npm run lint && tsc && npm run format",
24
+ "format": "prettier --write \"dist/*.js\"",
25
+ "lint": "tslint -p tsconfig.json",
26
+ "publish": "yarn build && npm publish --access public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/retrouser955/discord-player-deezer/"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/retrouser955/discord-player-deezer/issues"
34
+ }
35
+ }
package/readme.md ADDED
@@ -0,0 +1,26 @@
1
+ # Discord Player Deezer Extractor
2
+
3
+ Discord Player Deezer Extractor. An unofficial extractor made for discord-player for **Deezer** support. Deezer extractor works by either making a request to the Deezer API or extracting metadata from the Deezer site. Because we cannot get streams from deezer itself, the extractor uses the extracted data to stream from youtube!
4
+
5
+ ## Installing the extractor
6
+
7
+ ```bash
8
+ npm install https://github.com/retrouser955/discord-player-deezer.git
9
+ # or
10
+ yarn add https://github.com/retrouser955/discord-player-deezer.git
11
+ ```
12
+
13
+ *note: I will put the extractor on npm once I am done with the package!*
14
+
15
+ ## Loading the extractor
16
+
17
+ ```js
18
+ const DeezerExtractor = require("discord-player-deezer")
19
+ const player = getMainPlayerSomehow()
20
+
21
+ player.extractors.register(DeezerExtractor)
22
+ ```
23
+
24
+ *note: be sure to register it before loading the default extractors to make sure any conflicts with discord-player's default attachment extractor is resolved!*
25
+
26
+ That's it! See the magic happen as you bot is now able to play from Deezer URLs
@@ -0,0 +1,121 @@
1
+ import { BaseExtractor, ExtractorInfo, ExtractorSearchContext, Playlist, SearchQueryType, Track, Util } from "discord-player"
2
+ import { getData } from "@mithron/deezer-music-metadata"
3
+ import { YouTube } from "youtube-sr";
4
+ import { Readable } from "stream";
5
+ import { StreamFN, loadYtdl } from "@discord-player/extractor"
6
+
7
+ interface DeezerRegex {
8
+ track: RegExp;
9
+ playlistNalbums: RegExp;
10
+ share: RegExp
11
+ }
12
+
13
+ export default class DeezerExtractor extends BaseExtractor {
14
+ public static identifier: string = "com.discord-player.deezerextractor" as const
15
+ private _stream!: StreamFN
16
+
17
+ private deezerRegex: DeezerRegex = {
18
+ track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
19
+ playlistNalbums: /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/,
20
+ share: /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/
21
+ };
22
+
23
+ public async activate(): Promise<void> {
24
+ const { stream: ytdlStream } = await loadYtdl()
25
+
26
+ this._stream = ytdlStream
27
+ }
28
+
29
+ public async validate(query: string, _type?: SearchQueryType | null | undefined): Promise<boolean> {
30
+ if (typeof query !== "string") return false
31
+
32
+ return this.deezerRegex.track.test(query) || this.deezerRegex.playlistNalbums.test(query)
33
+ }
34
+
35
+ public async handle(query: string, _context: ExtractorSearchContext): Promise<ExtractorInfo> {
36
+ const data = await getData(query)
37
+
38
+ if (data?.type === "song") {
39
+ const returnData = {
40
+ playlist: null,
41
+ tracks: [
42
+ new Track(this.context.player, {
43
+ title: data?.name as string,
44
+ raw: data,
45
+ description: "",
46
+ author: data.author.map((artist) => artist.name).join(" "),
47
+ url: data.url,
48
+ source: "arbitrary",
49
+ thumbnail: data.thumbnail[0].url,
50
+ duration: Util.buildTimeCode(Util.parseMS(data.duration * 1000)),
51
+ views: 0
52
+ })
53
+ ]
54
+ }
55
+
56
+ return returnData
57
+ }
58
+
59
+ if (data?.type === "playlist" || data?.type === "album") {
60
+ const raw: string[] = data.url.split("/")
61
+ const identifier: string = raw[raw.length - 1]
62
+
63
+ const playlist = 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
+ rawPlaylist: data,
72
+ tracks: [],
73
+ description: "",
74
+ source: "arbitrary",
75
+ id: identifier,
76
+ url: data.url
77
+ })
78
+
79
+ const tracks = data.tracks.map(track => {
80
+ return new Track(this.context.player, {
81
+ title: track.name as string,
82
+ raw: track,
83
+ description: "",
84
+ author: track.author.map((artist) => artist.name).join(" "),
85
+ url: track.url,
86
+ source: "arbitrary",
87
+ thumbnail: track.thumbnail[0].url,
88
+ duration: Util.buildTimeCode(Util.parseMS(track.duration * 1000)),
89
+ views: 0
90
+ })
91
+ })
92
+
93
+ playlist.tracks = tracks
94
+
95
+ return {
96
+ playlist,
97
+ tracks
98
+ }
99
+ }
100
+
101
+ return { playlist: null, tracks: [] }
102
+ }
103
+
104
+ public async stream(info: Track): Promise<string | Readable> {
105
+
106
+ try {
107
+ const searchQuery: string = `${info.author} - ${info.title} audio`
108
+
109
+ const serachResults = await YouTube.search(searchQuery, {
110
+ limit: 1,
111
+ type: "video"
112
+ })
113
+
114
+ const stream = await this._stream(serachResults[0].url)
115
+
116
+ return stream
117
+ } catch (error) {
118
+ throw(error)
119
+ }
120
+ }
121
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "./discord-player-ts.base.json",
3
+ "include": ["./**/*.ts"],
4
+ "exclude": ["dist/*.ts"]
5
+ }
package/tslint.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["tslint:recommended", "tslint-config-prettier"]
3
+ }