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(route): NCKU CSIE News #16907

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
90 changes: 90 additions & 0 deletions lib/routes/ncku/csie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Route, DataItem } from '@/types';
import { load } from 'cheerio';

const currentURL = (catagory: string, page: number) => (catagory === '_all' ? `https://www.csie.ncku.edu.tw/zh-hant/news?page=${page}` : `https://www.csie.ncku.edu.tw/zh-hant/news/${catagory}?page=${page}`);

const catagories = {
_all: '全部資訊',
normal: '一般資訊',
bachelorAdmission: '大學部招生',
masterAdmission: '研究所招生',
speeches: '演講及活動資訊',
awards: '獲獎資訊',
scholarship: '獎助學金',
jobs: '徵人資訊',
};

export const route: Route = {
'zh-TW': {
name: '國立成功大學資訊系公告',
description: '可用分類:_all, normal, bachelorAdmission, masterAdmission, speeches, awards, scholarship, jobs',
},
name: 'NCKU CSIE News',
simbafs marked this conversation as resolved.
Show resolved Hide resolved
description: 'Availible catagories:_all, normal, bachelorAdmission, masterAdmission, speeches, awards, scholarship, jobs',
path: '/csie/:catagory?',
categories: ['university'],
example: '/csie/normal',
simbafs marked this conversation as resolved.
Show resolved Hide resolved
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.csie.ncku.edu.tw/zh-hant/news/:catagory?/', 'www.csie.ncku.edu.tw/en/news/:catagory?/'],
target: '/zh-hant/:catagory',
},
],
maintainers: ['simbafs'],
handler: async (ctx) => {
let catagory = ctx.req.param('catagory') ?? '_all';
if (catagories[catagory] === undefined) {
catagory = 'normal';
}

const base = 1; // get from query
const limit = 10; // get from query

// console.log({ catagory }, currentURL(catagory, 1));

const item = await Promise.allSettled(
Array.from({ length: limit }).map((_, i) =>
fetch(currentURL(catagory, base + i))
simbafs marked this conversation as resolved.
Show resolved Hide resolved
.then((res) => {
if (!res.ok) {
throw new Error(`Failed to fetch ${res.url}`);
}
return res.text();
})
.then(load)
.then(($) => {
const r = $('.list-title > li')
.toArray()
.map((el) => ({
title: $('a', el).text(),
pubDate: new Date($('small', el).text()),
link: `https://www.csie.ncku.edu.tw${$('a', el).attr('href')}`,
catagory: $('span:nth-child(2)', el).text(),
}));

return r;
})
)
)
.then((result) => result.filter((item) => item.status === 'fulfilled'))
.then((result) => result.reduce((acc: DataItem[], cur) => [...acc, ...cur.value], []));
Fixed Show fixed Hide fixed
simbafs marked this conversation as resolved.
Show resolved Hide resolved
// .then((d) => {
// console.log(d);
// return d;
// });

return {
title: `成大資訊系公告 - ${catagories[catagory]}`,
link: `https://www.csie.ncku.edu.tw/zh-hant/${catagory}/`,
item,
};
},
};
6 changes: 6 additions & 0 deletions lib/routes/ncku/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namepsace: Namespace = {
name: 'ncku',
simbafs marked this conversation as resolved.
Show resolved Hide resolved
url: 'ncku.edu.tw',
};