-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveFetch.api
More file actions
43 lines (38 loc) · 1.61 KB
/
saveFetch.api
File metadata and controls
43 lines (38 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?
const fs=require("fs");
const fsp=require("fs/promises");
const fetch=require("node-fetch");
if(input.token!=="z83na2k") throw 403;
if(input.action==="audioSave"){
//EXAMPLE: fetch("https://lff.one/z/saveFetch.api?action=audioSave&token=z83na2k&title="+escape(title)+"&artist="+escape(artist)+"&num="+playlistNum+"&src="+escape(src))
// INLINE JS: {b=document.getElementsByClassName("btn btn-new")[1];src=b.childNodes[0].dataset.url;playlistNum=b.parentElement.parentElement.children[0].children[0].children[0].innerText;title=b.parentElement.parentElement.children[0].children[0].children[1].children[0].innerText;artist=b.parentElement.parentElement.children[0].children[0].children[1].children[1].innerText;}
const downloadPath="/tmp/fetched-audio/";
const data={
url: unescape(input.src),
title: unescape(input.title),
artist: unescape(input.artist),
num: Number(input.num),
folder: input.folder?unescape(input.folder):"default",
};
//await fsp.mkdir(downloadPath);
const folder=downloadPath+data.folder+"/";
await fsp.mkdir(folder,{recursive:true});
const file=folder+String(data.num).padStart(3,"0")+" "+data.title+".mp3";
const res=await fetch(data.url);
if(!res.ok) throw 400;
const stream=fs.createWriteStream(file);
res.body.pipe(stream);
const error=await new Promise(resolve=>{
stream.on("finish",resolve);
stream.on("error",resolve);
});
if(error){
console.log("error: "+data.num+" "+data.title+",",error);
throw 400;
}
else res.write("OK");
await fsp.appendFile(folder+"files.csv",data.num+"; "+data.title+"; "+data.artist+"\n");
log("Saved: "+data.num+" "+data.title);
}
else throw 400;
?>