Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require("axios");
- var download = require("./download");
- const fs = require("fs");
- var functions = require("./functions");
- var folder = require("./folder");
- const os = require("os");
- const shell = require("shelljs");
- shell.config.silent = true;
- async function download_hls(link,path) {
- return await new Promise((resolve,reject) => {
- var schema = link.split("/")[0].replace(/:/,"");
- var domain = link.split("/")[2];
- var id = link.split("?id=")[1];
- if(id.includes("&")) id = id.split("&")[0];
- if(id) {
- axios({
- method:"POST",
- url: `${schema}://${domain}/vl/${id}`,
- headers: {
- "origin": `${schema}://${domain}`
- }
- }).then(async(response) => {
- var json = response.data;
- var quality = "";
- if(json.hasOwnProperty("2080p")) quality = "2080p";
- else if(json.hasOwnProperty("1080p")) quality = "1080p";
- else if(json.hasOwnProperty("720p")) quality = "720p";
- else if(json.hasOwnProperty("480p")) quality = "480p";
- else if(json.hasOwnProperty("360p")) quality = "360p";
- if(quality) {
- var md5 = json[quality].md5;
- var data = json[quality].data;
- var array = data[2];
- var file = [...new Set(array)];
- let dir = `./hls/${id}`;
- if (!fs.existsSync(dir)){
- fs.mkdirSync(dir, { recursive: true });
- }
- var concat = "";
- for(var i=0;i<file.length;i++) {
- await functions.sleep(1000);
- let tried = 0, check = false;
- let url = await axios.get(`${schema}://${domain}/getChunkLink?chunkFile=${md5}-chunk-${file[i]}.txt&t=${functions.timenow()}&mid=${md5}`,{
- headers:{
- "referer":`${schema}://${domain}/public/dist/index.html?id=${id}`
- }
- }).then((api) => {
- return api.data;
- });
- let filename = `${dir}/${i}.mp4`;
- console.log(url);
- url = `https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?s=e23055c9ba7686b8fe583fb8318a1f88&container=focus&refresh=31536000&url=${encodeURIComponent(url)}`;
- if(os.type().includes("Windows")) var cmd = `wget.exe "${url}" -O "${filename}"`;
- else var cmd = `wget "${url}" -O "${filename}"`;
- console.log(`Bắt đầu download ${filename}`);
- console.log(cmd);
- shell.exec(cmd);
- while(functions.get_file_size(filename) < 100000 && tried < 3) {
- tried++;
- console.error(`Download ${filename} lỗi, đang thử lại lần ${tried}`);
- // fs.appendFileSync("./error.txt",url);
- await functions.sleep(1000);
- shell.exec(cmd);
- }
- if(functions.get_file_size(filename) > 100000) check = true;
- if(check) concat += `file '${i}.mp4'\r\n`;
- }
- fs.writeFileSync(`${dir}/file.txt`, concat, { flag: 'w' });
- if (fs.existsSync(path)){
- fs.unlinkSync(path);
- }
- if(os.type().includes("Windows")) var cmd = `ffmpeg.exe -f concat -safe 0 -i "${dir}/file.txt" -codec copy "${path}"`;
- else var cmd = `ffmpeg -f concat -safe 0 -i "${slug}/file.txt" -codec copy "${path}"`;
- // console.log(cmd);
- shell.exec(cmd);
- await folder.delete_folder(dir);
- console.log(`Download thành công ${path}`);
- resolve({path,quality});
- } else {
- console.error("ko lấy đc quality");
- reject();
- }
- }).catch((err) => {
- console.error(err);
- reject();
- })
- } else {
- reject();
- }
- })
- }
- module.exports = download_hls;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement