Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add route for School of Economics & Management, Tongji University (同济大学经济与管理学院) #17516

Merged
merged 10 commits into from
Nov 10, 2024
71 changes: 71 additions & 0 deletions lib/routes/tongji/sem/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';

import { config } from '@/config';

export async function getNotifByPage(pageNumber: number) {
const pageUrl: string = `https://sem.tongji.edu.cn/semch/category/frontpage/notice/page/${pageNumber}`;

try {
const response = await got.get(pageUrl, {
sitdownkevin marked this conversation as resolved.
Show resolved Hide resolved
headers: {
'User-Agent': config.ua,
},
});

const html = response.body;
const $ = load(html);

const notifListElements = $('#page-wrap > div.maim_pages > div > div.leftmain_page > div > ul > li');

return notifListElements.toArray().map((Element) => {
const aTagFirst = $(Element).find('a.bt');
const aTagSecond = $(Element).find('a.time');

const title = aTagFirst.attr('title');
const href = aTagFirst.attr('href');
const time = aTagSecond.text().trim();

return {
title,
link: href,
pubDate: parseDate(time, 'YYYY-MM-DD'),
};
});
} catch {
// console.error(error);
}
return [];
}

export async function getLastPageNumber() {
try {
const response = await got.get('https://sem.tongji.edu.cn/semch/category/frontpage/notice', {
headers: {
'User-Agent': config.ua,
},
});
const html = response.body;
const $ = load(html);

const lastPageElement = $('#page-wrap > div.maim_pages > div > div.leftmain_page > div > div > a.extend');
const lastPageUrl: string | undefined = lastPageElement.attr('href');

// console.log(lastPageUrl);

if (lastPageUrl) {
const lastPageNumber = lastPageUrl.match(/page\/(\d+)/)?.[1];
if (lastPageNumber) {
// console.log(`Last page number: ${lastPageNumber}`);
return Number.parseInt(lastPageNumber);
} else {
// console.error('Failed to extract last page number.');
}
}
} catch {
// console.error(error);
}

return -1;
}
sitdownkevin marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions lib/routes/tongji/sem/notice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Warning: The author still knows nothing about javascript!
import { Route } from '@/types';

sitdownkevin marked this conversation as resolved.
Show resolved Hide resolved
export const route: Route = {
path: '/sem',
categories: ['university'],
example: '/tongji/sem',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '经济与管理学院通知',
maintainers: ['sitdownkevin'],
url: 'sem.tongji.edu.cn/semch/category/frontpage/notice',
handler,
description: ``,
};

import { getNotifByPage } from './_utils';

async function handler() {
sitdownkevin marked this conversation as resolved.
Show resolved Hide resolved
const results = await getNotifByPage(1);

// feed the data to rss
return {
title: '同济大学经济与管理学院',
link: 'https://sem.tongji.edu.cn/semch/category/frontpage/notice',
item: results,
};
}