discord-player-deezer 0.0.8 → 1.0.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.
@@ -1,12 +1,25 @@
1
1
  /// <reference types="node" />
2
2
  import { BaseExtractor, ExtractorInfo, ExtractorSearchContext, SearchQueryType, Track } from "discord-player";
3
3
  import { Readable } from "stream";
4
- export default class DeezerExtractor extends BaseExtractor {
4
+ import { default as SoundCloud } from "soundcloud.ts";
5
+ interface DeezerOptions {
6
+ bridgeFrom?: "YouTube" | "SoundCloud";
7
+ soundcloud?: {
8
+ clientId?: string;
9
+ oauthToken?: string;
10
+ proxy?: string;
11
+ };
12
+ onBeforeCreateStream?: (track: Track) => Promise<string | Readable>;
13
+ }
14
+ export default class DeezerExtractor extends BaseExtractor<DeezerOptions> {
5
15
  static identifier: string;
6
16
  private _stream;
17
+ client: SoundCloud;
7
18
  private deezerRegex;
8
19
  activate(): Promise<void>;
9
20
  validate(query: string, _type?: SearchQueryType | null | undefined): Promise<boolean>;
10
21
  handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo>;
22
+ brdgeProvider(track: Track): Promise<string>;
11
23
  stream(info: Track): Promise<string | Readable>;
12
24
  }
25
+ export {};
@@ -1,12 +1,24 @@
1
1
  'use strict';
2
+ var __importDefault =
3
+ (this && this.__importDefault) ||
4
+ function (mod) {
5
+ return mod && mod.__esModule ? mod : { default: mod };
6
+ };
2
7
  Object.defineProperty(exports, '__esModule', { value: true });
3
8
  const discord_player_1 = require('discord-player');
4
9
  const deezer_music_metadata_1 = require('@mithron/deezer-music-metadata');
5
10
  const youtube_sr_1 = require('youtube-sr');
6
- const extractor_1 = require('@discord-player/extractor');
11
+ const yt_stream_1 = require('yt-stream');
12
+ const soundcloud_ts_1 = __importDefault(require('soundcloud.ts'));
7
13
  class DeezerExtractor extends discord_player_1.BaseExtractor {
8
14
  constructor() {
9
15
  super(...arguments);
16
+ this._stream = yt_stream_1.stream;
17
+ this.client = new soundcloud_ts_1.default({
18
+ clientId: this.options.soundcloud?.clientId,
19
+ oauthToken: this.options.soundcloud?.oauthToken,
20
+ proxy: this.options.soundcloud?.proxy,
21
+ });
10
22
  this.deezerRegex = {
11
23
  track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
12
24
  playlistNalbums: /(^https:)\/\/(www\.)?deezer.com\/[a-zA-Z]+\/(playlist|album)\/[0-9]+(\?)?(.*)/,
@@ -14,8 +26,8 @@ class DeezerExtractor extends discord_player_1.BaseExtractor {
14
26
  };
15
27
  }
16
28
  async activate() {
17
- const { stream: ytdlStream } = await (0, extractor_1.loadYtdl)();
18
- this._stream = ytdlStream;
29
+ /* tslint:disable-next-line */
30
+ if (!this.options.bridgeFrom) this.options.bridgeFrom === 'YouTube';
19
31
  }
20
32
  async validate(query, _type) {
21
33
  if (typeof query !== 'string') return false;
@@ -26,6 +38,10 @@ class DeezerExtractor extends discord_player_1.BaseExtractor {
26
38
  );
27
39
  }
28
40
  async handle(query, context) {
41
+ if (this.deezerRegex.share.test(query)) {
42
+ const fetchRes = await fetch(query);
43
+ query = fetchRes.url.split('?')[0];
44
+ }
29
45
  const data = await (0, deezer_music_metadata_1.getData)(query);
30
46
  if (data?.type === 'song') {
31
47
  const returnData = {
@@ -87,18 +103,36 @@ class DeezerExtractor extends discord_player_1.BaseExtractor {
87
103
  }
88
104
  return { playlist: null, tracks: [] };
89
105
  }
106
+ async brdgeProvider(track) {
107
+ const query = this.createBridgeQuery(track);
108
+ if (this.options.bridgeFrom === 'YouTube') {
109
+ try {
110
+ const serachResults = await youtube_sr_1.YouTube.search(query, {
111
+ limit: 1,
112
+ type: 'video',
113
+ });
114
+ const ytStream = await this._stream(serachResults[0].url, {
115
+ quality: 'high',
116
+ type: 'audio',
117
+ highWaterMark: 1048576 * 32,
118
+ });
119
+ return ytStream.url;
120
+ } catch (error) {
121
+ throw new Error(`Could not find a source to bridge from. The error is as follows.\n\n${error}`);
122
+ }
123
+ }
124
+ const res = await this.client.tracks.searchV2({
125
+ q: query,
126
+ });
127
+ if (res.collection.length === 0) throw new Error('Could not find a suitable source to stream from.');
128
+ const str = this.client.util.streamLink(res.collection[0].permalink_url);
129
+ return str;
130
+ }
90
131
  async stream(info) {
91
- try {
92
- const searchQuery = `${info.author} - ${info.title} audio`;
93
- const serachResults = await youtube_sr_1.YouTube.search(searchQuery, {
94
- limit: 1,
95
- type: 'video',
96
- });
97
- const stream = await this._stream(serachResults[0].url);
98
- return stream;
99
- } catch (error) {
100
- throw error;
132
+ if (this.options.onBeforeCreateStream && typeof this.options.onBeforeCreateStream === 'function') {
133
+ return await this.options.onBeforeCreateStream(info);
101
134
  }
135
+ return this.brdgeProvider(info);
102
136
  }
103
137
  }
104
138
  exports.default = DeezerExtractor;
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "discord-player-deezer",
3
- "version": "0.0.8",
3
+ "version": "1.0.0",
4
4
  "description": "A custom extractor made for discord-player that enables you to extract from Deezer.",
5
5
  "main": "dist/DeezerExtractor.js",
6
6
  "author": "retro_ig",
7
7
  "license": "MIT",
8
8
  "private": false,
9
9
  "dependencies": {
10
+ "@discord-player/extractor": "^4.4.4",
10
11
  "@mithron/deezer-music-metadata": "^1.0.3",
11
- "discord-player": "^6.0.0",
12
+ "discord-player": "*",
13
+ "soundcloud.ts": "^0.5.2",
12
14
  "tslint-config-prettier": "^1.18.0",
13
15
  "youtube-sr": "^4.3.4",
14
16
  "yt-stream": "^1.4.4"
@@ -3,6 +3,17 @@ import { getData } from "@mithron/deezer-music-metadata"
3
3
  import { YouTube } from "youtube-sr";
4
4
  import { Readable } from "stream";
5
5
  import { stream } from "yt-stream";
6
+ import { default as SoundCloud } from "soundcloud.ts"
7
+
8
+ interface DeezerOptions {
9
+ bridgeFrom?: "YouTube" | "SoundCloud",
10
+ soundcloud?: {
11
+ clientId?: string;
12
+ oauthToken?: string;
13
+ proxy?: string;
14
+ };
15
+ onBeforeCreateStream?: (track: Track) => Promise<string | Readable>
16
+ }
6
17
 
7
18
  interface DeezerRegex {
8
19
  track: RegExp;
@@ -10,9 +21,14 @@ interface DeezerRegex {
10
21
  share: RegExp
11
22
  }
12
23
 
13
- export default class DeezerExtractor extends BaseExtractor {
24
+ export default class DeezerExtractor extends BaseExtractor<DeezerOptions> {
14
25
  public static identifier: string = "com.discord-player.deezerextractor" as const
15
26
  private _stream = stream
27
+ client = new SoundCloud({
28
+ clientId: this.options.soundcloud?.clientId,
29
+ oauthToken: this.options.soundcloud?.oauthToken,
30
+ proxy: this.options.soundcloud?.proxy
31
+ })
16
32
 
17
33
  private deezerRegex: DeezerRegex = {
18
34
  track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
@@ -20,6 +36,11 @@ export default class DeezerExtractor extends BaseExtractor {
20
36
  share: /(^https:)\/\/deezer\.page\.link\/[A-Za-z0-9]+/
21
37
  };
22
38
 
39
+ public async activate(): Promise<void> {
40
+ /* tslint:disable-next-line */
41
+ if(!this.options.bridgeFrom) this.options.bridgeFrom === "YouTube"
42
+ }
43
+
23
44
  public async validate(query: string, _type?: SearchQueryType | null | undefined): Promise<boolean> {
24
45
  if (typeof query !== "string") return false
25
46
 
@@ -27,6 +48,12 @@ export default class DeezerExtractor extends BaseExtractor {
27
48
  }
28
49
 
29
50
  public async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
51
+ if(this.deezerRegex.share.test(query)) {
52
+ const fetchRes = await fetch(query)
53
+
54
+ query = fetchRes.url.split("?")[0]
55
+ }
56
+
30
57
  const data = await getData(query)
31
58
 
32
59
  if (data?.type === "song") {
@@ -97,25 +124,44 @@ export default class DeezerExtractor extends BaseExtractor {
97
124
  return { playlist: null, tracks: [] }
98
125
  }
99
126
 
100
- public async stream(info: Track): Promise<string | Readable> {
127
+ public async brdgeProvider(track: Track) {
128
+ const query = this.createBridgeQuery(track)
101
129
 
102
- try {
103
- const searchQuery: string = `${info.author} - ${info.title} audio`
130
+ if(this.options.bridgeFrom === "YouTube") {
131
+ try {
132
+ const serachResults = await YouTube.search(query, {
133
+ limit: 1,
134
+ type: "video"
135
+ })
136
+
137
+ const ytStream = await this._stream(serachResults[0].url, {
138
+ quality: 'high',
139
+ type: 'audio',
140
+ highWaterMark: 1048576 * 32
141
+ })
142
+
143
+ return ytStream.url
144
+ } catch (error) {
145
+ throw new Error(`Could not find a source to bridge from. The error is as follows.\n\n${error}`)
146
+ }
147
+ }
104
148
 
105
- const serachResults = await YouTube.search(searchQuery, {
106
- limit: 1,
107
- type: "video"
108
- })
149
+ const res = await this.client.tracks.searchV2({
150
+ q: query
151
+ })
109
152
 
110
- const stream = await this._stream(serachResults[0].url, {
111
- quality: 'high',
112
- type: 'audio',
113
- highWaterMark: 1048576 * 32
114
- })
153
+ if(res.collection.length === 0) throw new Error("Could not find a suitable source to stream from.")
154
+
155
+ const str = this.client.util.streamLink(res.collection[0].permalink_url)
115
156
 
116
- return stream.url
117
- } catch (error) {
118
- throw(error)
157
+ return str
158
+ }
159
+
160
+ public async stream(info: Track): Promise<string | Readable> {
161
+ if(this.options.onBeforeCreateStream && typeof this.options.onBeforeCreateStream === "function") {
162
+ return await this.options.onBeforeCreateStream(info)
119
163
  }
164
+
165
+ return this.brdgeProvider(info)
120
166
  }
121
167
  }
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "extends": "./discord-player-ts.base.json",
3
- "include": ["./**/*.ts"],
3
+ "include": ["./src/*.ts"],
4
4
  "exclude": ["dist/*.ts"]
5
5
  }