mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-24 23:42:40 +08:00
fix(routes/eventernote): fix page iteration, type, radar rule (#19898)
* fix(routes/eventernote): increase page only once per iteration * fix(routes/eventernote): fix type errors * feat(routes/eventernote): add one more page for radar * fix(route/eventernote): refactor event fetching to use ofetch ---------
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Route, ViewType } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { type DataItem, type Route, ViewType } from '@/types';
|
||||
import type { Context } from 'hono';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { load } from 'cheerio';
|
||||
|
||||
const dateStringRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
|
||||
@@ -9,8 +10,8 @@ const timeStringRegexes = [
|
||||
/開場 (?<openHr>\d{2}):(?<openMin>\d{2}) 開演 - 終演 (?<closeHr>\d{2}):(?<closeMin>\d{2})/,
|
||||
];
|
||||
|
||||
const maxPages = 5;
|
||||
const pageCount = 10;
|
||||
const maxPages = 3;
|
||||
const pageCount = 20;
|
||||
|
||||
export const route: Route = {
|
||||
path: '/actors/:name/:id',
|
||||
@@ -28,7 +29,7 @@ export const route: Route = {
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['www.eventernote.com/actors/:name/:id/events'],
|
||||
source: ['www.eventernote.com/actors/:name/:id', 'www.eventernote.com/actors/:name/:id/events'],
|
||||
},
|
||||
],
|
||||
name: '声优活动及演唱会',
|
||||
@@ -36,90 +37,86 @@ export const route: Route = {
|
||||
handler,
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
async function handler(ctx: Context) {
|
||||
const { name, id } = ctx.req.param();
|
||||
|
||||
const title = `${name}のイベント・ライブ情報一覧`;
|
||||
const link = `https://www.eventernote.com/actors/${name}/${id}/events`;
|
||||
|
||||
const events = [];
|
||||
for (let page = 1; page <= maxPages; page++) {
|
||||
const pageLink = link + `?limit=${pageCount}&page=${page}`;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: pageLink,
|
||||
});
|
||||
const pageLinks = Array.from({ length: maxPages }, (_, i) => link + `?limit=${pageCount}&page=${i + 1}`);
|
||||
const responses = await Promise.all(pageLinks.map((pageUrl) => ofetch(pageUrl)));
|
||||
|
||||
const data = response.data;
|
||||
|
||||
const $ = load(data);
|
||||
const events = responses.flatMap((response) => {
|
||||
const $ = load(response);
|
||||
const list = $('li.clearfix');
|
||||
|
||||
if (list.length === 0) {
|
||||
break;
|
||||
return [] as DataItem[];
|
||||
}
|
||||
|
||||
for (const event of list) {
|
||||
// extract event name
|
||||
const eventName = $('div.event > h4 > a', event).text();
|
||||
const pageItems = list
|
||||
.toArray()
|
||||
.map((event) => {
|
||||
// extract event name
|
||||
const eventName = $('div.event > h4 > a', event).text();
|
||||
|
||||
// extract event location
|
||||
const eventLocation = $('div.event > div.place > a', event).text();
|
||||
// extract event location
|
||||
const eventLocation = $('div.event > div.place > a', event).text();
|
||||
|
||||
// extract event date
|
||||
const dateMatches = $('div.date > p', event).text().match(dateStringRegex);
|
||||
const eventYear = dateMatches.groups.year;
|
||||
const eventMonth = dateMatches.groups.month;
|
||||
const eventDay = dateMatches.groups.day;
|
||||
// extract event date
|
||||
const dateMatches = $('div.date > p', event).text().match(dateStringRegex);
|
||||
const eventYear = dateMatches?.groups?.year;
|
||||
const eventMonth = dateMatches?.groups?.month;
|
||||
const eventDay = dateMatches?.groups?.day;
|
||||
|
||||
// extract event time
|
||||
const timeString = $('div.event > div.place > span.s', event).text();
|
||||
let eventTimeObj = {
|
||||
openHr: null,
|
||||
openMin: null,
|
||||
startHr: null,
|
||||
startMin: null,
|
||||
closeHr: null,
|
||||
closeMin: null,
|
||||
};
|
||||
for (const r of timeStringRegexes) {
|
||||
const m = timeString.match(r);
|
||||
if (m === null) {
|
||||
continue;
|
||||
}
|
||||
eventTimeObj = {
|
||||
...eventTimeObj,
|
||||
...m.groups,
|
||||
// extract event time
|
||||
const timeString = $('div.event > div.place > span.s', event).text();
|
||||
let eventTimeObj = {
|
||||
openHr: null,
|
||||
openMin: null,
|
||||
startHr: null,
|
||||
startMin: null,
|
||||
closeHr: null,
|
||||
closeMin: null,
|
||||
};
|
||||
}
|
||||
for (const r of timeStringRegexes) {
|
||||
const m = timeString.match(r);
|
||||
if (m === null) {
|
||||
continue;
|
||||
}
|
||||
eventTimeObj = {
|
||||
...eventTimeObj,
|
||||
...m.groups,
|
||||
};
|
||||
}
|
||||
|
||||
// extract event link
|
||||
const link = $('div.event > h4 > a', event).attr('href');
|
||||
// extract event link
|
||||
const link = $('div.event > h4 > a', event).attr('href');
|
||||
|
||||
// compute event description
|
||||
const eventDescription = `イベント ${eventName}
|
||||
// compute event description
|
||||
const eventDescription = `イベント ${eventName}
|
||||
開催場所 ${eventLocation}
|
||||
開場 ${eventYear}-${eventMonth}-${eventDay} ${eventTimeObj.openHr}:${eventTimeObj.openMin}
|
||||
開演 ${eventYear}-${eventMonth}-${eventDay} ${eventTimeObj.startHr}:${eventTimeObj.startMin}
|
||||
終演 ${eventYear}-${eventMonth}-${eventDay} ${eventTimeObj.closeHr}:${eventTimeObj.closeHr}
|
||||
終演 ${eventYear}-${eventMonth}-${eventDay} ${eventTimeObj.closeHr}:${eventTimeObj.closeMin}
|
||||
`;
|
||||
|
||||
events.push({
|
||||
title: eventName,
|
||||
description: eventDescription,
|
||||
link,
|
||||
});
|
||||
}
|
||||
return {
|
||||
title: eventName,
|
||||
description: eventDescription,
|
||||
link,
|
||||
} as DataItem;
|
||||
})
|
||||
.filter(Boolean) as DataItem[];
|
||||
|
||||
page += 1;
|
||||
}
|
||||
return pageItems;
|
||||
});
|
||||
|
||||
return {
|
||||
title,
|
||||
link,
|
||||
description: title,
|
||||
language: 'ja',
|
||||
language: 'ja' as const,
|
||||
allowEmpty: true,
|
||||
item: events,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user