scraping backwards

This commit is contained in:
Nathan Rashleigh committed 2024-04-12 16:56:27 +10:00
1 parent 07cfa6df06
commit 29a3bcce39
7 files changed
+234 -177

No files matched your search

+57 -16
View File
@@ -1,4 +1,6 @@
import type { PrismaClient, Attachment, Meme } from '@prisma/client';
import type { Meilisearch } from 'meilisearch';
import type { Logger } from 'pino';
import * as attachments from './attachments'
@@ -10,23 +12,62 @@ function memeUrl(a: Attachment) {
return u.href
}
export function fromAttachment(a: Attachment) {
const text = a.vision.fullTextAnnotation?.text || ''
const tags = [
a.guild.name,
a.channel.name,
a.message.author.username,
]
export default async function memeService(baseLog: Logger, prisma: PrismaClient, meili: Meilisearch) {
const log = baseLog.child({ service: 'meme' })
const m: Meme = {
id: a.memeId,
url: memeUrl(a),
text,
tags: tags,
attachmentId: a.id,
createdAt: undefined,
updatedAt: undefined
function fromAttachment(a: Attachment) {
const text = a.vision.fullTextAnnotation?.text || ''
const tags = [
a.guild.name,
a.channel.name,
a.message.author.username,
]
const m: Meme = {
id: a.memeId,
url: memeUrl(a),
text,
tags: tags,
attachmentId: a.id,
createdAt: undefined,
updatedAt: undefined
}
return m
}
return m
async function ensureMeiliIndex() {
const delTask = await meili.deleteIndex('memes')
const delResult = await meili.waitForTask(delTask.taskUid)
const task = await meili.createIndex('memes', { primaryKey: 'id' })
const result = await meili.waitForTask(task.taskUid)
log.debug(result);
return meili.index('memes')
}
async function updateMeili() {
await ensureMeiliIndex();
const index = meili.index('memes')
const memes = await prisma.meme.findMany();
const task = await index.updateDocuments(memes)
const res = await meili.waitForTask(task.taskUid)
log.debug(res)
}
async function persist(m: Meme) {
const index = meili.index('memes')
return index.updateDocuments([m])
}
await ensureMeiliIndex();
await updateMeili();
return {
ensureMeiliIndex,
updateMeili,
fromAttachment,
persist,
}
}
export type MemeService = Awaited<ReturnType<typeof memeService>>