Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions lib/routes/ahm/abxw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { load } from 'cheerio';

import type { DataItem, Route } from '@/types';
import { parseDate } from '@/utils/parse-date';
import { getPlaywrightPage } from '@/utils/playwright';
import timezone from '@/utils/timezone';

import { namespace } from './namespace';

const baseUrl = 'https://www.ahm.cn';

export const route: Route = {
path: '/news/abxw',
categories: ['travel'],
example: '/ahm/news/abxw',
features: {
requireConfig: false,
requirePuppeteer: true,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.ahm.cn/News/List/abxw'],
target: '/news/abxw',
},
],
name: '安博新闻',
maintainers: ['magazian'],
handler: async () => {
const museumName = namespace.zh?.name || namespace.name;
const listUrl = `${baseUrl}/News/List/abxw`;

// Anhui Museum website use CT2-WAAP to prevent web scraping, so need to use Playwright to get the page content.
const { page, destroy } = await getPlaywrightPage(listUrl, {
gotoConfig: { waitUntil: 'domcontentloaded' },
});

let html: string;
try {
await page.waitForSelector('ul.img-cont-list li', {
timeout: 15000,
});
html = await page.content();
} finally {
await destroy();
}

const $ = load(html);

const items: DataItem[] = $('ul.img-cont-list li')
.toArray()
.map((el) => {
const $el = $(el);
const a = $el.find('a');
const href = a.attr('href') ?? '';
const link = new URL(href, baseUrl).href;

const dateP = $el.find('.date p');
const monthDay = dateP.eq(0).text();
const year = dateP.eq(1).text();
const dateStr = `${year}/${monthDay}`;

return {
title: $el.find('.cont h3').text(),
link,
pubDate: timezone(parseDate(dateStr, 'YYYY/MM/DD'), 8),
};
});

return {
title: `${museumName} - 安博新闻`,
link: listUrl,
language: 'zh-CN',
item: items,
};
},
};
10 changes: 10 additions & 0 deletions lib/routes/ahm/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Anhui Museum',
url: 'www.ahm.cn',

zh: {
name: '安徽博物院',
},
};
152 changes: 152 additions & 0 deletions lib/routes/ahm/xztj.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { load } from 'cheerio';
import dayjs from 'dayjs';
import { renderToString } from 'hono/jsx/dom/server';

import type { DataItem, Route } from '@/types';
import { parseDate } from '@/utils/parse-date';
import { getPlaywrightPage } from '@/utils/playwright';

import { namespace } from './namespace';

const baseUrl = 'https://www.ahm.cn';

const parseExhibitionDate = (dateStr?: string): string | undefined => (dateStr ? dayjs(dateStr).format('YYYY-MM-DD') : undefined);

const parseExhibitionDuration = (durationText: string) => {
const [start, end] = durationText.split('-').map((s) => s.trim());
return {
startDate: parseExhibitionDate(start),
endDate: parseExhibitionDate(end),
};
};

export const route: Route = {
path: '/exhibition/xztj',
categories: ['travel'],
example: '/ahm/exhibition/xztj',
features: {
requireConfig: false,
requirePuppeteer: true,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.ahm.cn/Exhibition/TListNow/xztj'],
target: '/exhibition/xztj',
},
],
name: 'Special Exhibition',
maintainers: ['magazian'],
handler: async () => {
const museumName = namespace.zh?.name || namespace.name;
const listUrl = `${baseUrl}/Exhibition/TListNow/xztj`;

// Anhui Museum website use CT2-WAAP to prevent web scraping, so need to use Playwright to get the page content.
const { page, destroy } = await getPlaywrightPage(listUrl, {
gotoConfig: { waitUntil: 'domcontentloaded' },
});

let html: string;
try {
await page.waitForSelector('ul.exhibition-new li', {
timeout: 15000,
});
html = await page.content();
} finally {
await destroy();
}

const $ = load(html);

const items: DataItem[] = $('ul.exhibition-new li')
.toArray()
.flatMap((el) => {
const $el = $(el);

const onclickAttr = $el.find('a[onclick]').first().attr('onclick') ?? '';
const onclickMatch = onclickAttr.match(/f_visitCount\('(\d+)','([^']+)','([^']+)'\)/);
if (!onclickMatch) {
return [];
}

const exhibitionId = onclickMatch[1];
const link = onclickMatch[2];

const title = $el.find('.cont h3').text();
const imgUrl = $el.find('img').attr('src') ?? '';

const durationText =
$el
.find('p')
.toArray()
.map((p) => $(p).text())
.find((t) => t.startsWith('展出时间:')) ?? '';

const locationText =
$el
.find('p')
.toArray()
.map((p) => $(p).text())
.find((t) => t.startsWith('展出地点:')) ?? '';

const location = locationText.replace('展出地点:', '');
const fullDuration = durationText.replace('展出时间:', '');

const { startDate, endDate } = parseExhibitionDuration(fullDuration);

const pubDate = startDate ? parseDate(startDate) : undefined;

const description = renderToString(
<div>
<img src={imgUrl} />
<br />
<p>
<b>地点:</b>
{location || '参考详情'}
</p>
<p>
<b>开展:</b>
{startDate || '未定/常设'}
</p>
<p>
<b>闭展:</b>
{endDate || '未定/常设'}
</p>
{fullDuration && (
<p>
<small>原始展期:{fullDuration}</small>
</p>
)}
</div>
);

const guid = `${baseUrl}/Exhibition/Details/xztj?nid=${exhibitionId}`;

return [
{
title,
link,
guid,
pubDate,
description,
_extra: {
museumName,
location,
startDate,
endDate,
},
},
];
});

return {
title: `${museumName} - 新展推介`,
link: listUrl,
language: 'zh-CN',
item: items,
};
},
};