Fixed find chapter error, soundcloud 403 and watch playlists

This commit is contained in:
AtariTom 2023-09-24 13:59:51 -04:00
parent 76c237346e
commit 6a8569feb8
5 changed files with 25 additions and 13 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "1.9.6", "version": "1.9.7",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "play-dl", "name": "play-dl",
"version": "1.9.6", "version": "1.9.7",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"play-audio": "^0.5.2" "play-audio": "^0.5.2"

View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "1.9.6", "version": "1.9.7",
"description": "YouTube, SoundCloud, Spotify, Deezer searching and streaming for discord-js bots", "description": "YouTube, SoundCloud, Spotify, Deezer searching and streaming for discord-js bots",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -111,10 +111,14 @@ export async function stream(url: string, quality?: number): Promise<SoundCloudS
* @returns client ID * @returns client ID
*/ */
export async function getFreeClientID(): Promise<string> { export async function getFreeClientID(): Promise<string> {
const data = await request('https://soundcloud.com/'); const data: any = await request('https://soundcloud.com/', {headers: {}}).catch(err => err);
if (data instanceof Error)
throw new Error("Failed to get response from soundcloud.com: " + data.message);
const splitted = data.split('<script crossorigin src="'); const splitted = data.split('<script crossorigin src="');
const urls: string[] = []; const urls: string[] = [];
splitted.forEach((r) => { splitted.forEach((r: string) => {
if (r.startsWith('https')) { if (r.startsWith('https')) {
urls.push(r.split('"')[0]); urls.push(r.split('"')[0]);
} }

View File

@ -24,7 +24,7 @@ export interface SpotifyDataOptions {
file?: boolean; file?: boolean;
} }
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//; const pattern = /^((https:)?\/\/)?open\.spotify\.com\/(?:intl\-.{2}\/)?(track|album|playlist)\//;
/** /**
* Gets Spotify url details. * Gets Spotify url details.
* *
@ -55,7 +55,9 @@ export async function spotify(url: string): Promise<Spotify> {
return err; return err;
}); });
if (response instanceof Error) throw response; if (response instanceof Error) throw response;
return new SpotifyTrack(JSON.parse(response)); const resObj = JSON.parse(response);
if (resObj.error) throw new Error(`Got ${resObj.error.status} from the spotify request: ${resObj.error.message}`);
return new SpotifyTrack(resObj);
} else if (url_.indexOf('album/') !== -1) { } else if (url_.indexOf('album/') !== -1) {
const albumID = url.split('album/')[1].split('&')[0].split('?')[0]; const albumID = url.split('album/')[1].split('&')[0].split('?')[0];
const response = await request(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, { const response = await request(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, {
@ -66,7 +68,9 @@ export async function spotify(url: string): Promise<Spotify> {
return err; return err;
}); });
if (response instanceof Error) throw response; if (response instanceof Error) throw response;
return new SpotifyAlbum(JSON.parse(response), spotifyData, false); const resObj = JSON.parse(response);
if (resObj.error) throw new Error(`Got ${resObj.error.status} from the spotify request: ${resObj.error.message}`);
return new SpotifyAlbum(resObj, spotifyData, false);
} else if (url_.indexOf('playlist/') !== -1) { } else if (url_.indexOf('playlist/') !== -1) {
const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0]; const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0];
const response = await request( const response = await request(
@ -80,7 +84,9 @@ export async function spotify(url: string): Promise<Spotify> {
return err; return err;
}); });
if (response instanceof Error) throw response; if (response instanceof Error) throw response;
return new SpotifyPlaylist(JSON.parse(response), spotifyData, false); const resObj = JSON.parse(response);
if (resObj.error) throw new Error(`Got ${resObj.error.status} from the spotify request: ${resObj.error.message}`);
return new SpotifyPlaylist(resObj, spotifyData, false);
} else throw new Error('URL is out of scope for play-dl.'); } else throw new Error('URL is out of scope for play-dl.');
} }
/** /**

View File

@ -20,7 +20,7 @@ const video_id_pattern = /^[a-zA-Z\d_-]{11,12}$/;
const playlist_id_pattern = /^(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}$/; const playlist_id_pattern = /^(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}$/;
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'; const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
const video_pattern = const video_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|shorts\/|embed\/|v\/)?)([\w\-]+)(\S+)?$/; /^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|shorts\/|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$/;
const playlist_pattern = const playlist_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))\/(?:(playlist|watch))?(.*)?((\?|\&)list=)(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}(&.*)?$/; /^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))\/(?:(playlist|watch))?(.*)?((\?|\&)list=)(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}(&.*)?$/;
/** /**
@ -81,6 +81,8 @@ function extractVideoId(urlOrId: string): string | false {
id = urlOrId.split('youtube.com/embed/')[1].split(/(\?|\/|&)/)[0]; id = urlOrId.split('youtube.com/embed/')[1].split(/(\?|\/|&)/)[0];
} else if (urlOrId.includes('youtube.com/shorts/')) { } else if (urlOrId.includes('youtube.com/shorts/')) {
id = urlOrId.split('youtube.com/shorts/')[1].split(/(\?|\/|&)/)[0]; id = urlOrId.split('youtube.com/shorts/')[1].split(/(\?|\/|&)/)[0];
} else if (urlOrId.includes('youtube.com/live/')) {
id = urlOrId.split('youtube.com/live/')[1].split(/(\?|\/|&)/)[0];
} else { } else {
id = (urlOrId.split('watch?v=')[1] ?? urlOrId.split('&v=')[1]).split(/(\?|\/|&)/)[0]; id = (urlOrId.split('watch?v=')[1] ?? urlOrId.split('&v=')[1]).split(/(\?|\/|&)/)[0];
} }
@ -231,7 +233,7 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
}); });
} }
const rawChapters = const rawChapters =
initial_response.playerOverlays.playerOverlayRenderer.decoratedPlayerBarRenderer?.decoratedPlayerBarRenderer.playerBar?.multiMarkersPlayerBarRenderer.markersMap.find( initial_response.playerOverlays.playerOverlayRenderer.decoratedPlayerBarRenderer?.decoratedPlayerBarRenderer.playerBar?.multiMarkersPlayerBarRenderer.markersMap?.find(
(m: any) => m.key === 'DESCRIPTION_CHAPTERS' (m: any) => m.key === 'DESCRIPTION_CHAPTERS'
)?.value?.chapters; )?.value?.chapters;
const chapters: VideoChapter[] = []; const chapters: VideoChapter[] = [];
@ -542,7 +544,7 @@ export async function playlist_info(url: string, options: PlaylistOptions = {}):
throw new Error(`While parsing playlist url\n${response.alerts[0].alertRenderer.text.runs[0].text}`); throw new Error(`While parsing playlist url\n${response.alerts[0].alertRenderer.text.runs[0].text}`);
else throw new Error('While parsing playlist url\nUnknown Playlist Error'); else throw new Error('While parsing playlist url\nUnknown Playlist Error');
} }
if (url_.indexOf('watch?v=') !== -1) { if (response.currentVideoEndpoint) {
return getWatchPlaylist(response, body, url_); return getWatchPlaylist(response, body, url_);
} else return getNormalPlaylist(response, body); } else return getNormalPlaylist(response, body);
} }
@ -629,7 +631,7 @@ async function acceptViewerDiscretion(
}, },
nextEndpoint: { nextEndpoint: {
urlEndpoint: { urlEndpoint: {
url: `watch?v=${videoId}` url: `/watch?v=${videoId}&has_verified=1`
} }
}, },
setControvercy: true setControvercy: true