mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
https Feature added
This commit is contained in:
@@ -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}`
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user