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
logindata
._*
output.txt
*.txt
node_modules

9
Bot.js
View File

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

View File

@ -1,9 +1,12 @@
import {unescape} from 'html-escaper';
import fetchRetry from "fetch-retry";
import { appendFileSync } from "fs";
import credentials from "./credentials.json" assert { type: 'json' };
import Bot from "./Bot.js";
import { getPostDataById, getNewPixivPosts } from "./pixiv.js";
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
global.fetch = fetchRetry(global.fetch);
var mikubot = new Bot({
url: "https://mikuobsession.net",
@ -26,35 +29,31 @@ await mikubot_nsfw.login();
var newPosts = await getNewPixivPosts("初音ミク");
posts: for (let post of newPosts) {
for (let post of newPosts) {
let bot = post.xRestrict ? mikubot_nsfw : mikubot;
let url = `https://www.pixiv.net/en/artworks/${post.id}`;
let attempts = 0, error;
do {
attempts++;
if (attempts > 1) console.log("attempt", attempts);
try {
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 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}`;
if (images.length > 4) {
status += `<br>⚠ There are ${images.length} images.`;
}
await bot.post({
status,
content_type: "text/html",
files: images,
sensitive: Boolean(post.xRestrict),
visibility: "public"
});
continue posts;
} catch(e) {
console.error(e.stack);
error = e;
try {
let {images, illust} = await getPostDataById(post.id);
let date = new Date(illust.createDate);
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) {
status += `<br>⚠ There are ${images.length} images.`;
}
} while (attempts < 3)
await bot.post({
status: `${url}\n#error\n${error.stack}`,
visibility: "public"
});
await bot.post({
status,
content_type: "text/html",
files: images,
sensitive: Boolean(post.xRestrict),
visibility: "public"
});
appendFileSync(`known_ids/初音ミク.txt`, "\n" + post.id);
} catch(error) {
console.error(error.stack);
await bot.post({
status: `${url}\n#error\n${error.stack}`,
visibility: "public"
});
}
}

11
package-lock.json generated
View File

@ -5,9 +5,15 @@
"packages": {
"": {
"dependencies": {
"fetch-retry": "^6.0.0",
"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": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",
@ -15,6 +21,11 @@
}
},
"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": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",

View File

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

View File

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