Compare commits
2 Commits
b7642eea4d
...
9d8653e0ac
Author | SHA1 | Date | |
---|---|---|---|
9d8653e0ac | |||
0b5199241e |
44
index.js
44
index.js
@ -14,13 +14,29 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function get(url) {
|
async function get(url) {
|
||||||
console.log("get", url);
|
var res;
|
||||||
var res = await fetch(url, {
|
await (async function tryRequest() {
|
||||||
headers: {
|
console.log("get", url);
|
||||||
Cookie,
|
try {
|
||||||
Referer: "https://www.pixiv.net/"
|
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) {
|
if (!res.ok) {
|
||||||
throw new Error(`${res.url} ${res.status} ${res.statusText} ${await res.text()}`);
|
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++) {
|
for (let i = 0; i < illust.pageCount; i++) {
|
||||||
let url = illust.urls.original.replace('p0', 'p'+i);
|
let url = illust.urls.original.replace('p0', 'p'+i);
|
||||||
let filename = url.split('/').pop();
|
let filename = url.split('/').pop();
|
||||||
let writeStream = fs.createWriteStream(`collection/${prefix}_${filename}`, {flags: "wx"});
|
try {
|
||||||
let res = await get(url);
|
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) {
|
} catch (error) {
|
||||||
fs.writeFileSync(`collection/${prefix}_${id}_error.txt`, error.stack, {flag: "wx"});
|
fs.writeFileSync(`collection/${prefix}_${id}_error.txt`, error.stack, {flag: "wx"});
|
||||||
@ -71,6 +92,11 @@ async function archiveArtwork(id, index) {
|
|||||||
lastartwork.id = id;
|
lastartwork.id = id;
|
||||||
lastartwork.index++;
|
lastartwork.index++;
|
||||||
fs.writeFileSync("lastartwork.json", JSON.stringify(lastartwork));
|
fs.writeFileSync("lastartwork.json", JSON.stringify(lastartwork));
|
||||||
|
await sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
async function sleep(seconds) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user