|
| 1 | +import { config } from '@/config'; |
| 2 | +import type { Route } from '@/types'; |
| 3 | +import { ViewType } from '@/types'; |
| 4 | +import ofetch from '@/utils/ofetch'; |
| 5 | +import { parseDate } from '@/utils/parse-date'; |
| 6 | + |
| 7 | +interface NoticeItem { |
| 8 | + id: number; |
| 9 | + title: string; |
| 10 | + url?: string; |
| 11 | + time: number; |
| 12 | + code?: string; |
| 13 | + detail?: string; |
| 14 | + textOnlyBody?: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface NoticeResponse { |
| 18 | + code: string; |
| 19 | + data: NoticeItem[] | null; |
| 20 | +} |
| 21 | + |
| 22 | +const handler: Route['handler'] = async (ctx) => { |
| 23 | + const baseUrl = 'https://www.binance.com'; |
| 24 | + const listUrl = `${baseUrl}/bapi/apex/v1/public/apex/market/notice/get`; |
| 25 | + const pageUrl = `${baseUrl}/zh-CN/messages/v2/group/announcement`; |
| 26 | + const language = 'zh-CN'; |
| 27 | + const limit = Number.parseInt(ctx.req.query('limit') ?? '20', 10); |
| 28 | + const rows = Number.isNaN(limit) || limit <= 0 ? 20 : limit; |
| 29 | + const headers = { |
| 30 | + Referer: pageUrl, |
| 31 | + 'Accept-Language': language, |
| 32 | + 'User-Agent': config.trueUA, |
| 33 | + lang: language, |
| 34 | + }; |
| 35 | + |
| 36 | + const noticeResponse = (await ofetch<NoticeResponse>(`${listUrl}?rows=${rows}&page=1`, { headers })) as NoticeResponse; |
| 37 | + |
| 38 | + const notices = Array.isArray(noticeResponse?.data) ? noticeResponse.data : []; |
| 39 | + |
| 40 | + const items = notices.map((notice) => { |
| 41 | + const noticeKey = notice.code || String(notice.id); |
| 42 | + const link = notice.url?.startsWith('http') ? notice.url : `${baseUrl}/zh-CN/support/announcement/${noticeKey}`; |
| 43 | + const description = notice.detail || notice.textOnlyBody; |
| 44 | + |
| 45 | + return { |
| 46 | + title: notice.title, |
| 47 | + link, |
| 48 | + description, |
| 49 | + pubDate: parseDate(notice.time), |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + return { |
| 54 | + title: '币安公告', |
| 55 | + link: pageUrl, |
| 56 | + item: items, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +export const route: Route = { |
| 61 | + path: '/messages/announcement', |
| 62 | + categories: ['finance'], |
| 63 | + view: ViewType.Articles, |
| 64 | + example: '/binance/messages/announcement', |
| 65 | + radar: [ |
| 66 | + { |
| 67 | + source: ['www.binance.com/zh-CN/messages/v2/group/announcement'], |
| 68 | + target: '/binance/messages/announcement', |
| 69 | + }, |
| 70 | + ], |
| 71 | + name: '币安公告', |
| 72 | + description: 'Announcement list from Binance message center.', |
| 73 | + maintainers: ['DIYgod'], |
| 74 | + handler, |
| 75 | +}; |
0 commit comments