diff --git a/lib/routes/sxhm/announcement.ts b/lib/routes/sxhm/announcement.ts new file mode 100644 index 000000000..09fa5d3e9 --- /dev/null +++ b/lib/routes/sxhm/announcement.ts @@ -0,0 +1,59 @@ +import { load } from 'cheerio'; + +import type { DataItem, Route } from '@/types'; +import got from '@/utils/got'; +import { parseDate } from '@/utils/parse-date'; + +import { namespace } from './namespace'; + +export const route: Route = { + path: '/announcement', + categories: ['travel'], + example: '/sxhm/announcement', + parameters: {}, + name: 'Announcements', + maintainers: ['magazian'], + radar: [ + { + source: ['www.sxhm.com/info/announcement.html'], + target: '/announcement', + }, + ], + + handler: async () => { + const baseUrl = 'https://www.sxhm.com'; + const apiUrl = `${baseUrl}/info/announcement.html`; + const museumName = namespace.zh?.name || namespace.name; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + + const $ = load(response.data); + + const items = $('.listwrap .list .li') + .toArray() + .map((el) => { + const $item = $(el); + const $a = $item.find('.ltit a'); + const title = $item.find('.ltit .t').text(); + const link = new URL($a.attr('href')!, baseUrl).href; + + const dateStr = $item.find('.lcont .date').text().trim(); + + return { + title, + link, + pubDate: parseDate(dateStr), + }; + }); + + return { + title: `${museumName} - 最新公告`, + link: apiUrl, + language: 'zh-CN', + item: items as DataItem[], + }; + }, +}; diff --git a/lib/routes/sxhm/namespace.ts b/lib/routes/sxhm/namespace.ts new file mode 100644 index 000000000..e6fda8f2b --- /dev/null +++ b/lib/routes/sxhm/namespace.ts @@ -0,0 +1,10 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Shaanxi History Museum', + url: 'www.sxhm.com/', + + zh: { + name: '陕西历史博物馆', + }, +}; diff --git a/lib/routes/sxhm/temporary.tsx b/lib/routes/sxhm/temporary.tsx new file mode 100644 index 000000000..3b9250929 --- /dev/null +++ b/lib/routes/sxhm/temporary.tsx @@ -0,0 +1,180 @@ +import { load } from 'cheerio'; +import { renderToString } from 'hono/jsx/dom/server'; + +import type { DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { parseDate } from '@/utils/parse-date'; + +import { namespace } from './namespace'; + +// format the date to YYYY-MM-DD and handle missing year or month +const extractDates = (durationStr: string) => { + let startDate: string | undefined; + let endDate: string | undefined; + + if (!durationStr) { + return { startDate, endDate }; + } + + const parts = durationStr.split(/[-—~至]+/).map((p) => p.trim()); + const startStr = parts[0]; + const endStr = parts[1]; + + let startYear: string | undefined; + let startMonth: string | undefined; + + const startRegex = /(\d{4})年(\d{1,2})月(\d{1,2})日/; + const startMatch = startStr.match(startRegex); + + if (startMatch) { + startYear = startMatch[1]; + startMonth = startMatch[2].padStart(2, '0'); + const startDay = startMatch[3].padStart(2, '0'); + startDate = `${startYear}-${startMonth}-${startDay}`; + } + + if (endStr && startDate) { + const endRegex = /(?:(\d{4})年)?(?:(\d{1,2})月)?(\d{1,2})日/; + const endMatch = endStr.match(endRegex); + + if (endMatch) { + const matchYear = endMatch[1]; + const matchMonth = endMatch[2]?.padStart(2, '0'); + const matchDay = endMatch[3].padStart(2, '0'); + + const finalEndYear = matchYear || startYear; + const finalEndMonth = matchMonth || startMonth; + const finalEndDay = matchDay; + endDate = `${finalEndYear}-${finalEndMonth}-${finalEndDay}`; + } + } + + return { startDate, endDate }; +}; + +export const route: Route = { + path: '/temporary', + categories: ['travel'], + example: '/sxhm/temporary', + parameters: {}, + name: 'Special Exhibition', // use sxhm's EN version name + maintainers: ['magazian'], + radar: [ + { + source: ['www.sxhm.com/Temporary.html'], + target: '/temporary', + }, + ], + + handler: async () => { + const baseUrl = 'https://www.sxhm.com'; + const apiUrl = `${baseUrl}/Temporary.html`; + const museumName = namespace.zh?.name || namespace.name; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + + const $ = load(response.data); + + const items = await Promise.all( + $('.temporary_exhibition2 .list .item') + .toArray() + .map(async (el) => { + const $item = $(el); + + const $a = $item.find('.text .t1'); + const link = new URL($a.attr('href')!, baseUrl).href; + const imgUrlRaw = $item.find('.pic img.i').attr('src') || ''; + const imgUrl = new URL(imgUrlRaw, baseUrl).href; + + let title = $a.text(); + + const $pEls = $item.find('.text .t2 .p'); + let fullDuration = $pEls.eq(0).text().replaceAll('时间:', '').trim(); + const location = $pEls.eq(1).text().replaceAll('地点:', '').trim(); + + // if title or duration ends with ..., need to fetch the detail page to get the full info + if (title.endsWith('...') || fullDuration.endsWith('...')) { + const detailData = await cache.tryGet(link, async () => { + const detailRes = await got({ + method: 'get', + url: link, + }); + return detailRes.data; + }); + + const $detail = load(detailData as string); + + const detailTitle = $detail('.universal2 .title, .universal3 .tbox .title').text(); + if (detailTitle) { + title = detailTitle; + } + + const detailDuration = $detail('.universal2 .txt_box .infor .item .t').eq(0).text().trim(); + if (detailDuration) { + fullDuration = detailDuration; + } + } + + // get title from “” + const titleMatch = title.match(/“(.+?)”/); + if (titleMatch) { + title = titleMatch[1]; + } + + const { startDate, endDate } = extractDates(fullDuration); + const pubDate = startDate ? parseDate(startDate) : undefined; + + const description = renderToString( +
+ 地点: + {location} +
++ 开展: + {startDate ?? '未定/常设'} +
++ 闭展: + {endDate ?? '未定/常设'} +
+ {fullDuration && ( ++ 原始展期:{fullDuration} +
+ )} +