Compare commits

...

4 Commits

Author SHA1 Message Date
lamp fa33969f61 mkdir 2024-03-29 20:05:10 -07:00
lamp 90b066504b Add readme.md 2024-03-29 21:29:31 -05:00
lamp 6551a50726 download missing images 2024-03-29 19:07:55 -07:00
lamp 22bb7502f9 infer lastartwork from dir 2024-03-29 18:31:45 -07:00
3 changed files with 67 additions and 20 deletions
+1
View File
@@ -2,3 +2,4 @@ collection
config.json config.json
lastartwork.json lastartwork.json
.DS_Store .DS_Store
._*
+46 -20
View File
@@ -1,16 +1,8 @@
var fs = require("fs"); var fs = require("fs");
var {Readable} = require("stream");
if (!fs.existsSync("collection")) fs.mkdirSync("collection");
var {UserID, Cookie} = require("./config.json"); var {UserID, Cookie} = require("./config.json");
try { async function sleep(seconds) {
var lastartwork = require("./lastartwork.json"); await new Promise(resolve => setTimeout(resolve, seconds * 1000));
} catch (error) {
var lastartwork = {
id: undefined,
index: -1
};
} }
async function get(url) { async function get(url) {
@@ -36,7 +28,6 @@ async function get(url) {
await tryRequest(); 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()}`);
} }
@@ -68,9 +59,51 @@ async function archiveArtwork(id, index) {
} }
} }
(async function archiveBookmarks() { (async function main() {
var lastartwork = {
id: undefined,
index: -1
};
try {
var files = fs.readdirSync("collection");
} catch (e) {
if (e.code == "ENOENT") {
fs.mkdirSync("collection");
var files = [];
} else throw e;
}
for (let file of files) {
let match = file.match(/^(\d{5})_(\d+)/);
if (!match) continue;
let prefix = match[1], index = Number(prefix), id = Number(match[2])
// find last saved artwork
if (index > lastartwork.index) {
lastartwork.index = index;
lastartwork.id = id;
}
// fetch missing images
if (file.endsWith(".json")) {
let illust = JSON.parse(fs.readFileSync(`collection/${file}`, "utf8"));
for (let i = 0; i < illust.pageCount; i++) {
let img_url = illust.urls.original.replace('p0', 'p'+i);
let img_filename = img_url.split('/').pop();
let img_local_filename = `${prefix}_${img_filename}`;
if (!files.includes(img_local_filename)) {
console.log(`missing ${img_local_filename}`);
try {
let res = await get(img_url);
let buffer = Buffer.from(await res.arrayBuffer());
fs.writeFileSync(`collection/${img_local_filename}`, buffer, {flag: "wx"});
} catch(error) {
console.error(error.stack);
fs.writeFileSync(`collection/${img_local_filename}_error.txt`, error.stack, {flag: "wx"});
}
}
}
}
}
console.debug("lastartwork", lastartwork);
var newIds = []; var newIds = [];
var offset = 0; var offset = 0;
l1: while (true) { l1: while (true) {
let data = await get(`https://www.pixiv.net/ajax/user/${UserID}/illusts/bookmarks?tag=&offset=${offset}&limit=100&rest=show&lang=en&version=5dc84ab282403a049abea4e2f2214b6a69d31da6`) let data = await get(`https://www.pixiv.net/ajax/user/${UserID}/illusts/bookmarks?tag=&offset=${offset}&limit=100&rest=show&lang=en&version=5dc84ab282403a049abea4e2f2214b6a69d31da6`)
@@ -84,19 +117,12 @@ async function archiveArtwork(id, index) {
if (!artworkIds.length) break; if (!artworkIds.length) break;
offset += artworkIds.length; offset += artworkIds.length;
} }
newIds = newIds.reverse(); newIds = newIds.reverse();
console.log("archiving", newIds.length); console.log("archiving", newIds.length);
for (let id of newIds) { for (let id of newIds) {
await archiveArtwork(id, lastartwork.index + 1); await archiveArtwork(id, lastartwork.index + 1);
lastartwork.id = id; lastartwork.id = id;
lastartwork.index++; lastartwork.index++;
fs.writeFileSync("lastartwork.json", JSON.stringify(lastartwork));
await sleep(1); await sleep(1);
} }
})(); })();
async function sleep(seconds) {
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
+20
View File
@@ -0,0 +1,20 @@
A script to download your Pixiv bookmarks, because some of them disappear!
## How to use
Bypass Cloudflare or it might block the script. Find origin IPs with `nslookup pixiv.net` and add to your local DNS or hosts file for `www.pixiv.net`.
Create `config.json` file:
```json
{
"UserID": "your_pixiv_user_id",
"Cookie": "paste_cookies_here"
}
```
User ID is in your Pixiv profile url: `https://www.pixiv.net/en/users/<user id>`.
Use devtools to get Cookie, select a www.pixiv.net request and copy the entire contents of Cookie header. (todo: not sure which exact cookies are actually needed)
Run Node.js 18+ `node .`. All bookmarked images plus meta json are saved in `collection` folder. Run again whenever you save new bookmarks.