Global Search added

This commit is contained in:
killer069
2021-09-24 15:13:45 +05:30
parent 5befd9a4f4
commit 844a3a4b8c
7 changed files with 111 additions and 27 deletions

View File

@@ -33,6 +33,23 @@ export async function soundcloud(url: string): Promise<SoundCloudTrack | SoundCl
else return new SoundCloudPlaylist(json_data, soundData.client_id);
}
export async function so_search(
query: string,
type: 'tracks' | 'playlists' | 'albums',
limit: number = 10
): Promise<(SoundCloudPlaylist | SoundCloudTrack)[]> {
const response = await request(
`https://api-v2.soundcloud.com/search/${type}?q=${query}&client_id=${soundData.client_id}&limit=${limit}`
);
const results: (SoundCloudPlaylist | SoundCloudTrack)[] = [];
const json_data = JSON.parse(response);
json_data.collection.forEach((x: any) => {
if (type === 'tracks') results.push(new SoundCloudTrack(x));
else results.push(new SoundCloudPlaylist(x, soundData.client_id));
});
return results;
}
export async function stream(url: string, quality?: number): Promise<Stream> {
const data = await soundcloud(url);

View File

@@ -110,6 +110,45 @@ export function is_expired(): boolean {
else return false;
}
export async function sp_search(
query: string,
type: 'album' | 'playlist' | 'track',
limit: number = 10
): Promise<(SpotifyAlbum | SpotifyPlaylist | SpotifyVideo)[]> {
const results: (SpotifyAlbum | SpotifyPlaylist | SpotifyVideo)[] = [];
if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?');
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.replaceAll(' ', '+')}&limit=${limit}&market=${
spotifyData.market
}`,
{
headers: {
Authorization: `${spotifyData.token_type} ${spotifyData.access_token}`
}
}
).catch((err: Error) => {
return err;
});
if (response instanceof Error) throw response;
const json_data = JSON.parse(response);
if (type === 'track') {
json_data.tracks.items.forEach((track: any) => {
results.push(new SpotifyVideo(track));
});
} else if (type === 'album') {
json_data.albums.items.forEach((album: any) => {
results.push(new SpotifyAlbum(album, spotifyData));
});
} else if (type === 'playlist') {
json_data.playlists.items.forEach((playlist: any) => {
results.push(new SpotifyPlaylist(playlist, spotifyData));
});
}
return results;
}
export async function refreshToken(): Promise<boolean> {
const response = await request(`https://accounts.spotify.com/api/token`, {
headers: {

View File

@@ -1,3 +1,2 @@
export { search } from './search';
export { stream, stream_from_info } from './stream';
export * from './utils';

View File

@@ -10,7 +10,7 @@ enum SearchType {
Channel = 'EgIQAg%253D%253D'
}
export async function search(
export async function yt_search(
search: string,
options: ParseSearchInterface = {}
): Promise<(Video | Channel | PlayList)[]> {

View File

@@ -1,15 +1,25 @@
export { playlist_info, video_basic_info, video_info, search, yt_validate, extractID } from './YouTube';
export { playlist_info, video_basic_info, video_info, yt_validate, extractID } from './YouTube';
export { spotify, sp_validate, refreshToken, is_expired } from './Spotify';
export { soundcloud, so_validate } from './SoundCloud';
interface SearchOptions {
limit?: number;
source?: {
youtube?: 'video' | 'playlist' | 'channel';
spotify?: 'album' | 'playlist' | 'track';
soundcloud?: 'tracks' | 'playlists' | 'albums';
};
}
import readline from 'readline';
import fs from 'fs';
import { sp_validate, yt_validate, so_validate } from '.';
import { SpotifyAuthorize } from './Spotify';
import { check_id, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
import { SpotifyAuthorize, sp_search } from './Spotify';
import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
import { InfoData, stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
import { SoundCloudTrack, Stream as SoStream } from './SoundCloud/classes';
import { LiveStreaming, Stream as YTStream } from './YouTube/classes/LiveStream';
import { yt_search } from './YouTube/search';
export async function stream(url: string, options: StreamOptions = {}): Promise<YTStream | LiveStreaming | SoStream> {
if (url.length === 0) throw new Error('Stream URL has a length of 0. Check your url again.');
@@ -17,6 +27,14 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
else return await yt_stream(url, { cookie: options.cookie });
}
export async function search(query: string, options: SearchOptions = {}) {
if (!options.source) options.source = { youtube: 'video' };
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);
}
export async function stream_from_info(
info: InfoData | SoundCloudTrack,
options: StreamOptions = {}