This commit is contained in:
Nathan Rashleigh committed 2024-04-10 17:22:43 +10:00
1 parent 396b288ea8
commit 763b225fc2
8 files changed
+561 -96

No files matched your search

+27 -4
View File
@@ -2,6 +2,8 @@ import "dotenv/config"
import fs from 'fs/promises'
import pino from 'pino'
import * as Minio from 'minio'
import Vision from '@google-cloud/vision'
import { PrismaClient } from '@prisma/client'
@@ -19,6 +21,8 @@ BigInt.prototype.toJSON = function bigIntStringify() {
const VISION_CREDENTIALS = (await import(process.env.GOOGLE_APPLICATION_CREDENTIALS, { assert: { type: "json" } })).default
const STORAGE_BUCKET = process.env.STORAGE_BUCKET
const log = pino({ base: {} });
const vision = new Vision.ImageAnnotatorClient({ credentials: VISION_CREDENTIALS })
const prisma = new PrismaClient()
const minio = new Minio.Client({
@@ -26,13 +30,13 @@ const minio = new Minio.Client({
accessKey: process.env.STORAGE_ACCESS_KEY,
secretKey: process.env.STORAGE_SECRET_KEY,
})
const client = new MeiliSearch({
const meili = new MeiliSearch({
host: process.env.MEILI_URL,
apiKey: process.env.MEILI_MASTER_KEY,
})
const attachments = attachmentService(minio, prisma)
const scraper = discordScraper(process.env.DISCORD_AUTH, vision, minio)
const attachments = attachmentService(log, minio, prisma)
const scraper = discordScraper(process.env.DISCORD_AUTH, vision, minio, prisma, log)
async function ensureStorageBucketExists() {
const exists = await minio.bucketExists(STORAGE_BUCKET)
@@ -40,18 +44,37 @@ async function ensureStorageBucketExists() {
await minio.makeBucket(STORAGE_BUCKET)
}
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.info(result);
}
async function processChannels(channelIds) {
await ensureStorageBucketExists()
const msgs = await Promise.all(channelIds.map(c => scraper.getChannelAttachments(c)))
const results = await Promise.allSettled(msgs.flat().map(a => attachments.process(a)))
console.log(results);
log.info(results.map(r => r.status));
return results
}
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.info(res)
}
const CHANNEL_IDS = [
367936426962190336n, // TPM shitposts
1095060717117374687n, // aella memes
]
await processChannels(CHANNEL_IDS)
await updateMeili()