discord-player-deezer 0.0.8 → 0.0.9

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;
@@ -87,18 +99,36 @@ class DeezerExtractor extends discord_player_1.BaseExtractor {
87
99
  }
88
100
  return { playlist: null, tracks: [] };
89
101
  }
102
+ async brdgeProvider(track) {
103
+ const query = this.createBridgeQuery(track);
104
+ if (this.options.bridgeFrom === 'YouTube') {
105
+ try {
106
+ const serachResults = await youtube_sr_1.YouTube.search(query, {
107
+ limit: 1,
108
+ type: 'video',
109
+ });
110
+ const ytStream = await this._stream(serachResults[0].url, {
111
+ quality: 'high',
112
+ type: 'audio',
113
+ highWaterMark: 1048576 * 32,
114
+ });
115
+ return ytStream.url;
116
+ } catch (error) {
117
+ throw new Error(`Could not find a source to bridge from. The error is as follows.\n\n${error}`);
118
+ }
119
+ }
120
+ const res = await this.client.tracks.searchV2({
121
+ q: query,
122
+ });
123
+ if (res.collection.length === 0) throw new Error('Could not find a suitable source to stream from.');
124
+ const str = this.client.util.streamLink(res.collection[0].permalink_url);
125
+ return str;
126
+ }
90
127
  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;
128
+ if (this.options.onBeforeCreateStream && typeof this.options.onBeforeCreateStream === 'function') {
129
+ return await this.options.onBeforeCreateStream(info);
101
130
  }
131
+ return this.brdgeProvider(info);
102
132
  }
103
133
  }
104
134
  exports.default = DeezerExtractor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discord-player-deezer",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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",
@@ -8,7 +8,8 @@
8
8
  "private": false,
9
9
  "dependencies": {
10
10
  "@mithron/deezer-music-metadata": "^1.0.3",
11
- "discord-player": "^6.0.0",
11
+ "discord-player": "*",
12
+ "soundcloud.ts": "^0.5.2",
12
13
  "tslint-config-prettier": "^1.18.0",
13
14
  "youtube-sr": "^4.3.4",
14
15
  "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
 
@@ -97,25 +118,44 @@ export default class DeezerExtractor extends BaseExtractor {
97
118
  return { playlist: null, tracks: [] }
98
119
  }
99
120
 
100
- public async stream(info: Track): Promise<string | Readable> {
121
+ public async brdgeProvider(track: Track) {
122
+ const query = this.createBridgeQuery(track)
123
+
124
+ if(this.options.bridgeFrom === "YouTube") {
125
+ try {
126
+ const serachResults = await YouTube.search(query, {
127
+ limit: 1,
128
+ type: "video"
129
+ })
130
+
131
+ const ytStream = await this._stream(serachResults[0].url, {
132
+ quality: 'high',
133
+ type: 'audio',
134
+ highWaterMark: 1048576 * 32
135
+ })
136
+
137
+ return ytStream.url
138
+ } catch (error) {
139
+ throw new Error(`Could not find a source to bridge from. The error is as follows.\n\n${error}`)
140
+ }
141
+ }
101
142
 
102
- try {
103
- const searchQuery: string = `${info.author} - ${info.title} audio`
143
+ const res = await this.client.tracks.searchV2({
144
+ q: query
145
+ })
104
146
 
105
- const serachResults = await YouTube.search(searchQuery, {
106
- limit: 1,
107
- type: "video"
108
- })
147
+ if(res.collection.length === 0) throw new Error("Could not find a suitable source to stream from.")
109
148
 
110
- const stream = await this._stream(serachResults[0].url, {
111
- quality: 'high',
112
- type: 'audio',
113
- highWaterMark: 1048576 * 32
114
- })
149
+ const str = this.client.util.streamLink(res.collection[0].permalink_url)
115
150
 
116
- return stream.url
117
- } catch (error) {
118
- throw(error)
151
+ return str
152
+ }
153
+
154
+ public async stream(info: Track): Promise<string | Readable> {
155
+ if(this.options.onBeforeCreateStream && typeof this.options.onBeforeCreateStream === "function") {
156
+ return await this.options.onBeforeCreateStream(info)
119
157
  }
158
+
159
+ return this.brdgeProvider(info)
120
160
  }
121
161
  }
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
  }