YT shorts support
This commit is contained in:
parent
2406bd0630
commit
5fd1045091
@ -156,9 +156,7 @@ export async function sp_search(
|
||||
if (query.length === 0) throw new Error('Pass some query to search.');
|
||||
if (limit > 50 || limit < 0) throw new Error(`You crossed limit range of Spotify [ 0 - 50 ]`);
|
||||
const response = await request(
|
||||
`https://api.spotify.com/v1/search?type=${type}&q=${query}&limit=${limit}&market=${
|
||||
spotifyData.market
|
||||
}`,
|
||||
`https://api.spotify.com/v1/search?type=${type}&q=${query}&limit=${limit}&market=${spotifyData.market}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${spotifyData.token_type} ${spotifyData.access_token}`
|
||||
|
||||
@ -61,6 +61,8 @@ export function extractID(url: string): string {
|
||||
if (url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split(/(\?|\/|&)/)[0];
|
||||
else if (url.includes('youtube.com/embed/'))
|
||||
video_id = url.split('youtube.com/embed/')[1].split(/(\?|\/|&)/)[0];
|
||||
else if (url.includes('youtube.com/shorts/'))
|
||||
video_id = url.split('youtube.com/shorts/')[1].split(/(\?|\/|&)/)[0];
|
||||
else video_id = url.split('watch?v=')[1].split(/(\?|\/|&)/)[0];
|
||||
return video_id;
|
||||
} else {
|
||||
@ -76,15 +78,25 @@ export function extractID(url: string): string {
|
||||
*/
|
||||
export async function video_basic_info(url: string, options: InfoOptions = {}) {
|
||||
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
|
||||
let video_id: string = extractID(url);
|
||||
const video_id: string = extractID(url);
|
||||
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;
|
||||
const body = await request(new_url, {
|
||||
proxies: options.proxy ?? [],
|
||||
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' },
|
||||
cookies: true
|
||||
});
|
||||
const player_response = JSON.parse(body.split('var ytInitialPlayerResponse = ')[1].split(';</script>')[0].split(/; (var|const|let)/)[0]);
|
||||
const initial_response = JSON.parse(body.split('var ytInitialData = ')[1].split(';</script>')[0].split(/; (var|const|let)/)[0]);
|
||||
const player_response = JSON.parse(
|
||||
body
|
||||
.split('var ytInitialPlayerResponse = ')[1]
|
||||
.split(';</script>')[0]
|
||||
.split(/; (var|const|let)/)[0]
|
||||
);
|
||||
const initial_response = JSON.parse(
|
||||
body
|
||||
.split('var ytInitialData = ')[1]
|
||||
.split(';</script>')[0]
|
||||
.split(/; (var|const|let)/)[0]
|
||||
);
|
||||
if (player_response.playabilityStatus.status !== 'OK')
|
||||
throw new Error(
|
||||
`While getting info from url\n${
|
||||
|
||||
@ -77,7 +77,7 @@ export async function search(
|
||||
options: SearchOptions = {}
|
||||
): Promise<YouTube[] | Spotify[] | SoundCloud[]> {
|
||||
if (!options.source) options.source = { youtube: 'video' };
|
||||
query = encodeURIComponent(query)
|
||||
query = encodeURIComponent(query);
|
||||
if (options.source.youtube) return await yt_search(query, { limit: options.limit, type: options.source.youtube });
|
||||
else if (options.source.spotify) return await sp_search(query, options.source.spotify, options.limit);
|
||||
else if (options.source.soundcloud) return await so_search(query, options.source.soundcloud, options.limit);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user