-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(route): add okx Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): use data from ssr Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): remove try-catch to avoid poisoning cache Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): handler typing Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): do not raise error when items' empty Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): fixup previous commit Signed-off-by: lxl66566 <[email protected]> * fix(route, okx): another fixup: allowEmpty Signed-off-by: lxl66566 <[email protected]> --------- Signed-off-by: lxl66566 <[email protected]>
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { DataItem, Route } from '@/types'; | ||
import cache from '@/utils/cache'; | ||
import { Context } from 'hono'; | ||
import ofetch from '@/utils/ofetch'; | ||
import { load } from 'cheerio'; | ||
import got from '@/utils/got'; | ||
|
||
export const route: Route = { | ||
path: '/:section?', | ||
categories: ['finance'], | ||
example: '/okx/new-listings', | ||
parameters: { | ||
section: { | ||
description: '公告版块', | ||
default: 'latest-announcements', | ||
options: [ | ||
{ | ||
value: 'latest-announcements', | ||
label: '最新公告', | ||
}, | ||
{ | ||
value: 'new-listings', | ||
label: '新币种上线', | ||
}, | ||
{ | ||
value: 'delistings', | ||
label: '币对下线', | ||
}, | ||
{ | ||
value: 'trading-updates', | ||
label: '交易规则更新', | ||
}, | ||
{ | ||
value: 'deposit-withdrawal-suspension-resumption', | ||
label: '充提暂停/恢复公告', | ||
}, | ||
{ | ||
value: 'p2p-trading', | ||
label: 'C2C 公告', | ||
}, | ||
{ | ||
value: 'web3', | ||
label: 'Web3', | ||
}, | ||
{ | ||
value: 'earn', | ||
label: '赚币', | ||
}, | ||
{ | ||
value: 'jumpstart', | ||
label: 'Jumpstart', | ||
}, | ||
{ | ||
value: 'api', | ||
label: 'API公告', | ||
}, | ||
{ | ||
value: 'okb-buy-back-burn', | ||
label: 'OKB销毁', | ||
}, | ||
{ | ||
value: 'others', | ||
label: '其他', | ||
}, | ||
], | ||
}, | ||
}, | ||
features: { | ||
requireConfig: false, | ||
requirePuppeteer: false, | ||
antiCrawler: false, | ||
supportBT: false, | ||
supportPodcast: false, | ||
supportScihub: false, | ||
}, | ||
radar: [ | ||
{ | ||
source: ['www.okx.com/zh-hans/help/section/:section'], | ||
target: '/:section', | ||
}, | ||
], | ||
name: '公告', | ||
maintainers: ['lxl66566'], | ||
handler, | ||
}; | ||
|
||
async function handler(ctx: Context) { | ||
const baseUrl = 'https://www.okx.com'; | ||
let { section = 'latest-announcements' } = ctx.req.param(); | ||
section = section.replace(/^announcements-/, ''); | ||
|
||
const data = await ofetch(`${baseUrl}/zh-hans/help/section/announcements-${section}`); | ||
const $ = load(data); | ||
|
||
const ssrData = JSON.parse($('script[data-id="__app_data_for_ssr__"]').text()); | ||
const itemsTemp: { title: string; link: string; pubDate: Date }[] = | ||
ssrData?.appContext?.initialProps?.sectionData?.articleList?.items?.map((item: { title: string; slug: string; publishTime: string }) => ({ | ||
title: item.title, | ||
link: `${baseUrl}/zh-hans/help/${item.slug}`, | ||
pubDate: new Date(item.publishTime), | ||
})) || []; | ||
|
||
const items = await Promise.all( | ||
itemsTemp.map((item) => | ||
cache.tryGet(item.link, async () => { | ||
const content = await got(item.link).then((response) => { | ||
const $ = load(response.data); | ||
return $('div[class^="index_richTextContent"]').html(); | ||
}); | ||
|
||
return { | ||
...item, | ||
description: content || '内容获取失败', | ||
}; | ||
}) | ||
) | ||
); | ||
|
||
return { | ||
title: ssrData?.appContext?.serverSideProps?.sectionOutline?.title || 'Unknown', | ||
link: `${baseUrl}/zh-hans/help/section/announcements-${section}`, | ||
item: items as DataItem[], | ||
allowEmpty: true, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: '欧易 OKX', | ||
url: 'www.okx.com/zh-hans', | ||
lang: 'zh-CN', | ||
}; |