Skip to content

Commit

Permalink
feat(app): list resources
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 6, 2023
1 parent 6b16f5b commit ee65673
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/app/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { WORKER_BASE } from './constant';

const baseURL = 'https://' + (import.meta.env.SSR ? WORKER_BASE : 'garden.onekuma.cn/api/');

const ofetch = async (url: string | RequestInfo, init?: RequestInit) => {
export const ofetch = async (url: string | RequestInfo, init?: RequestInit) => {
if (import.meta.env.DEV && import.meta.env.HTTPS_PROXY) {
const { ProxyAgent } = await import('undici');
return fetch(url, {
Expand Down
62 changes: 62 additions & 0 deletions packages/app/src/pages/feed.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { APIRoute } from 'astro';

import rss from '@astrojs/rss';
import { toDate } from 'date-fns-tz';
import { fetchResources } from 'animegarden';

import { ofetch } from '../fetch';
import { WORKER_BASE } from '../constant';

export const get: APIRoute = async (context) => {
const url = new URL(context.request.url);
url.protocol = 'https:';
url.host = WORKER_BASE;
url.port = '';
url.pathname = '/resources/search';
const request = new Request(url, context.request);

const { resources } = await (ofetch(request).then((r) => r.json()) as ReturnType<
typeof fetchResources
>);

return rss({
title: 'Anime Garden - 動漫花園資源網 第三方镜像站',
description:
'Anime Garden 是動漫花園資源網的第三方镜像站, 動漫花園資訊網是一個動漫愛好者交流的平台,提供最及時,最全面的動畫,漫畫,動漫音樂,動漫下載,BT,ED,動漫遊戲,資訊,分享,交流,讨论.',
site: context.site!.origin,
items: resources.map((r) => {
return {
title: r.title,
pubDate: toDate(r.createdAt, { timeZone: 'Asia/Shanghai' }),
link: toGardenURL(context.site!.origin, r.href),
enclosure: {
url: r.magnet,
length: r.size ? formatSize(r.size) : 1,
type: 'application/x-bittorrent'
}
};
})
});
};

function toGardenURL(origin: string, href: string) {
const id = href.split('/').at(-1)!;
return origin + '/resource/' + id;
}

function formatSize(size: string) {
const RES = [
[/^([0-9\.]+)B$/, 1],
[/^([0-9\.]+)KB$/, 1024],
[/^([0-9\.]+)MB$/, 1024 * 1024],
[/^([0-9\.]+)GB$/, 1024 * 1024 * 1024],
[/^([0-9\.]+)TB$/, 1024 * 1024 * 1024 * 1024]
] as const;
for (const [RE, base] of RES) {
const match = RE.exec(size);
if (match) {
return Math.round(+match[1] * base);
}
}
return 0;
}

0 comments on commit ee65673

Please sign in to comment.