SetToken function added
This commit is contained in:
parent
80d6ecba96
commit
229bbc7a11
@ -151,3 +151,7 @@ function parseHlsFormats(data: SoundCloudTrackFormat[]) {
|
|||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setSoundCloudToken(options : SoundDataOptions){
|
||||||
|
soundData = options
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@ import fs from 'fs';
|
|||||||
let spotifyData: SpotifyDataOptions;
|
let spotifyData: SpotifyDataOptions;
|
||||||
if (fs.existsSync('.data/spotify.data')) {
|
if (fs.existsSync('.data/spotify.data')) {
|
||||||
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString());
|
spotifyData = JSON.parse(fs.readFileSync('.data/spotify.data').toString());
|
||||||
|
spotifyData.file = true
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Spotify Data options that are stored in spotify.data file.
|
* Spotify Data options that are stored in spotify.data file.
|
||||||
@ -12,7 +13,7 @@ if (fs.existsSync('.data/spotify.data')) {
|
|||||||
export interface SpotifyDataOptions {
|
export interface SpotifyDataOptions {
|
||||||
client_id: string;
|
client_id: string;
|
||||||
client_secret: string;
|
client_secret: string;
|
||||||
redirect_url: string;
|
redirect_url?: string;
|
||||||
authorization_code?: string;
|
authorization_code?: string;
|
||||||
access_token?: string;
|
access_token?: string;
|
||||||
refresh_token?: string;
|
refresh_token?: string;
|
||||||
@ -20,6 +21,7 @@ export interface SpotifyDataOptions {
|
|||||||
expires_in?: number;
|
expires_in?: number;
|
||||||
expiry?: number;
|
expiry?: number;
|
||||||
market?: string;
|
market?: string;
|
||||||
|
file? : boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//;
|
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//;
|
||||||
@ -89,14 +91,14 @@ export function sp_validate(url: string): 'track' | 'playlist' | 'album' | 'sear
|
|||||||
* @param data Sportify Data options to validate
|
* @param data Sportify Data options to validate
|
||||||
* @returns boolean.
|
* @returns boolean.
|
||||||
*/
|
*/
|
||||||
export async function SpotifyAuthorize(data: SpotifyDataOptions): Promise<boolean> {
|
export async function SpotifyAuthorize(data: SpotifyDataOptions, file : boolean): Promise<boolean> {
|
||||||
const response = await request(`https://accounts.spotify.com/api/token`, {
|
const response = await request(`https://accounts.spotify.com/api/token`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
|
'Authorization': `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
},
|
},
|
||||||
body: `grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(
|
body: `grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(
|
||||||
data.redirect_url
|
data.redirect_url as string
|
||||||
)}`,
|
)}`,
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
}).catch((err: Error) => {
|
}).catch((err: Error) => {
|
||||||
@ -115,7 +117,14 @@ export async function SpotifyAuthorize(data: SpotifyDataOptions): Promise<boolea
|
|||||||
token_type: resp_json.token_type,
|
token_type: resp_json.token_type,
|
||||||
market: data.market
|
market: data.market
|
||||||
};
|
};
|
||||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
if(file) fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
||||||
|
else {
|
||||||
|
console.log(`Client ID : ${spotifyData.client_id}`)
|
||||||
|
console.log(`Client Secret : ${spotifyData.client_secret}`)
|
||||||
|
console.log(`Refresh Token : ${spotifyData.refresh_token}`)
|
||||||
|
console.log(`Market : ${spotifyData.market}`)
|
||||||
|
console.log(`\nPaste above info in setToken function.`)
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -198,6 +207,12 @@ export async function refreshToken(): Promise<boolean> {
|
|||||||
spotifyData.expires_in = Number(resp_json.expires_in);
|
spotifyData.expires_in = Number(resp_json.expires_in);
|
||||||
spotifyData.expiry = Date.now() + (resp_json.expires_in - 1) * 1000;
|
spotifyData.expiry = Date.now() + (resp_json.expires_in - 1) * 1000;
|
||||||
spotifyData.token_type = resp_json.token_type;
|
spotifyData.token_type = resp_json.token_type;
|
||||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
if(spotifyData.file) fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setSpotifyToken(options: SpotifyDataOptions) {
|
||||||
|
spotifyData = options
|
||||||
|
spotifyData.file = false
|
||||||
|
refreshToken()
|
||||||
|
}
|
||||||
@ -3,10 +3,12 @@ import fs from 'fs';
|
|||||||
let youtubeData: youtubeDataOptions;
|
let youtubeData: youtubeDataOptions;
|
||||||
if (fs.existsSync('.data/youtube.data')) {
|
if (fs.existsSync('.data/youtube.data')) {
|
||||||
youtubeData = JSON.parse(fs.readFileSync('.data/youtube.data').toString());
|
youtubeData = JSON.parse(fs.readFileSync('.data/youtube.data').toString());
|
||||||
|
youtubeData.file = true
|
||||||
}
|
}
|
||||||
|
|
||||||
interface youtubeDataOptions {
|
interface youtubeDataOptions {
|
||||||
cookie?: Object;
|
cookie?: Object;
|
||||||
|
file? : boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCookies(): undefined | string {
|
export function getCookies(): undefined | string {
|
||||||
@ -27,5 +29,19 @@ export function setCookie(key: string, value: string): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function uploadCookie() {
|
export function uploadCookie() {
|
||||||
if (youtubeData) fs.writeFileSync('.data/youtube.data', JSON.stringify(youtubeData, undefined, 4));
|
if (youtubeData.cookie && youtubeData.file) fs.writeFileSync('.data/youtube.data', JSON.stringify(youtubeData, undefined, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setCookieToken(options : { cookie : string }){
|
||||||
|
let cook = options.cookie
|
||||||
|
let cookie: Object = {};
|
||||||
|
cook.split(';').forEach((x) => {
|
||||||
|
const arr = x.split('=');
|
||||||
|
if (arr.length <= 1) return;
|
||||||
|
const key = arr.shift()?.trim() as string;
|
||||||
|
const value = arr.join('=').trim();
|
||||||
|
Object.assign(cookie, { [key]: value });
|
||||||
|
});
|
||||||
|
youtubeData = { cookie }
|
||||||
|
youtubeData.file = false
|
||||||
|
}
|
||||||
169
play-dl/index.ts
169
play-dl/index.ts
@ -1,6 +1,7 @@
|
|||||||
export { playlist_info, video_basic_info, video_info, yt_validate, extractID, YouTube, YouTubeStream } from './YouTube';
|
export { playlist_info, video_basic_info, video_info, yt_validate, extractID, YouTube, YouTubeStream } from './YouTube';
|
||||||
export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify';
|
export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify';
|
||||||
export { soundcloud, so_validate, SoundCloud, SoundCloudStream } from './SoundCloud';
|
export { soundcloud, so_validate, SoundCloud, SoundCloudStream } from './SoundCloud';
|
||||||
|
export { setToken } from './token'
|
||||||
|
|
||||||
enum AudioPlayerStatus {
|
enum AudioPlayerStatus {
|
||||||
Idle = 'idle',
|
Idle = 'idle',
|
||||||
@ -114,92 +115,112 @@ export function authorization(): void {
|
|||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout
|
output: process.stdout
|
||||||
});
|
});
|
||||||
ask.question('Choose your service - sc (for SoundCloud) / sp (for Spotify) / yo (for YouTube): ', (msg) => {
|
ask.question('Do you want to save data in a file ? (Yes / No): ', (msg) => {
|
||||||
if (msg.toLowerCase().startsWith('sp')) {
|
let file : boolean;
|
||||||
let client_id: string, client_secret: string, redirect_url: string, market: string;
|
if(msg.toLowerCase() === 'yes') file = true
|
||||||
ask.question('Start by entering your Client ID : ', (id) => {
|
else if(msg.toLowerCase() === 'no') file = false
|
||||||
client_id = id;
|
else {
|
||||||
ask.question('Now enter your Client Secret : ', (secret) => {
|
console.log("That option doesn't exist. Try again...");
|
||||||
client_secret = secret;
|
ask.close();
|
||||||
ask.question('Enter your Redirect URL now : ', (url) => {
|
return;
|
||||||
redirect_url = url;
|
}
|
||||||
console.log(
|
ask.question('Choose your service - sc (for SoundCloud) / sp (for Spotify) / yo (for YouTube): ', (msg) => {
|
||||||
'\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'
|
if (msg.toLowerCase().startsWith('sp')) {
|
||||||
);
|
let client_id: string, client_secret: string, redirect_url: string, market: string;
|
||||||
ask.question('Enter your region code (2-letter country code) : ', (mar) => {
|
ask.question('Start by entering your Client ID : ', (id) => {
|
||||||
if (mar.length === 2) market = mar;
|
client_id = id;
|
||||||
else {
|
ask.question('Now enter your Client Secret : ', (secret) => {
|
||||||
|
client_secret = secret;
|
||||||
|
ask.question('Enter your Redirect URL now : ', (url) => {
|
||||||
|
redirect_url = url;
|
||||||
|
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) => {
|
||||||
|
if (mar.length === 2) market = mar;
|
||||||
|
else {
|
||||||
|
console.log(
|
||||||
|
"That doesn't look like a valid region code, IN will be selected as default."
|
||||||
|
);
|
||||||
|
market = 'IN';
|
||||||
|
}
|
||||||
console.log(
|
console.log(
|
||||||
"That doesn't look like a valid region code, IN will be selected as default."
|
'\nNow open your browser and paste the below url, then authorize it and copy the redirected url. \n'
|
||||||
);
|
);
|
||||||
market = 'IN';
|
console.log(
|
||||||
}
|
`https://accounts.spotify.com/authorize?client_id=${client_id}&response_type=code&redirect_uri=${encodeURI(
|
||||||
console.log(
|
redirect_url
|
||||||
'\nNow open your browser and paste the below url, then authorize it and copy the redirected url. \n'
|
)} \n`
|
||||||
);
|
);
|
||||||
console.log(
|
ask.question('Paste the url which you just copied : ', async (url) => {
|
||||||
`https://accounts.spotify.com/authorize?client_id=${client_id}&response_type=code&redirect_uri=${encodeURI(
|
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
||||||
redirect_url
|
const spotifyData = {
|
||||||
)} \n`
|
client_id,
|
||||||
);
|
client_secret,
|
||||||
ask.question('Paste the url which you just copied : ', async (url) => {
|
redirect_url,
|
||||||
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
authorization_code: url.split('code=')[1],
|
||||||
const spotifyData = {
|
market
|
||||||
client_id,
|
};
|
||||||
client_secret,
|
const check = await SpotifyAuthorize(spotifyData, file);
|
||||||
redirect_url,
|
if (check === false) throw new Error('Failed to get access token.');
|
||||||
authorization_code: url.split('code=')[1],
|
ask.close();
|
||||||
market
|
});
|
||||||
};
|
|
||||||
const check = await SpotifyAuthorize(spotifyData);
|
|
||||||
if (check === false) throw new Error('Failed to get access token.');
|
|
||||||
ask.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
} else if (msg.toLowerCase().startsWith('sc')) {
|
||||||
} else if (msg.toLowerCase().startsWith('sc')) {
|
if(!file){
|
||||||
ask.question('Client ID : ', async (id) => {
|
console.log("You already had a client ID, just paste that in setToken function.");
|
||||||
let client_id = id;
|
|
||||||
if (!client_id) {
|
|
||||||
console.log("You didn't provide a client ID. Try again...");
|
|
||||||
ask.close();
|
ask.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
ask.question('Client ID : ', async (id) => {
|
||||||
console.log('Validating your client ID, hold on...');
|
let client_id = id;
|
||||||
if (await check_id(client_id)) {
|
if (!client_id) {
|
||||||
console.log('Client ID has been validated successfully.');
|
console.log("You didn't provide a client ID. Try again...");
|
||||||
fs.writeFileSync('.data/soundcloud.data', JSON.stringify({ client_id }, undefined, 4));
|
ask.close();
|
||||||
} else console.log("That doesn't look like a valid client ID. Retry with a correct client ID.");
|
return;
|
||||||
ask.close();
|
}
|
||||||
});
|
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
||||||
} else if (msg.toLowerCase().startsWith('yo')) {
|
console.log('Validating your client ID, hold on...');
|
||||||
ask.question('Cookies : ', (cook: string) => {
|
if (await check_id(client_id)) {
|
||||||
if (!cook || cook.length === 0) {
|
console.log('Client ID has been validated successfully.');
|
||||||
console.log("You didn't provide a cookie. Try again...");
|
fs.writeFileSync('.data/soundcloud.data', JSON.stringify({ client_id }, undefined, 4));
|
||||||
|
} else console.log("That doesn't look like a valid client ID. Retry with a correct client ID.");
|
||||||
ask.close();
|
ask.close();
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
|
||||||
console.log('Cookies has been added successfully.');
|
|
||||||
let cookie: Object = {};
|
|
||||||
cook.split(';').forEach((x) => {
|
|
||||||
const arr = x.split('=');
|
|
||||||
if (arr.length <= 1) return;
|
|
||||||
const key = arr.shift()?.trim() as string;
|
|
||||||
const value = arr.join('=').trim();
|
|
||||||
Object.assign(cookie, { [key]: value });
|
|
||||||
});
|
});
|
||||||
fs.writeFileSync('.data/youtube.data', JSON.stringify({ cookie }, undefined, 4));
|
} else if (msg.toLowerCase().startsWith('yo')) {
|
||||||
|
if(!file){
|
||||||
|
console.log("You already had cookie, just paste that in setToken function.");
|
||||||
|
ask.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ask.question('Cookies : ', (cook: string) => {
|
||||||
|
if (!cook || cook.length === 0) {
|
||||||
|
console.log("You didn't provide a cookie. Try again...");
|
||||||
|
ask.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fs.existsSync('.data')) fs.mkdirSync('.data');
|
||||||
|
console.log('Cookies has been added successfully.');
|
||||||
|
let cookie: Object = {};
|
||||||
|
cook.split(';').forEach((x) => {
|
||||||
|
const arr = x.split('=');
|
||||||
|
if (arr.length <= 1) return;
|
||||||
|
const key = arr.shift()?.trim() as string;
|
||||||
|
const value = arr.join('=').trim();
|
||||||
|
Object.assign(cookie, { [key]: value });
|
||||||
|
});
|
||||||
|
fs.writeFileSync('.data/youtube.data', JSON.stringify({ cookie }, undefined, 4));
|
||||||
|
ask.close();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("That option doesn't exist. Try again...");
|
||||||
ask.close();
|
ask.close();
|
||||||
});
|
}
|
||||||
} else {
|
});
|
||||||
console.log("That option doesn't exist. Try again...");
|
})
|
||||||
ask.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function attachListeners(player: EventEmitter, resource: YouTubeStream | SoundCloudStream) {
|
export function attachListeners(player: EventEmitter, resource: YouTubeStream | SoundCloudStream) {
|
||||||
|
|||||||
24
play-dl/token.ts
Normal file
24
play-dl/token.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { setSoundCloudToken } from "./SoundCloud";
|
||||||
|
import { setSpotifyToken } from "./Spotify";
|
||||||
|
import { setCookieToken } from "./YouTube/utils/cookie";
|
||||||
|
|
||||||
|
interface tokenOptions {
|
||||||
|
spotify? : {
|
||||||
|
client_id : string
|
||||||
|
client_secret : string;
|
||||||
|
refresh_token : string
|
||||||
|
market : string
|
||||||
|
}
|
||||||
|
soundcloud? : {
|
||||||
|
client_id : string
|
||||||
|
}
|
||||||
|
youtube? : {
|
||||||
|
cookie : string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setToken(options : tokenOptions){
|
||||||
|
if(options.spotify) setSpotifyToken(options.spotify)
|
||||||
|
if(options.soundcloud) setSoundCloudToken(options.soundcloud)
|
||||||
|
if(options.youtube) setCookieToken(options.youtube)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user