Skip to content

Commit

Permalink
feat(server): fetch dmhy detail
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 16, 2024
1 parent 42646ae commit 059246f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
64 changes: 64 additions & 0 deletions packages/server/src/query/detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { Context } from 'hono';

import { and, eq } from 'drizzle-orm';
import { prefixStorage } from 'unstorage';

import { resources } from '@animegarden/database';
import { fetchDmhyDetail } from '@animegarden/scraper';

import { logger as rootLogger } from '../logger';
import { storage } from '../storage';
import { database } from '../database';

const logger = rootLogger.forkIntegrationLogger('detail');

const dmhyDetailStorage = prefixStorage(storage, 'dmhy-detail');

export async function getDmhyResourceDetail(ctx: Context) {
const href = ctx.req.param('href');
const id = resolveId(href);
if (id === undefined) {
return ctx.json({ message: '404 NOT FOUND' }, 404);
}

const cache = await dmhyDetailStorage.getItem('' + id);
if (!!cache) {
logger.info(`Resouce detail ${id} hit cache`);
return ctx.json({ id, detail: cache });
}

logger.info(`Resouce detail ${id} cache miss`);

const realHref = /^\d+$/.test(href)
? (
await database
.select({ href: resources.href })
.from(resources)
.where(and(eq(resources.provider, 'dmhy'), eq(resources.providerId, '' + id)))
.execute()
)[0]?.href
: href;

if (!realHref) {
return ctx.json({ message: '404 NOT FOUND' }, 404);
}

logger.info(`Try fetching dmhy detail of ${realHref}`);
const resp = await fetchDmhyDetail(fetch, realHref);
if (!resp) {
return ctx.json({ message: '404 NOT FOUND' }, 404);
}

logger.info(`Set resouce detail ${id} cache`);

// Ignore cache put error
const detail = { ...resp, id };
await dmhyDetailStorage.setItem('' + id, detail, { ttl: 60 * 60 * 24 * 7 }).catch(() => {});

return ctx.json({ id, detail });

function resolveId(href: string) {
const id = href.split('_')[0];
return id && /^\d+$/.test(id) ? +id : undefined;
}
}
17 changes: 11 additions & 6 deletions packages/server/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseSearchURL } from 'animegarden';
import { registerApp } from '../app';

import { queryResources } from './resources';
import { getDmhyResourceDetail } from './detail';

export function registerQuery() {
registerApp((app) => {
Expand All @@ -27,25 +28,29 @@ export function registerQuery() {
app.post(`/resources`, async (ctx) => {
return listResourcesHandler(ctx);
});
app.get(`/detail`, async (req) => {});
app.get(`/resource`, async (req) => {});
app.get(`/detail/:href`, async (ctx) => {});
app.get(`/resource/:href`, async (ctx) => {});

app.get(`/dmhy/resources`, async (ctx) => {
return listResourcesHandler(ctx, 'dmhy');
});
app.post(`/dmhy/resources`, async (ctx) => {
return listResourcesHandler(ctx, 'dmhy');
});
app.get(`/dmhy/detail`, async (req) => {});
app.get(`/dmhy/resource`, async (req) => {});
app.get(`/dmhy/detail/:href`, async (ctx) => {
return getDmhyResourceDetail(ctx);
});
app.get(`/dmhy/resource/:href`, async (ctx) => {
return getDmhyResourceDetail(ctx);
});

app.get(`/moe/resources`, async (ctx) => {
return listResourcesHandler(ctx, 'moe');
});
app.post(`/moe/resources`, async (ctx) => {
return listResourcesHandler(ctx, 'moe');
});
app.get(`/moe/detail`, async (req) => {});
app.get(`/moe/resource`, async (req) => {});
app.get(`/moe/detail/:href`, async (ctx) => {});
app.get(`/moe/resource/:href`, async (ctx) => {});
});
}

0 comments on commit 059246f

Please sign in to comment.