From a52786c2ea46b326b284012c8282bb90cbd0a1e0 Mon Sep 17 00:00:00 2001 From: XLor Date: Fri, 26 Jul 2024 03:40:09 +0800 Subject: [PATCH] feat(database): insert moe resources and docs --- packages/animegarden/src/types.ts | 2 +- packages/database/src/operations/dmhy.ts | 1 + packages/database/src/operations/moe.ts | 57 +++++++++++++++++++++--- packages/database/src/schema/index.ts | 2 + 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/packages/animegarden/src/types.ts b/packages/animegarden/src/types.ts index 7a1c5d83..5c1a3ef1 100644 --- a/packages/animegarden/src/types.ts +++ b/packages/animegarden/src/types.ts @@ -69,7 +69,7 @@ export type ResourceWithId = Resource & { id: number; }; -export type FetchedResource = Omit; +export type FetchedResource = Omit, 'fetchedAt'>; export interface ResourceDetail { provider: string; diff --git a/packages/database/src/operations/dmhy.ts b/packages/database/src/operations/dmhy.ts index 2f2a13aa..5741cdc7 100644 --- a/packages/database/src/operations/dmhy.ts +++ b/packages/database/src/operations/dmhy.ts @@ -177,6 +177,7 @@ function transformResource(resource: FetchedResource, now: Date) { type: resource.type, size: resource.size, magnet: resource.magnet, + tracker: resource.tracker, // Convert to UTC+8 createdAt: toShanghai(new Date(resource.createdAt)), fetchedAt: toShanghai(new Date(now)), diff --git a/packages/database/src/operations/moe.ts b/packages/database/src/operations/moe.ts index 271315d2..977c335c 100644 --- a/packages/database/src/operations/moe.ts +++ b/packages/database/src/operations/moe.ts @@ -1,20 +1,67 @@ import MeiliSearch from 'meilisearch'; import { parse } from 'anitomy'; -import { and, desc, eq, gt, inArray, lt } from 'drizzle-orm'; +import { and, desc, eq, gt, inArray, lt, sql } from 'drizzle-orm'; import { normalizeTitle, type FetchedResource } from 'animegarden'; +import type { Resource } from '../schema'; import type { Database } from '../connection'; -import type { NewResource, Resource } from '../schema'; -import { resources } from '../schema/resource'; +import { toShanghai } from '../utils'; import { insertResourceDocuments } from '../meilisearch'; +import { resources } from '../schema/resource'; + export async function insertMoeResources( database: Database, meili: MeiliSearch, fetchedResources: FetchedResource[] ) { - // TODO - return []; + const now = new Date(); + const res = fetchedResources.map((r) => transformResource(r, now)); + + const data = await database.insert(resources).values(res).onConflictDoNothing().returning({ + id: resources.id, + providerId: resources.providerId, + isDuplicated: resources.isDuplicated + }); + + const map = new Map(res.map((r) => [r.providerId, r] as const)); + const docs = data + .map((r) => { + const item = map.get(r.providerId); + if (item) { + // Manually add default fields + return { id: r.id, isDeleted: false, ...item, isDuplicated: r.isDuplicated }; + } + }) + .filter(Boolean) as Resource[]; + if (docs.length > 0) { + await insertResourceDocuments(meili, docs); + } + + return data; +} + +function transformResource(resource: FetchedResource, now: Date) { + const titleAlt = normalizeTitle(resource.title); + + return { + provider: 'moe' as const, + providerId: resource.providerId, + href: resource.href, + title: resource.title, + titleAlt, + type: resource.type, + size: resource.size, + magnet: resource.magnet, + tracker: resource.tracker, + // Convert to UTC+8 + createdAt: toShanghai(new Date(resource.createdAt)), + fetchedAt: toShanghai(new Date(now)), + anitomy: resource.type === '動畫' ? JSON.stringify(parse(resource.title)) : undefined, + fansubId: resource.fansub?.id ? resource.fansub?.id : undefined, + publisherId: resource.publisher.id, + isDuplicated: sql`EXISTS (SELECT 1 FROM ${resources} WHERE ${resources.magnet} = ${resource.magnet})` + }; } diff --git a/packages/database/src/schema/index.ts b/packages/database/src/schema/index.ts index 4cad1c97..26cbbdf3 100644 --- a/packages/database/src/schema/index.ts +++ b/packages/database/src/schema/index.ts @@ -33,6 +33,8 @@ export interface ResourceDocument { magnet: Resource['magnet']; + tracker: Resource['tracker']; + createdAt: number; fetchedAt: number;