Some more documentation work (90% complete)

This commit is contained in:
killer069 2021-11-22 16:03:33 +05:30
parent 283d864183
commit 029ae9b8f3
4 changed files with 83 additions and 19 deletions

View File

@ -13,9 +13,20 @@ interface SoundDataOptions {
const pattern = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(api\.soundcloud\.com|soundcloud\.com|snd\.sc)\/(.*)$/; const pattern = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(api\.soundcloud\.com|soundcloud\.com|snd\.sc)\/(.*)$/;
/** /**
* Function to get info from a soundcloud url * Gets info from a soundcloud url.
*
* ```ts
* let sound = await play.soundcloud('soundcloud url')
*
* // sound.type === "track" | "playlist" | "user"
*
* if (sound.type === "track") {
* spot = spot as play.SoundCloudTrack
* // Code with SoundCloud track class.
* }
* ```
* @param url soundcloud url * @param url soundcloud url
* @returns SoundCloud Track or SoundCloud Playlist * @returns A {@link SoundCloudTrack} or {@link SoundCloudPlaylist}
*/ */
export async function soundcloud(url: string): Promise<SoundCloud> { export async function soundcloud(url: string): Promise<SoundCloud> {
if (!soundData) throw new Error('SoundCloud Data is missing\nDid you forgot to do authorization ?'); if (!soundData) throw new Error('SoundCloud Data is missing\nDid you forgot to do authorization ?');
@ -85,7 +96,17 @@ export async function stream(url: string, quality?: number): Promise<SoundCloudS
return new SoundCloudStream(s_data.url, type); return new SoundCloudStream(s_data.url, type);
} }
/** /**
* Function to get Free Client ID of soundcloud. * Gets Free SoundCloud Client ID.
*
* Use this in beginning of your code to add SoundCloud support.
*
* ```ts
* play.getFreeClientID().then((clientID) => play.setToken({
* soundcloud : {
* client_id : clientID
* }
* }))
* ```
* @returns client ID * @returns client ID
*/ */
export async function getFreeClientID(): Promise<string> { export async function getFreeClientID(): Promise<string> {
@ -133,12 +154,16 @@ export async function check_id(id: string): Promise<boolean> {
else return true; else return true;
} }
/** /**
* Function to validate for a soundcloud url * Validates a soundcloud url
* @param url soundcloud url * @param url soundcloud url
* @returns "false" | 'track' | 'playlist' * @returns
* ```ts
* false | 'track' | 'playlist'
* ```
*/ */
export async function so_validate(url: string): Promise<false | 'track' | 'playlist' | 'search'> { export async function so_validate(url: string): Promise<false | 'track' | 'playlist' | 'search'> {
if (!url.match(pattern)) return 'search'; if (!url.startsWith('https')) return 'search';
if (!url.match(pattern)) return false;
const data = await request( const data = await request(
`https://api-v2.soundcloud.com/resolve?url=${url}&client_id=${soundData.client_id}` `https://api-v2.soundcloud.com/resolve?url=${url}&client_id=${soundData.client_id}`
).catch((err: Error) => err); ).catch((err: Error) => err);
@ -153,7 +178,7 @@ export async function so_validate(url: string): Promise<false | 'track' | 'playl
/** /**
* Function to select only hls streams from SoundCloud format array * Function to select only hls streams from SoundCloud format array
* @param data SoundCloud Track Format data * @param data SoundCloud Track Format data
* @returns a new array containing hls formats * @returns HLS Formats Array
*/ */
function parseHlsFormats(data: SoundCloudTrackFormat[]) { function parseHlsFormats(data: SoundCloudTrackFormat[]) {
const result: SoundCloudTrackFormat[] = []; const result: SoundCloudTrackFormat[] = [];

View File

@ -26,9 +26,20 @@ export interface SpotifyDataOptions {
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//; const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//;
/** /**
* Function to get Playlist | Album | Track * Gets Spotify url details.
* @param url url of spotify from which you want info *
* @returns Spotify type. * ```ts
* let spot = await play.spotify('spotify url')
*
* // spot.type === "track" | "playlist" | "album"
*
* if (spot.type === "track") {
* spot = spot as play.SpotifyTrack
* // Code with spotify track class.
* }
* ```
* @param url Spotify Url
* @returns A {@link SpotifyTrack} or {@link SpotifyPlaylist} or {@link SpotifyAlbum}
*/ */
export async function spotify(url: string): Promise<Spotify> { export async function spotify(url: string): Promise<Spotify> {
if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?'); if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?');
@ -72,12 +83,16 @@ export async function spotify(url: string): Promise<Spotify> {
} else throw new Error('URL is out of scope for play-dl.'); } else throw new Error('URL is out of scope for play-dl.');
} }
/** /**
* Function to validate Spotify url * Validate Spotify url
* @param url url for validation * @param url Spotify URL
* @returns type of url or false. * @returns
* ```ts
* 'track' | 'playlist' | 'album' | 'search' | false
* ```
*/ */
export function sp_validate(url: string): 'track' | 'playlist' | 'album' | 'search' | false { export function sp_validate(url: string): 'track' | 'playlist' | 'album' | 'search' | false {
if (!url.match(pattern)) return 'search'; if (!url.startsWith('https')) return 'search';
if (!url.match(pattern)) return false;
if (url.indexOf('track/') !== -1) { if (url.indexOf('track/') !== -1) {
return 'track'; return 'track';
} else if (url.indexOf('album/') !== -1) { } else if (url.indexOf('album/') !== -1) {
@ -128,7 +143,14 @@ export async function SpotifyAuthorize(data: SpotifyDataOptions, file: boolean):
return true; return true;
} }
/** /**
* Function to check if authorization token is expired or not. * Checks if spotify token is expired or not.
*
* Update token if returned false.
* ```ts
* if (!play.is_expired()) {
* await play.refreshToken()
* }
* ```
* @returns boolean * @returns boolean
*/ */
export function is_expired(): boolean { export function is_expired(): boolean {
@ -183,8 +205,14 @@ export async function sp_search(
return results; return results;
} }
/** /**
* Function to refresh Token * Refreshes Token
* @returns boolean to check whether token is refreshed or not *
* ```ts
* if (!play.is_expired()) {
* await play.refreshToken()
* }
* ```
* @returns boolean
*/ */
export async function refreshToken(): Promise<boolean> { export async function refreshToken(): Promise<boolean> {
const response = await request(`https://accounts.spotify.com/api/token`, { const response = await request(`https://accounts.spotify.com/api/token`, {

View File

@ -61,13 +61,13 @@ import { YouTubePlayList } from './YouTube/classes/Playlist';
import { YouTubeChannel } from './YouTube/classes/Channel'; import { YouTubeChannel } from './YouTube/classes/Channel';
import { SpotifyAlbum, SpotifyPlaylist, SpotifyTrack } from './Spotify/classes'; import { SpotifyAlbum, SpotifyPlaylist, SpotifyTrack } from './Spotify/classes';
import { DeezerAlbum, DeezerPlaylist, DeezerTrack } from './Deezer/classes'; import { DeezerAlbum, DeezerPlaylist, DeezerTrack } from './Deezer/classes';
/** /**
* Main stream Command for streaming through various sources * Main stream Command for streaming through various sources
* @param url The video / track url to make stream of * @param url The video / track url to make stream of
* @param options contains quality, cookie and proxy to set for stream * @param options contains quality, cookie and proxy to set for stream
* @returns YouTube / SoundCloud Stream to play * @returns YouTube / SoundCloud Stream to play
*/ */
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream | SoundCloudStream> { export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream | SoundCloudStream> {
if (url.length === 0) throw new Error('Stream URL has a length of 0. Check your url again.'); if (url.length === 0) throw new Error('Stream URL has a length of 0. Check your url again.');
if (url.indexOf('spotify') !== -1) { if (url.indexOf('spotify') !== -1) {

View File

@ -16,7 +16,18 @@ interface tokenOptions {
cookie: string; cookie: string;
}; };
} }
/**
* Sets
*
* i> YouTube :- cookies.
*
* ii> SoundCloud :- client ID.
*
* iii> Spotify :- client ID, client secret, refresh token, market.
*
* locally in memory.
* @param options {@link tokenOptions}
*/
export function setToken(options: tokenOptions) { export function setToken(options: tokenOptions) {
if (options.spotify) setSpotifyToken(options.spotify); if (options.spotify) setSpotifyToken(options.spotify);
if (options.soundcloud) setSoundCloudToken(options.soundcloud); if (options.soundcloud) setSoundCloudToken(options.soundcloud);