From 163d8119c109c42e64ab37b01dec131f2cb5bf61 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 19:25:03 +0200 Subject: Code improvements, support a manual post queue --- src/services/dedupe.ts | 65 --------------------------------- src/services/jobs.ts | 22 ++++++------ src/services/postDatabase.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 76 deletions(-) delete mode 100644 src/services/dedupe.ts create mode 100644 src/services/postDatabase.ts (limited to 'src/services') diff --git a/src/services/dedupe.ts b/src/services/dedupe.ts deleted file mode 100644 index 2eeb5ee..0000000 --- a/src/services/dedupe.ts +++ /dev/null @@ -1,65 +0,0 @@ -import fs from "fs/promises"; -import path from "path"; -import * as f from "fp-ts"; -import * as t from "io-ts"; - -export const E926DedupeEntryC = t.type({ - provider: t.literal("e926"), - id: t.number, -}); - -export const DedupeEntryC = E926DedupeEntryC; - -export type E926DedupeEntry = t.TypeOf; - -export type DedupeEntry = t.TypeOf; - -export class Dedupe { - private entries: DedupeEntry[] = []; - - private readonly filePath: string; - - private isLoaded = false; - - constructor(private max: number, filename: string) { - this.filePath = path.join(process.cwd(), filename); - } - - private async load() { - if (this.isLoaded) { - return; - } - - try { - await fs.stat(this.filePath); - } catch { - await this.save(); - } - - const fileContent = await fs.readFile(this.filePath, "utf8"); - const entries = t.array(DedupeEntryC).decode(fileContent); - - if (f.either.isRight(entries)) { - this.entries = entries.right; - } - } - - private async save() { - await fs.writeFile(this.filePath, JSON.stringify(this.entries ?? []), "utf8"); - } - - async check(entry: DedupeEntry) { - await this.load(); - - const has = !!this.entries.find((e) => e.provider === entry.provider && e.id === entry.id); - - if (!has) { - this.entries.push(entry); - await this.save(); - } - - return has; - } -} - -export default new Dedupe(50, "dedupe.json"); diff --git a/src/services/jobs.ts b/src/services/jobs.ts index cf3b894..354c66d 100644 --- a/src/services/jobs.ts +++ b/src/services/jobs.ts @@ -1,8 +1,18 @@ -import * as e621 from "../api/e621"; +import * as e621 from "../api/e926"; import * as mastodon from "../api/mastodon"; import config from "../config"; import Sharp from "sharp"; +export async function postSpecificPicture(id: number) { + console.log(`Fetching post ${id}...`); + + const post = await e621.getPostById(id); + + console.log(`Got ${post.id}`); + + await handlePost(post); +} + export async function postRandomPicture() { console.log("Fetching random post..."); @@ -15,16 +25,6 @@ export async function postRandomPicture() { await handlePost(post); } -export async function postSpecificPicture(id: number) { - console.log(`Fetching post ${id}...`); - - const post = await e621.getPostById(id); - - console.log(`Got ${post.id}`); - - await handlePost(post); -} - async function handlePost(post: e621.Post) { const source = post.sources.length ? post.sources[0] : undefined; const cws = config.cw.filter((w) => post.tags.general.includes(w)); diff --git a/src/services/postDatabase.ts b/src/services/postDatabase.ts new file mode 100644 index 0000000..e3be7bb --- /dev/null +++ b/src/services/postDatabase.ts @@ -0,0 +1,86 @@ +import fs from "fs/promises"; +import path from "path"; +import * as f from "fp-ts"; +import * as t from "io-ts"; + +export const PostDatabaseEntryC = t.type({ + provider: t.literal("e926"), + id: t.number, +}); + +export type PostDatabaseEntry = t.TypeOf; + +export class PostDatabase { + private entries: PostDatabaseEntry[] = []; + + private readonly filePath: string; + + private isLoaded = false; + + constructor(filename: string, private max?: number) { + this.filePath = path.join(process.cwd(), filename); + } + + private async load() { + if (this.isLoaded) { + return; + } + + try { + await fs.stat(this.filePath); + } catch { + await this.save(); + } + + const fileContent = await fs.readFile(this.filePath, "utf8"); + const entries = t.array(PostDatabaseEntryC).decode(fileContent); + + if (f.either.isRight(entries)) { + this.entries = this.max ? entries.right.slice(0, -1 * this.max) : entries.right; + } + } + + private async save() { + await fs.writeFile(this.filePath, JSON.stringify(this.entries), "utf8"); + } + + async insertIfNotExists(entry: PostDatabaseEntry): Promise { + await this.load(); + + const has = !!this.entries.find((e) => e.provider === entry.provider && e.id === entry.id); + + if (!has) { + this.entries.push(entry); + await this.save(); + } + + return has; + } + + async takeFirst(): Promise { + await this.load(); + const entry = this.entries.shift(); + await this.save(); + return entry; + } + + async remove(entry: PostDatabaseEntry): Promise { + await this.load(); + const i = this.entries.findIndex((e) => e.provider === entry.provider && e.id === entry.id); + + if (i === -1) { + return false; + } + + this.entries.splice(i, 1); + await this.save(); + return true; + } + + async getCount() { + await this.load(); + return this.entries.length; + } +} + +export default PostDatabase; -- cgit v1.2.3-70-g09d2