Compare commits

...

2 Commits

Author SHA1 Message Date
9d8653e0ac buffer 2024-03-29 01:03:17 -07:00
0b5199241e handle 429 and error 2024-03-29 00:18:52 -07:00

View File

@ -14,13 +14,29 @@ try {
}
async function get(url) {
var res;
await (async function tryRequest() {
console.log("get", url);
var res = await fetch(url, {
try {
res = await fetch(url, {
headers: {
Cookie,
Referer: "https://www.pixiv.net/"
}
});
} catch(error) {
console.error(error.stack);
await sleep(1);
await tryRequest();
return;
}
if (res.status == 429) {
console.warn(res.status, res.statusText);
await sleep(10);
await tryRequest();
}
})();
if (!res.ok) {
throw new Error(`${res.url} ${res.status} ${res.statusText} ${await res.text()}`);
}
@ -37,9 +53,14 @@ async function archiveArtwork(id, index) {
for (let i = 0; i < illust.pageCount; i++) {
let url = illust.urls.original.replace('p0', 'p'+i);
let filename = url.split('/').pop();
let writeStream = fs.createWriteStream(`collection/${prefix}_${filename}`, {flags: "wx"});
try {
let res = await get(url);
Readable.fromWeb(res.body).pipe(writeStream);
let buffer = Buffer.from(await res.arrayBuffer());
fs.writeFileSync(`collection/${prefix}_${filename}`, buffer, {flag: "wx"});
} catch(error) {
console.error(error.stack);
fs.writeFileSync(`collection/${prefix}_${filename}_error.txt`, error.stack, {flag: "wx"});
}
}
} catch (error) {
fs.writeFileSync(`collection/${prefix}_${id}_error.txt`, error.stack, {flag: "wx"});
@ -71,6 +92,11 @@ async function archiveArtwork(id, index) {
lastartwork.id = id;
lastartwork.index++;
fs.writeFileSync("lastartwork.json", JSON.stringify(lastartwork));
await sleep(1);
}
})();
async function sleep(seconds) {
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
}