Skip to content

Commit

Permalink
feat(database): insert moe resources and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jul 25, 2024
1 parent 3d317d3 commit a52786c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/animegarden/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type ResourceWithId<T extends FetchResourcesOptions> = Resource<T> & {
id: number;
};

export type FetchedResource = Omit<Resource, 'fetchedAt'>;
export type FetchedResource = Omit<Resource<{ tracker: true }>, 'fetchedAt'>;

export interface ResourceDetail {
provider: string;
Expand Down
1 change: 1 addition & 0 deletions packages/database/src/operations/dmhy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
57 changes: 52 additions & 5 deletions packages/database/src/operations/moe.ts
Original file line number Diff line number Diff line change
@@ -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})`
};
}
2 changes: 2 additions & 0 deletions packages/database/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface ResourceDocument {

magnet: Resource['magnet'];

tracker: Resource['tracker'];

createdAt: number;

fetchedAt: number;
Expand Down

0 comments on commit a52786c

Please sign in to comment.