Skip to content

Commit

Permalink
Merge pull request #37 from Marli-Marlete/feat/spotify-sanitize-url
Browse files Browse the repository at this point in the history
spotify sanitize url
  • Loading branch information
ikariwill authored Sep 16, 2023
2 parents f0f2424 + fc7837d commit fca7a9f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 5 additions & 6 deletions src/sources/play-dl-source/play-dl-source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from 'node:stream';
import play, { validate as validateStreamUrl, YouTubeVideo } from 'play-dl';

import { sanitizeUrl } from '@/helpers/helpers';
import { BotError, ERRORS } from '@/shared/errors';

import {
Expand Down Expand Up @@ -83,18 +84,16 @@ 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);
}
}

async validate(input: string): Promise<boolean> {
const validatedStreamUrl = (await validateStreamUrl(input)) as string;
const validatedStreamUrl = (await validateStreamUrl(
sanitizeUrl(input)
)) as string;

this.streamType = validatedStreamUrl;

Expand Down
7 changes: 4 additions & 3 deletions src/sources/play-dl-source/strategies/spotify-strategy.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,7 +10,7 @@ export class SpotifyTrackStrategy implements IStrategy {
constructor(private playDlSourceStream: PlayDlSourceStream) {}

async getStreamInfo(url: string): Promise<StreamInfo[]> {
const spotifyInfo = (await play.spotify(url.trim())) as SpotifyTrack;
const spotifyInfo = (await play.spotify(sanitizeUrl(url))) as SpotifyTrack;

const searched = (
await this.playDlSourceStream.search(
Expand Down Expand Up @@ -38,7 +39,7 @@ export class SpotifyTrackStrategy implements IStrategy {

export class SpotifyPlaylistStrategy implements IStrategy {
async getStreamInfo(url: string): Promise<StreamInfo[]> {
const playlist = (await play.spotify(url.trim())) as SpotifyPlaylist;
const playlist = (await play.spotify(sanitizeUrl(url))) as SpotifyPlaylist;

const tracks = await playlist.all_tracks();

Expand All @@ -54,7 +55,7 @@ export class SpotifyPlaylistStrategy implements IStrategy {

export class SpotifyAlbumStrategy implements IStrategy {
async getStreamInfo(url: string): Promise<StreamInfo[]> {
const album = (await play.spotify(url.trim())) as SpotifyAlbum;
const album = (await play.spotify(sanitizeUrl(url))) as SpotifyAlbum;

const tracks = await album.all_tracks();

Expand Down

0 comments on commit fca7a9f

Please sign in to comment.