discord-player-deezer
Advanced tools
Comparing version 2.1.0 to 2.2.0-dev.0
@@ -17,2 +17,3 @@ import { Track, ExtractorStreamable, BaseExtractor, ExtractorSearchContext, Playlist as Playlist$1, ExtractorInfo, Player } from 'discord-player'; | ||
createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>; | ||
searchLimit?: number; | ||
}; | ||
@@ -42,2 +43,4 @@ type DeezerUserInfo = { | ||
declare function getCrypto(): Promise<typeof crypto>; | ||
declare function isUrl(query: string): Promise<boolean>; | ||
declare function search(query: string, limit?: number): Promise<DeezerSearchTrackResponse>; | ||
type ArrayOrObject<T> = T | T[]; | ||
@@ -72,2 +75,2 @@ interface DeezerSearchTrackResponse { | ||
export { type ArrayOrObject, DeezerExtractor, type DeezerExtractorOptions, type DeezerSearchTrackResponse, type DeezerUserInfo, Warnings, buildTrackFromSearch, deezerRegex, extractTrackId, getCrypto, searchOneTrack, streamTrack, validate }; | ||
export { type ArrayOrObject, DeezerExtractor, type DeezerExtractorOptions, type DeezerSearchTrackResponse, type DeezerUserInfo, Warnings, buildTrackFromSearch, deezerRegex, extractTrackId, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate }; |
@@ -39,2 +39,4 @@ "use strict"; | ||
getCrypto: () => getCrypto, | ||
isUrl: () => isUrl, | ||
search: () => search, | ||
searchOneTrack: () => searchOneTrack, | ||
@@ -63,2 +65,10 @@ streamTrack: () => streamTrack, | ||
} | ||
async function isUrl(query) { | ||
try { | ||
new URL(query); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
var DeezerAPIRoutes = { | ||
@@ -70,2 +80,10 @@ searchTrack(query, limit) { | ||
}; | ||
async function search(query, limit = 10) { | ||
const route = DeezerAPIRoutes.searchTrack(query, limit); | ||
const response = await fetch(route); | ||
if (!response.status.toString().startsWith("2")) throw new Error("Server responded with a non 2xx status."); | ||
const jsonTrack = await response.json(); | ||
if (!jsonTrack.data || !Array.isArray(jsonTrack.data) || jsonTrack.data.length === 0) throw new Error("Unable get tracks from Deezer for the query '" + query + "'"); | ||
return jsonTrack; | ||
} | ||
function buildTrackFromSearch(track, player, requestedBy) { | ||
@@ -237,2 +255,10 @@ const tracks = track.data.map((v) => new import_discord_player.Track(player, { | ||
async handle(query, context) { | ||
if (!isUrl(query)) { | ||
try { | ||
const tracks = buildTrackFromSearch(await search(query, this.options.searchLimit), this.context.player, context.requestedBy); | ||
return this.createResponse(null, tracks); | ||
} catch { | ||
return this.createResponse(); | ||
} | ||
} | ||
if (deezerRegex.share.test(query)) { | ||
@@ -281,2 +307,4 @@ const redirect = await fetch(query); | ||
getCrypto, | ||
isUrl, | ||
search, | ||
searchOneTrack, | ||
@@ -283,0 +311,0 @@ streamTrack, |
{ | ||
"name": "discord-player-deezer", | ||
"version": "2.1.0", | ||
"version": "2.2.0-dev.0", | ||
"description": "A custom extractor made for discord-player that enables you to extract from Deezer.", | ||
@@ -8,3 +8,2 @@ "main": "dist/index.js", | ||
"license": "MIT", | ||
"private": false, | ||
"dependencies": { | ||
@@ -11,0 +10,0 @@ "@mithron/deezer-music-metadata": "^1.0.3", |
@@ -0,0 +0,0 @@ # Discord Player Deezer |
import { BaseExtractor, ExtractorSearchContext, ExtractorStreamable, Track, Playlist, Util as DPUtil, ExtractorInfo } from "discord-player" | ||
import { Playlist as DeezerPlaylist, Track as DeezerTrack, getData } from "@mithron/deezer-music-metadata"; | ||
import { buildTrackFromSearch, deezerRegex, getCrypto, searchOneTrack, streamTrack, validate } from "./utils/util"; | ||
import { buildTrackFromSearch, deezerRegex, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate } from "./utils/util"; | ||
@@ -16,3 +16,4 @@ /** | ||
decryptionKey?: string; // needed for decrypting deezer songs | ||
createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable> | ||
createStream?: (track: Track, ext: DeezerExtractor) => Promise<ExtractorStreamable>; | ||
searchLimit?: number; | ||
} | ||
@@ -90,2 +91,14 @@ | ||
async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> { | ||
if(!isUrl(query)) { | ||
// Player is using deezer protocol | ||
try { | ||
const tracks = buildTrackFromSearch(await search(query, this.options.searchLimit), this.context.player, context.requestedBy); | ||
return this.createResponse(null, tracks); | ||
} catch { | ||
// deezer didnt return a good api response | ||
return this.createResponse() | ||
} | ||
} | ||
if (deezerRegex.share.test(query)) { | ||
@@ -133,2 +146,2 @@ const redirect = await fetch(query); | ||
} | ||
} | ||
} |
export * from "./DeezerExtractor" | ||
export * from "./utils/util" |
import { type Player, Track, Util } from "discord-player"; | ||
import type { DeezerExtractor } from "../DeezerExtractor"; | ||
import { Readable, PassThrough } from 'stream' | ||
import type { BinaryLike, CipherGCMTypes, CipherKey, Decipher } from "crypto"; | ||
import Blowfish from "blowfish-node"; | ||
@@ -20,2 +19,12 @@ | ||
export async function isUrl(query: string) { | ||
try { | ||
/* tslint:disable-next-line */ | ||
new URL(query) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
const DeezerAPIRoutes = { | ||
@@ -29,2 +38,16 @@ searchTrack(query: string, limit?: number) { | ||
export async function search(query: string, limit: number = 10) { | ||
const route = DeezerAPIRoutes.searchTrack(query, limit); | ||
const response = await fetch(route) | ||
if(!response.status.toString().startsWith("2")) throw new Error("Server responded with a non 2xx status.") | ||
const jsonTrack = await response.json() | ||
if(!jsonTrack.data || !Array.isArray(jsonTrack.data) || jsonTrack.data.length === 0) throw new Error("Unable get tracks from Deezer for the query '" + query + "'") | ||
return jsonTrack as DeezerSearchTrackResponse | ||
} | ||
export type ArrayOrObject<T> = T | T[] | ||
@@ -196,2 +219,2 @@ | ||
return deezerRegex.track.test(query) ?? deezerRegex.playlistNalbums.test(query) ?? deezerRegex.share.test(query) | ||
} | ||
} |
@@ -0,0 +0,0 @@ { |
{ | ||
"extends": ["tslint:recommended", "tslint-config-prettier"] | ||
} |
@@ -0,0 +0,0 @@ import { defineConfig } from "tsup" |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
278316
13
700
2
7