https Feature added

This commit is contained in:
killer069
2021-09-13 00:31:52 +05:30
parent 2f2de00747
commit ad22f873e5
11 changed files with 112 additions and 554 deletions

View File

@@ -1,4 +1,4 @@
import got, { Response } from "got/dist/source";
import { request } from "../YouTube/utils/request";
import { SpotifyDataOptions } from ".";
@@ -126,14 +126,14 @@ export class SpotifyPlaylist{
let work = []
for(let i = 2; i <= Math.ceil(fetching/100); i++){
work.push(new Promise(async (resolve, reject) => {
let response = await got(`https://api.spotify.com/v1/playlists/${this.id}/tracks?offset=${(i-1)*100}&limit=100&market=${this.spotifyData.market}`, {
let response = await request(`https://api.spotify.com/v1/playlists/${this.id}/tracks?offset=${(i-1)*100}&limit=100&market=${this.spotifyData.market}`, {
headers : {
"Authorization" : `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
}
}).catch((err) => reject(`Response Error : \n${err}`))
let videos: SpotifyVideo[] = []
let res = response as Response<string>
let json_data = JSON.parse(res.body)
if(typeof response !== 'string') return
let json_data = JSON.parse(response)
json_data.items.forEach((v : any) => {
videos.push(new SpotifyVideo(v.track))
})
@@ -223,14 +223,14 @@ export class SpotifyAlbum{
let work = []
for(let i = 2; i <= Math.ceil(fetching/50); i++){
work.push(new Promise(async (resolve, reject) => {
let response = await got(`https://api.spotify.com/v1/albums/${this.id}/tracks?offset=${(i-1)*50}&limit=50&market=${this.spotifyData.market}`, {
let response = await request(`https://api.spotify.com/v1/albums/${this.id}/tracks?offset=${(i-1)*50}&limit=50&market=${this.spotifyData.market}`, {
headers : {
"Authorization" : `${this.spotifyData.token_type} ${this.spotifyData.access_token}`
}
}).catch((err) => reject(`Response Error : \n${err}`))
let videos: SpotifyTracks[] = []
let res = response as Response<string>
let json_data = JSON.parse(res.body)
if(typeof response !== 'string') return
let json_data = JSON.parse(response)
json_data.items.forEach((v : any) => {
videos.push(new SpotifyTracks(v))
})

View File

@@ -1,4 +1,4 @@
import got from "got/dist/source"
import { request } from "../YouTube/utils/request";
import { SpotifyAlbum, SpotifyPlaylist, SpotifyVideo } from "./classes"
import readline from 'readline'
import fs from 'fs'
@@ -28,32 +28,32 @@ export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlayl
if(!url.match(pattern)) throw new Error('This is not a Spotify URL')
if(url.indexOf('track/') !== -1){
let trackID = url.split('track/')[1].split('&')[0].split('?')[0]
let response = await got(`https://api.spotify.com/v1/tracks/${trackID}?market=${spotifyData.market}`, {
let response = await request(`https://api.spotify.com/v1/tracks/${trackID}?market=${spotifyData.market}`, {
headers : {
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
}
}).catch((err) => {return 0})
if(typeof response !== 'number') return new SpotifyVideo(JSON.parse(response.body))
if(typeof response !== 'number') return new SpotifyVideo(JSON.parse(response))
else throw new Error('Failed to get spotify Track Data')
}
else if(url.indexOf('album/') !== -1){
let albumID = url.split('album/')[1].split('&')[0].split('?')[0]
let response = await got(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, {
let response = await request(`https://api.spotify.com/v1/albums/${albumID}?market=${spotifyData.market}`, {
headers : {
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
}
}).catch((err) => {return 0})
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response.body), spotifyData)
if(typeof response !== 'number') return new SpotifyAlbum(JSON.parse(response), spotifyData)
else throw new Error('Failed to get spotify Album Data')
}
else if(url.indexOf('playlist/') !== -1){
let playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0]
let response = await got(`https://api.spotify.com/v1/playlists/${playlistID}?market=${spotifyData.market}`, {
let response = await request(`https://api.spotify.com/v1/playlists/${playlistID}?market=${spotifyData.market}`, {
headers : {
"Authorization" : `${spotifyData.token_type} ${spotifyData.access_token}`
}
}).catch((err) => {return 0})
if(typeof response !== 'number') return new SpotifyPlaylist(JSON.parse(response.body), spotifyData)
if(typeof response !== 'number') return new SpotifyPlaylist(JSON.parse(response), spotifyData)
else throw new Error('Failed to get spotify Playlist Data')
}
else throw new Error('URL is out of scope for play-dl.')
@@ -115,17 +115,19 @@ export function Authorization(){
}
async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<boolean>{
let response = await got.post(`https://accounts.spotify.com/api/token?grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(data.redirect_url)}`, {
let response = await request(`https://accounts.spotify.com/api/token`, {
headers : {
"Authorization" : `Basic ${Buffer.from(`${data.client_id}:${data.client_secret}`).toString('base64')}`,
"Content-Type" : "application/x-www-form-urlencoded"
}
},
body : `grant_type=authorization_code&code=${data.authorization_code}&redirect_uri=${encodeURI(data.redirect_url)}`,
method : "POST"
}).catch(() => {
return 0
})
if(typeof response === 'number') return false
let resp_json = JSON.parse(response.body)
let resp_json = JSON.parse(response)
spotifyData = {
client_id : data.client_id,
client_secret : data.client_secret,
@@ -147,17 +149,19 @@ export function is_expired(){
}
export async function RefreshToken(): Promise<true | false>{
let response = await got.post(`https://accounts.spotify.com/api/token?grant_type=refresh_token&refresh_token=${spotifyData.refresh_token}`, {
let response = await request(`https://accounts.spotify.com/api/token`, {
headers : {
"Authorization" : `Basic ${Buffer.from(`${spotifyData.client_id}:${spotifyData.client_secret}`).toString('base64')}`,
"Content-Type" : "application/x-www-form-urlencoded"
}
},
body : `grant_type=refresh_token&refresh_token=${spotifyData.refresh_token}`,
method : "POST"
}).catch(() => {
return 0
})
if(typeof response === 'number') return false
let resp_json = JSON.parse(response.body)
let resp_json = JSON.parse(response)
spotifyData.access_token = resp_json.access_token
spotifyData.expires_in = Number(resp_json.expires_in)
spotifyData.expiry = Date.now() + (Number(resp_json.expires_in) * 1000)

View File

@@ -1,7 +1,7 @@
import { PassThrough } from 'stream'
import got from 'got'
import { IncomingMessage } from 'http';
import { StreamType } from '../stream';
import Request from 'got/dist/source/core';
import { request, request_stream } from '../utils/request';
import { video_info } from '..';
export interface FormatInterface{
@@ -21,7 +21,7 @@ export class LiveStreaming{
private video_url : string
private dash_timer : NodeJS.Timer | null
private segments_urls : string[]
private request : Request | null
private request : IncomingMessage | null
constructor(dash_url : string, target_interval : number, video_url : string){
this.type = StreamType.Arbitrary
this.url = dash_url
@@ -53,8 +53,8 @@ export class LiveStreaming{
}
private async dash_getter(){
let response = await got(this.url)
let audioFormat = response.body.split('<AdaptationSet id="0"')[1].split('</AdaptationSet>')[0].split('</Representation>')
let response = await request(this.url)
let audioFormat = response.split('<AdaptationSet id="0"')[1].split('</AdaptationSet>')[0].split('</Representation>')
if(audioFormat[audioFormat.length - 1] === '') audioFormat.pop()
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0]
let list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0]
@@ -90,8 +90,8 @@ export class LiveStreaming{
if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){
continue
}
await new Promise((resolve, reject) => {
let stream = got.stream(this.base_url + segment)
await new Promise(async(resolve, reject) => {
let stream = await request_stream(this.base_url + segment)
this.request = stream
stream.pipe(this.stream, { end : false })
stream.on('end', () => {
@@ -118,7 +118,7 @@ export class Stream {
private content_length : number;
private data_ended : boolean;
private playing_count : number;
private request : Request | null
private request : IncomingMessage | null
constructor(url : string, type : StreamType, duration : number, contentLength : number){
this.url = url
this.type = type
@@ -155,13 +155,13 @@ export class Stream {
this.per_sec_bytes = 0
}
private loop(){
private async loop(){
if(this.stream.destroyed){
this.cleanup()
return
}
let end : number = this.bytes_count + this.per_sec_bytes * 300;
let stream = got.stream(this.url, {
let stream = await request_stream(this.url, {
headers : {
"range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
}

View File

@@ -1,5 +1,5 @@
import { getPlaylistVideos, getContinuationToken } from "../utils/extractor";
import { url_get } from "../utils/request";
import { request } from "../utils/request";
import { Thumbnail } from "./Thumbnail";
import { Channel } from "./Channel";
import { Video } from "./Video";
@@ -62,7 +62,7 @@ export class PlayList{
async next(limit: number = Infinity): Promise<Video[]> {
if (!this._continuation || !this._continuation.token) return [];
let nextPage = await url_get(`${BASE_API}${this._continuation.api}`, {
let nextPage = await request(`${BASE_API}${this._continuation.api}`, {
method: "POST",
body: JSON.stringify({
continuation: this._continuation.token,

View File

@@ -1,4 +1,4 @@
import { url_get } from "./utils/request";
import { request } from "./utils/request";
import { ParseSearchInterface, ParseSearchResult } from "./utils/parser";
import { Video } from "./classes/Video";
import { Channel } from "./classes/Channel";
@@ -29,7 +29,7 @@ export async function search(search :string, options? : ParseSearchInterface): P
break
}
}
let body = await url_get(url, {
let body = await request(url, {
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
})
let data = ParseSearchResult(body, options)

View File

@@ -1,6 +1,6 @@
import got from "got/dist/source"
import { video_info } from "."
import { LiveStreaming, Stream } from "./classes/LiveStream"
import { request } from "./utils/request"
export enum StreamType{
Arbitrary = 'arbitrary',
@@ -41,18 +41,16 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
}
let resp = await got(info.format[info.format.length - 1].url, {
let resp = await request(info.format[info.format.length - 1].url, {
headers : {
"range" : `bytes=0-1`
},
retry : 0
}).catch(() => {
return 0
})
if(resp === 0){
return await stream(info.video_details.url, cookie)
}
else if(typeof resp !== "number") resp.destroy()
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
@@ -81,18 +79,16 @@ export async function stream_from_info(info : InfoData, cookie? : string): Promi
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
}
let resp = await got(info.format[info.format.length - 1].url, {
let resp = await request(info.format[info.format.length - 1].url, {
headers : {
"range" : `bytes=0-1`
},
retry : 0
}).catch(() => {
return 0
})
if(resp === 0){
return await stream(info.video_details.url, cookie)
}
else if(typeof resp !== "number") resp.destroy()
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")

View File

@@ -1,5 +1,5 @@
import { URL } from 'url'
import { url_get } from './request'
import { request } from './request'
import querystring from 'querystring'
interface formatOptions {
@@ -131,7 +131,6 @@ function deciper_signature(tokens : string[], signature :string){
function swappositions(array : string[], position : number){
let first = array[0]
let pos_args = array[position]
array[0] = array[position]
array[position] = first
return array
@@ -154,7 +153,7 @@ function download_url(format: formatOptions, sig : string){
}
export async function format_decipher(formats: formatOptions[], html5player : string){
let body = await url_get(html5player)
let body = await request(html5player)
let tokens = js_tokens(body)
formats.forEach((format) => {
let cipher = format.signatureCipher || format.cipher;

View File

@@ -1,4 +1,4 @@
import { url_get } from './request'
import { request } from './request'
import { format_decipher, js_tokens } from './cipher'
import { Video } from '../classes/Video'
import { PlayList } from '../classes/Playlist'
@@ -46,7 +46,7 @@ export async function video_basic_info(url : string, cookie? : string){
}
else video_id = url
let new_url = `https://www.youtube.com/watch?v=${video_id}`
let body = await url_get(new_url, {
let body = await request(new_url, {
headers : (cookie) ? { 'cookie' : cookie, 'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' } : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
})
let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}')
@@ -129,7 +129,7 @@ export async function playlist_info(url : string, parseIncomplete : boolean = fa
else Playlist_id = url
let new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`
let body = await url_get(new_url, {
let body = await request(new_url, {
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
})
let response = JSON.parse(body.split("var ytInitialData = ")[1].split(";</script>")[0])

View File

@@ -1,9 +1,56 @@
import got, { OptionsOfTextResponseBody } from 'got/dist/source'
import https, { RequestOptions } from 'https'
import {IncomingMessage } from 'http'
import { URL } from 'url'
export async function url_get (url : string, options? : OptionsOfTextResponseBody) : Promise<string>{
let response = await got(url, options)
if(response.statusCode === 200) {
return response.body
}
else throw new Error(`Got ${response.statusCode} from ${url}`)
interface RequestOpts extends RequestOptions{
body? : string;
method? : "GET" | "POST"
}
async function https_getter(req_url : string, options : RequestOpts = {}): Promise<IncomingMessage>{
return new Promise((resolve, reject) => {
let s = new URL(req_url)
if(!options.method) options.method = "GET"
let req_options : RequestOptions = {
host : s.hostname,
path : s.pathname + s.search,
headers : (options.headers) ? options.headers : {}
}
let req = https.request(req_options, (response) => {
resolve(response)
})
if(options.method === "POST") req.write(options.body)
req.end()
})
}
export async function request(url : string, options? : RequestOpts): Promise<string>{
return new Promise(async (resolve, reject) => {
let data = ''
let res = await https_getter(url, options)
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
res = await https_getter(res.headers.location as string , options)
}
else if(Number(res.statusCode) > 400){
reject(`Got ${res.statusCode} from the request`)
}
res.setEncoding('utf-8')
res.on('data', (c) => data+=c)
res.on('end', () => resolve(data))
})
}
export async function request_stream(url : string, options? : RequestOpts): Promise<IncomingMessage>{
return new Promise(async (resolve, reject) => {
let data = ''
let res = await https_getter(url, options)
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
res = await https_getter(res.headers.location as string, options)
}
else if(Number(res.statusCode) > 400){
reject(`Got ${res.statusCode} from the request`)
}
resolve(res)
})
}