From 838fc8e4149f0602926928fd1cf6b2e50cb5cdb6 Mon Sep 17 00:00:00 2001 From: Willian Silva Date: Sat, 16 Sep 2023 14:07:16 -0300 Subject: [PATCH] feat: sanitize i18n spotify shared url --- src/helpers/helpers.ts | 8 ++++++++ src/sources/play-dl-source/play-dl-source.ts | 6 +----- src/sources/play-dl-source/strategies/spotify-strategy.ts | 7 ++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index ad3b9c9..a5b4b2d 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -77,3 +77,11 @@ export function createEmbedMessage(embed: APIEmbed): APIEmbed { return replyEmbed; } + +export function sanitizeUrl(url: string) { + const regex = /intl-[a-zA-Z]{2}\//; + + const modifiedUrl = url.trim().replace(regex, ''); + + return modifiedUrl; +} diff --git a/src/sources/play-dl-source/play-dl-source.ts b/src/sources/play-dl-source/play-dl-source.ts index 1c5117d..7dcc514 100644 --- a/src/sources/play-dl-source/play-dl-source.ts +++ b/src/sources/play-dl-source/play-dl-source.ts @@ -83,11 +83,7 @@ export class PlayDlSourceStream implements SourceStream { const Strategy = playDlStrategies[this.streamType]; - const regex = /intl-[a-zA-Z]{2}\//; - - const modifiedUrl = url.replace(regex, ""); - - return new Strategy(this).getStreamInfo(modifiedUrl); + return new Strategy(this).getStreamInfo(url); } catch (e) { throw new BotError(e.stack || e.message, ERRORS.RESULT_NOT_FOUND); } diff --git a/src/sources/play-dl-source/strategies/spotify-strategy.ts b/src/sources/play-dl-source/strategies/spotify-strategy.ts index 05bdb84..5d5918e 100644 --- a/src/sources/play-dl-source/strategies/spotify-strategy.ts +++ b/src/sources/play-dl-source/strategies/spotify-strategy.ts @@ -1,5 +1,6 @@ import play, { SpotifyAlbum, SpotifyPlaylist, SpotifyTrack } from 'play-dl'; +import { sanitizeUrl } from '@/helpers/helpers'; import { StreamInfo } from '@/sources/source-stream'; import { PlayDlSourceStream } from '../play-dl-source'; @@ -9,7 +10,7 @@ export class SpotifyTrackStrategy implements IStrategy { constructor(private playDlSourceStream: PlayDlSourceStream) {} async getStreamInfo(url: string): Promise { - const spotifyInfo = (await play.spotify(url.trim())) as SpotifyTrack; + const spotifyInfo = (await play.spotify(sanitizeUrl(url))) as SpotifyTrack; const searched = ( await this.playDlSourceStream.search( @@ -38,7 +39,7 @@ export class SpotifyTrackStrategy implements IStrategy { export class SpotifyPlaylistStrategy implements IStrategy { async getStreamInfo(url: string): Promise { - const playlist = (await play.spotify(url.trim())) as SpotifyPlaylist; + const playlist = (await play.spotify(sanitizeUrl(url))) as SpotifyPlaylist; const tracks = await playlist.all_tracks(); @@ -54,7 +55,7 @@ export class SpotifyPlaylistStrategy implements IStrategy { export class SpotifyAlbumStrategy implements IStrategy { async getStreamInfo(url: string): Promise { - const album = (await play.spotify(url.trim())) as SpotifyAlbum; + const album = (await play.spotify(sanitizeUrl(url))) as SpotifyAlbum; const tracks = await album.all_tracks();