|
| 1 | +import { renderToString } from 'hono/jsx/dom/server'; |
| 2 | +import { Agent } from 'undici'; |
| 3 | + |
| 4 | +import got from '@/utils/got'; |
| 5 | +import { parseDate } from '@/utils/parse-date'; |
| 6 | +import timezone from '@/utils/timezone'; |
| 7 | + |
| 8 | +import { namespace } from './namespace'; |
| 9 | + |
| 10 | +export const route = { |
| 11 | + path: '/exhibition/:type?', |
| 12 | + categories: ['travel'], |
| 13 | + example: '/zjmuseum/exhibition/ondisplay', |
| 14 | + parameters: { |
| 15 | + type: 'Temporary Exhibition type, supported values: ondisplay (正在展出)、forecast(即将开始)、review(展览回顾). Default: All exhibitions (ondisplay, forecast and review).', |
| 16 | + }, |
| 17 | + name: 'Temporary Exhibition', |
| 18 | + maintainers: ['magazian'], |
| 19 | + radar: [ |
| 20 | + { |
| 21 | + source: ['www.zjmuseum.com.cn/cn/'], |
| 22 | + target: '/exhibition/:type?', |
| 23 | + }, |
| 24 | + ], |
| 25 | + handler: async (ctx) => { |
| 26 | + const baseUrl = 'https://www.zjmuseum.com.cn'; |
| 27 | + const apiUrl = `${baseUrl}/api/exhibition/page`; |
| 28 | + |
| 29 | + const apiConfig = { |
| 30 | + ondisplay: { name: '正在展出', payload: { hasOpen: '1' } }, |
| 31 | + forecast: { name: '即将开始', payload: { hasOpen: '2' } }, |
| 32 | + review: { name: '展览回顾', payload: { hasOpen: '3' } }, |
| 33 | + }; |
| 34 | + |
| 35 | + const typeParam = ctx.req.param('type'); |
| 36 | + const fetchTypes = typeParam ? [typeParam] : Object.keys(apiConfig); |
| 37 | + |
| 38 | + const museumName = namespace.zh?.name || namespace.name; |
| 39 | + |
| 40 | + const titleTag = typeParam ? apiConfig[typeParam as keyof typeof apiConfig]?.name : '全部展览'; |
| 41 | + |
| 42 | + const responses = await Promise.all( |
| 43 | + fetchTypes.map(async (t) => { |
| 44 | + const config = apiConfig[t as keyof typeof apiConfig]; |
| 45 | + |
| 46 | + const response = await got({ |
| 47 | + method: 'post', |
| 48 | + url: apiUrl, |
| 49 | + // The API server has an incompatible ECDHE implementation causing TLS handshake failures in Node 18+. |
| 50 | + dispatcher: new Agent({ |
| 51 | + connect: { |
| 52 | + ecdhCurve: 'X25519:P-256:P-521:P-384', |
| 53 | + }, |
| 54 | + }), |
| 55 | + json: { |
| 56 | + siteId: 22, |
| 57 | + type: 2, |
| 58 | + pageNum: 1, |
| 59 | + pageSize: 10, |
| 60 | + startDate: '', |
| 61 | + endDate: '', |
| 62 | + hasLink: '', |
| 63 | + ...config.payload, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const resData = response.data?.data; |
| 68 | + const list = resData?.[1] || []; |
| 69 | + |
| 70 | + return list.map((item) => { |
| 71 | + const title = item.title; |
| 72 | + const itemLink = `${baseUrl}/cn/#/Exhibition/TemporaryExhibition/${item.content_id}`; |
| 73 | + const pubDate = timezone(parseDate(item.create_time), 8); |
| 74 | + const startDate = item.start_time.split('T', 1)[0]; |
| 75 | + const endDate = item.end_time.split('T', 1)[0]; |
| 76 | + const location = [item.museum_area, item.address].filter(Boolean).join(' '); |
| 77 | + const imgUrl = `${baseUrl}${item.cover_url}`; |
| 78 | + |
| 79 | + // fullDuration is not used here due to the website only provides start and end date. |
| 80 | + const description = renderToString( |
| 81 | + <div> |
| 82 | + <img src={imgUrl} /> |
| 83 | + <br /> |
| 84 | + <p> |
| 85 | + <b>地点:</b> |
| 86 | + {location} |
| 87 | + </p> |
| 88 | + <p> |
| 89 | + <b>开展:</b> |
| 90 | + {startDate || '未定/常设'} |
| 91 | + </p> |
| 92 | + <p> |
| 93 | + <b>闭展:</b> |
| 94 | + {endDate || '未定/常设'} |
| 95 | + </p> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + |
| 99 | + return { |
| 100 | + title, |
| 101 | + link: itemLink, |
| 102 | + pubDate, |
| 103 | + description, |
| 104 | + // For further .ics file processing |
| 105 | + _extra: { |
| 106 | + museumName, |
| 107 | + location, |
| 108 | + startDate, |
| 109 | + endDate, |
| 110 | + }, |
| 111 | + }; |
| 112 | + }); |
| 113 | + }) |
| 114 | + ); |
| 115 | + |
| 116 | + const items = responses.flat(); |
| 117 | + |
| 118 | + return { |
| 119 | + title: `${museumName} - 临时展览 - ${titleTag}`, |
| 120 | + link: `${baseUrl}/cn/#/Exhibition/TemporaryExhibition`, |
| 121 | + language: 'zh-CN', |
| 122 | + item: items, |
| 123 | + }; |
| 124 | + }, |
| 125 | +}; |
0 commit comments