feat(route): 新增路由当当开放平台 (#15878)

* feat(route): 新增路由当当开放平台

* feat(route): 新增路由当当开放平台
This commit is contained in:
chziyun 2024-06-12 11:16:37 +08:00 committed by GitHub
parent d80cd56006
commit 3f579f4aae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '当当开放平台',
url: 'open.dangdang.com',
};

View File

@ -0,0 +1,69 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
const typeMap = {
0: '全部',
1: '其他',
2: '规则变更',
};
/**
*
* @param ctx {import('koa').Context}
*/
export const route: Route = {
path: '/notice/:type?',
categories: ['programming'],
example: '/dangdang/notice/1',
parameters: { type: '公告分类,默认为全部' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '公告',
maintainers: ['zhijunchai'],
handler,
description: `| 类型 | type |
| -------- | ---- |
| | 0 |
| | 1 |
| | 2 |`,
};
async function handler(ctx) {
const type = ctx.req.param('type');
const url = `https://open.dangdang.com/op-api/developer-platform/document/menu/list?categoryId=3&type=${type > 0 ? typeMap[type] : ''}`;
const response = await got({ method: 'get', url });
const list = response.data.data.documentMenu.map((item) => ({
title: item.title,
description: item.type,
documentId: item.documentId,
source: `https://open.dangdang.com/op-api/developer-platform/document/info/get?document_id=${item.documentId}`,
link: `https://open.dangdang.com/home/notice/message/1/${item.documentId}`,
pubDate: timezone(parseDate(item.modifyTime), +8),
}));
const result = await Promise.all(
list.map((item) =>
cache.tryGet(item.source, async () => {
const itemResponse = await got(item.source);
item.description = itemResponse.data.data.documentContentList[0].content;
return item;
})
)
);
return {
title: `当当开放平台 - ${typeMap[type] || typeMap[0]}`,
link: `https://open.dangdang.com/home/notice/message/1`,
item: result,
};
}