Compare commits

..

2 Commits

Author SHA1 Message Date
c8a91e54e2 fix loosing posts if error with pleroma
add time
2024-09-08 00:55:32 -07:00
557f59e2f7 fetch retry 2024-09-08 00:25:55 -07:00
6 changed files with 50 additions and 36 deletions

2
.gitignore vendored
View File

@ -2,5 +2,5 @@ credentials.json
known_ids known_ids
logindata logindata
._* ._*
output.txt *.txt
node_modules node_modules

9
Bot.js
View File

@ -18,11 +18,12 @@ export default class Bot {
form.append("grant_type", "password"); form.append("grant_type", "password");
form.append("username", this.username); form.append("username", this.username);
form.append("password", this.password); form.append("password", this.password);
var data = await fetch(this.url + "/oauth/token", { var res = await fetch(this.url + "/oauth/token", {
method: "POST", method: "POST",
body: form body: form
}).then(res => res.json()); });
this.loginData = data; if (!res.ok) throw new Error("HTTP "+ res.status);
this.loginData = await res.json();
writeFileSync(`logindata/${this.username}.json`, JSON.stringify(this.loginData)); writeFileSync(`logindata/${this.username}.json`, JSON.stringify(this.loginData));
} }
@ -46,6 +47,7 @@ export default class Bot {
"Authorization": `${this.loginData.token_type} ${this.loginData.access_token}` "Authorization": `${this.loginData.token_type} ${this.loginData.access_token}`
} }
}); });
if (!res.ok) throw new Error("HTTP "+ res.status);
var json = await res.json(); var json = await res.json();
console.log("posted", res.status, json.uri || json); console.log("posted", res.status, json.uri || json);
return json; return json;
@ -61,6 +63,7 @@ export default class Bot {
"Authorization": `${this.loginData.token_type} ${this.loginData.access_token}` "Authorization": `${this.loginData.token_type} ${this.loginData.access_token}`
} }
}); });
if (!res.ok) throw new Error("HTTP "+ res.status);
var json = await res.json(); var json = await res.json();
console.log("uploaded file", res.status, json.url); console.log("uploaded file", res.status, json.url);
return json; return json;

View File

@ -1,9 +1,12 @@
import {unescape} from 'html-escaper'; import {unescape} from 'html-escaper';
import fetchRetry from "fetch-retry";
import { appendFileSync } from "fs";
import credentials from "./credentials.json" assert { type: 'json' }; import credentials from "./credentials.json" assert { type: 'json' };
import Bot from "./Bot.js"; import Bot from "./Bot.js";
import { getPostDataById, getNewPixivPosts } from "./pixiv.js"; import { getPostDataById, getNewPixivPosts } from "./pixiv.js";
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
global.fetch = fetchRetry(global.fetch);
var mikubot = new Bot({ var mikubot = new Bot({
url: "https://mikuobsession.net", url: "https://mikuobsession.net",
@ -26,17 +29,15 @@ await mikubot_nsfw.login();
var newPosts = await getNewPixivPosts("初音ミク"); var newPosts = await getNewPixivPosts("初音ミク");
posts: for (let post of newPosts) { for (let post of newPosts) {
let bot = post.xRestrict ? mikubot_nsfw : mikubot; let bot = post.xRestrict ? mikubot_nsfw : mikubot;
let url = `https://www.pixiv.net/en/artworks/${post.id}`; let url = `https://www.pixiv.net/en/artworks/${post.id}`;
let attempts = 0, error;
do {
attempts++;
if (attempts > 1) console.log("attempt", attempts);
try { try {
let {images, illust} = await getPostDataById(post.id); let {images, illust} = await getPostDataById(post.id);
let date = new Date(illust.createDate).toLocaleString("en-US", {timeZone: "JST", month: "long", day: "numeric", year: "numeric"}); let date = new Date(illust.createDate);
let status = `<b>${illust.title}</b> / ${illust.userName} / ${date}<br>${unescape(illust.description)}<br>${illust.aiType == 2 ? `#AIgenerated ` : ''}${post.tags.map(tag => `#${tag}`).join(" ")}<br>${url}<br>https://www.pixiv.net/en/users/${illust.userId}`; let dateString = date.toLocaleDateString("en-US", {timeZone: "JST", month: "long", day: "numeric", year: "numeric"});
let timeString = date.toLocaleTimeString("en-US", {timeZone: "JST", hour12: true, hour: "numeric", "minute": "numeric"}) + " JST";
let status = `<b>${illust.title}</b> / ${illust.userName} / ${dateString} ${timeString}<br>${unescape(illust.description)}<br>${illust.aiType == 2 ? `#AIgenerated ` : ''}${post.tags.map(tag => `#${tag}`).join(" ")}<br>${url}<br>https://www.pixiv.net/en/users/${illust.userId}`;
if (images.length > 4) { if (images.length > 4) {
status += `<br>⚠ There are ${images.length} images.`; status += `<br>⚠ There are ${images.length} images.`;
} }
@ -47,14 +48,12 @@ posts: for (let post of newPosts) {
sensitive: Boolean(post.xRestrict), sensitive: Boolean(post.xRestrict),
visibility: "public" visibility: "public"
}); });
continue posts; appendFileSync(`known_ids/初音ミク.txt`, "\n" + post.id);
} catch(e) { } catch(error) {
console.error(e.stack); console.error(error.stack);
error = e;
}
} while (attempts < 3)
await bot.post({ await bot.post({
status: `${url}\n#error\n${error.stack}`, status: `${url}\n#error\n${error.stack}`,
visibility: "public" visibility: "public"
}); });
} }
}

11
package-lock.json generated
View File

@ -5,9 +5,15 @@
"packages": { "packages": {
"": { "": {
"dependencies": { "dependencies": {
"fetch-retry": "^6.0.0",
"html-escaper": "^3.0.3" "html-escaper": "^3.0.3"
} }
}, },
"node_modules/fetch-retry": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz",
"integrity": "sha512-BUFj1aMubgib37I3v4q78fYo63Po7t4HUPTpQ6/QE6yK6cIQrP+W43FYToeTEyg5m2Y7eFUtijUuAv/PDlWuag=="
},
"node_modules/html-escaper": { "node_modules/html-escaper": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",
@ -15,6 +21,11 @@
} }
}, },
"dependencies": { "dependencies": {
"fetch-retry": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz",
"integrity": "sha512-BUFj1aMubgib37I3v4q78fYo63Po7t4HUPTpQ6/QE6yK6cIQrP+W43FYToeTEyg5m2Y7eFUtijUuAv/PDlWuag=="
},
"html-escaper": { "html-escaper": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",

View File

@ -1,6 +1,7 @@
{ {
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"fetch-retry": "^6.0.0",
"html-escaper": "^3.0.3" "html-escaper": "^3.0.3"
} }
} }

View File

@ -21,8 +21,7 @@ export async function getNewPixivPosts(tag = "初音ミク") {
} }
}).then(res => res.json()); }).then(res => res.json());
if (data.error) { if (data.error) {
console.error(data); throw new Error(JSON.stringify(data));
break;
} }
for (let post of data.body.illustManga.data) { for (let post of data.body.illustManga.data) {
@ -36,7 +35,8 @@ export async function getNewPixivPosts(tag = "初音ミク") {
await sleep(1); await sleep(1);
} }
return { return newPosts.reverse();
/*return {
[Symbol.iterator]() { [Symbol.iterator]() {
return this; return this;
}, },
@ -47,7 +47,7 @@ export async function getNewPixivPosts(tag = "初音ミク") {
} }
return {value, done: !Boolean(value)} return {value, done: !Boolean(value)}
} }
} }*/
} }
async function sleep(seconds) { async function sleep(seconds) {