This commit is contained in:
killer069
2021-08-07 15:53:18 +05:30
parent 408a745cc5
commit f91508d652
6 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
import { get } from "./request";
import fs from 'fs'
export function valid_url(url : string): boolean{
let valid_url = /^https?:\/\/(youtu\.be\/|(www\.)?youtube\.com\/(embed|watch|v|shorts)(\/|\?))/
if(url.search(valid_url) !== -1) return true
else return false
}
export async function getBasicInfo(url : string){
if(valid_url(url)){
let body = await get(url)
var final ={
initial_data : yt_initial_data(body)
}
}
else {
throw 'Not a Valid YouTube URL'
process.exit(1)
}
}
function parse_json(str : string, start_index : number){
let matches = 0
let result : string = '';
if(str[start_index] !== '{') {
throw 'Start Index is wrong.'
process.exit(1)
}
matches++
result += str[start_index]
for(let x = start_index + 1; x <= str.length; x++){
if(matches === 0) break
if(str[x] === '(' || str[x] === '{' || str[x] === '[') matches++
if(str[x] === ')' || str[x] === '}' || str[x] === ']') matches--
result += str[x]
}
return JSON.parse(result)
}
function yt_initial_data(data : string): JSON{
let pattern = /ytInitialData\s=\s/
return parse_json(data, data.search(pattern) + 16)
}

View File

@@ -0,0 +1,10 @@
import fetch from 'node-fetch'
export async function get (url : string) : Promise<string>{
return new Promise(async(resolve, reject) => {
let response = await fetch(url)
if(response.status === 200) resolve(await response.text())
else reject(`Got ${response.status} from ${url}`)
})
}

3
node-youtube-dl/index.ts Normal file
View File

@@ -0,0 +1,3 @@
import { getBasicInfo } from "./YouTube/utils/extractor";
getBasicInfo('https://www.youtube.com/watch?v=V_jHc_n0p9c')