2021-09-20 17:20:15 +05:30
export { playlist_info , video_basic_info , video_info , search , yt_validate , extractID } from './YouTube' ;
2021-09-17 14:36:32 +05:30
export { spotify , sp_validate , refreshToken , is_expired } from './Spotify' ;
2021-09-20 17:20:15 +05:30
export { soundcloud , so_validate } from './SoundCloud' ;
2021-09-05 18:50:57 +05:30
2021-09-20 17:20:15 +05:30
import readline from 'readline' ;
2021-09-17 14:36:32 +05:30
import fs from 'fs' ;
2021-09-20 17:20:15 +05:30
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 { InfoData , stream as yt_stream , stream_from_info as yt_stream_info } from './YouTube/stream' ;
import { SoundCloudTrack , Stream as SoStream } from './SoundCloud/classes' ;
2021-09-21 13:00:38 +05:30
import { LiveStreaming , Stream as YTStream } from './YouTube/classes/LiveStream' ;
2021-09-17 14:36:32 +05:30
2021-09-21 13:00:38 +05:30
export async function stream ( url : string , cookie? : string ) : Promise < YTStream | LiveStreaming | SoStream > {
2021-09-20 17:20:15 +05:30
if ( url . indexOf ( 'soundcloud' ) !== - 1 ) return await so_stream ( url ) ;
else return await yt_stream ( url , cookie ) ;
}
export async function stream_from_info (
info : InfoData | SoundCloudTrack ,
cookie? : string
2021-09-21 13:00:38 +05:30
) : Promise < YTStream | LiveStreaming | SoStream > {
2021-09-20 17:20:15 +05:30
if ( info instanceof SoundCloudTrack ) return await so_stream_info ( info ) ;
else return await yt_stream_info ( info , cookie ) ;
}
2021-09-24 10:53:57 +05:30
export async function validate ( url : string ) : Promise < "so_playlist" | "so_track" | "sp_track" | "sp_album" | "sp_playlist" | "yt_video" | "yt_playlist" | false > {
let check ;
2021-09-24 11:09:22 +05:30
if ( url . indexOf ( 'spotify' ) !== - 1 ) {
2021-09-24 10:53:57 +05:30
check = sp_validate ( url ) ;
return check !== false ? 'sp_' + check as "sp_track" | "sp_album" | "sp_playlist" : false ;
2021-09-24 11:09:22 +05:30
} else if ( url . indexOf ( 'soundcloud' ) !== - 1 ) {
2021-09-24 10:53:57 +05:30
check = await so_validate ( url ) ;
return check !== false ? 'so_' + check as "so_playlist" | "so_track" : false ;
2021-09-17 14:36:32 +05:30
} else {
2021-09-24 10:53:57 +05:30
check = yt_validate ( url ) ;
return check !== false ? 'yt_' + check as "yt_video" | "yt_playlist" : false ;
2021-09-05 18:50:57 +05:30
}
2021-09-17 14:36:32 +05:30
}
2021-09-20 17:20:15 +05:30
export function authorization ( ) : void {
2021-09-17 14:36:32 +05:30
const ask = readline . createInterface ( {
input : process.stdin ,
output : process.stdout
} ) ;
2021-09-26 14:09:07 +05:30
ask . question ( 'Choose your service - so (for SoundCloud) / sp (for Spotify) : ' , ( msg ) = > {
2021-09-17 14:36:32 +05:30
if ( msg . toLowerCase ( ) . startsWith ( 'sp' ) ) {
let client_id : string , client_secret : string , redirect_url : string , market : string ;
2021-09-26 14:09:07 +05:30
ask . question ( 'Start by entering your Client ID : ' , ( id ) = > {
2021-09-17 14:36:32 +05:30
client_id = id ;
2021-09-26 14:09:07 +05:30
ask . question ( 'Now enter your Client Secret : ' , ( secret ) = > {
2021-09-17 14:36:32 +05:30
client_secret = secret ;
2021-09-26 14:09:07 +05:30
ask . question ( 'Enter your Redirect URL now : ' , ( url ) = > {
2021-09-17 14:36:32 +05:30
redirect_url = url ;
2021-09-26 14:09:07 +05:30
console . log ( '\nIf you would like to know your region code visit : \nhttps://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements \n' ) ;
ask . question ( 'Enter your region code (2-letter country code) : ' , ( mar ) = > {
2021-09-17 14:36:32 +05:30
if ( mar . length === 2 ) market = mar ;
else {
2021-09-26 14:09:07 +05:30
console . log ( 'That doesn\'t look like a valid region code, IN will be selected as default.' ) ;
2021-09-17 14:36:32 +05:30
market = 'IN' ;
}
console . log (
2021-09-26 14:09:07 +05:30
'\nNow open your browser and paste the below url, then authorize it and copy the redirected url. \n'
2021-09-17 14:36:32 +05:30
) ;
console . log (
` https://accounts.spotify.com/authorize?client_id= ${ client_id } &response_type=code&redirect_uri= ${ encodeURI (
redirect_url
) } \ n `
) ;
2021-09-26 14:09:07 +05:30
ask . question ( 'Paste the url which you just copied : ' , async ( url ) = > {
2021-09-17 14:36:32 +05:30
if ( ! fs . existsSync ( '.data' ) ) fs . mkdirSync ( '.data' ) ;
const spotifyData = {
client_id ,
client_secret ,
redirect_url ,
authorization_code : url.split ( 'code=' ) [ 1 ] ,
market
} ;
const check = await SpotifyAuthorize ( spotifyData ) ;
2021-09-26 14:09:07 +05:30
if ( check === false ) throw new Error ( 'Failed to get access token.' ) ;
2021-09-17 14:36:32 +05:30
ask . close ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} else if ( msg . toLowerCase ( ) . startsWith ( 'so' ) ) {
let client_id : string ;
ask . question ( 'Client ID : ' , async ( id ) = > {
client_id = id ;
if ( ! client_id ) {
2021-09-26 14:09:07 +05:30
console . log ( "You didn't provide a client ID. Try again..." ) ;
2021-09-17 14:36:32 +05:30
ask . close ( ) ;
return ;
}
if ( ! fs . existsSync ( '.data' ) ) fs . mkdirSync ( '.data' ) ;
2021-09-26 14:09:07 +05:30
console . log ( 'Validating your client ID, hold on...' ) ;
2021-09-17 14:36:32 +05:30
if ( await check_id ( client_id ) ) {
2021-09-26 14:09:07 +05:30
console . log ( 'Client ID has been validated successfully.' ) ;
2021-09-17 14:36:32 +05:30
fs . writeFileSync ( '.data/soundcloud.data' , JSON . stringify ( { client_id } , undefined , 4 ) ) ;
2021-09-26 14:09:07 +05:30
} else console . log ( 'That doesn\'t look like a valid client ID. Retry with a correct client ID.' ) ;
2021-09-17 14:36:32 +05:30
ask . close ( ) ;
} ) ;
} else {
2021-09-26 14:09:07 +05:30
console . log ( 'That option doesn\'t exist. Try again...' ) ;
2021-09-17 14:36:32 +05:30
ask . close ( ) ;
2021-09-05 18:50:57 +05:30
}
2021-09-17 14:36:32 +05:30
} ) ;
}