fix(route): route jiaowu and xxgk for Donghua University (#15721)

* fix: update name url for Donghua University

* fix: route /dhu/jiaowu/news and /dhu/jiaowu/xxgk/news and fetch content as description

* fix: camelCase / fetch content and meta at the same time

* fix: return item using cache.tryGet
This commit is contained in:
Ishirai 2024-05-28 10:40:15 +08:00 committed by GitHub
parent 9a0ef60f53
commit 73c9e1fdfd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 100 additions and 43 deletions

View File

@ -2,13 +2,15 @@ import { Route } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import cache from '@/utils/cache';
const base_url = 'https://jw.dhu.edu.cn';
const baseUrl = 'https://jw.dhu.edu.cn';
const map = {
student: '/tzggwxszl/list.htm',
teacher: '/tzggwjszl/list.htm',
class: '/tzggwxkzl_19850/list.htm',
fxzy: '/fxzy/list.htm',
};
export const route: Route = {
path: '/jiaowu/news/:type?',
@ -26,32 +28,59 @@ export const route: Route = {
name: '教务处通知',
maintainers: ['KiraKiseki'],
handler,
description: `| 学生专栏 | 教师专栏 |
| -------- | -------- |
| student | teacher |`,
description: `| 学生专栏 | 教师专栏 | 选课专栏(仅选课期间开放) | 辅修专业 |
| -------- | -------- | -------- | -------- |
| student | teacher | class | fxzy |`,
};
async function handler(ctx) {
const type = ctx.req.param('type');
const link = Object.hasOwn(map, type) ? `${base_url}${map[type]}` : `${base_url}/tzggwxszl/list.htm`;
const response = await got({
method: 'get',
url: link,
headers: {
Referer: base_url,
},
});
const type = ctx.req.param('type') || 'student';
const link = `${baseUrl}${map[type]}`;
const { data: response } = await got(link);
const $ = load(response);
const items = await Promise.all(
$('.list2 > li')
.toArray()
.map(async (item) => {
item = $(item);
const newsTitle = item.find('.news_title > a');
const newsMeta = item.find('.news_meta');
// article meta
const link = newsTitle.attr('href');
const title = newsTitle.text();
const pubDate = parseDate(newsMeta.text(), 'YYYY-MM-DD', 'zh-cn');
// fetch article content and return item using cache.tryGet
// url as cache key
const url = `${baseUrl}${link}`;
return await cache.tryGet(url, async () => {
// fetch article content
// some contents are only available for internal network
let description = '';
try {
const { data: response } = await got(url);
const $ = load(response);
description = $('.wp_articlecontent').first().html() ?? '';
} catch {
description = '';
}
return {
title,
link,
pubDate,
description,
};
});
})
);
const $ = load(response.data);
return {
link: base_url,
title: '东华大学教务处-' + $('.col_title').text(),
item: $('.list_item')
.map((_, elem) => ({
link: new URL($('a', elem).attr('href'), base_url),
title: $('a', elem).attr('title'),
pubDate: timezone(parseDate($('.Article_PublishDate', elem).text()), +8),
}))
.get(),
link,
item: items,
};
}

View File

@ -2,5 +2,5 @@ import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '东华大学',
url: 'gs.dhu.edu.cn',
url: 'www.dhu.edu.cn',
};

View File

@ -2,9 +2,10 @@ import { Route } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import cache from '@/utils/cache';
const base_url = 'https://xxgk.dhu.edu.cn/1737/list.htm';
const siteUrl = 'https://xxgk.dhu.edu.cn';
const baseUrl = 'https://xxgk.dhu.edu.cn/1737/list.htm';
export const route: Route = {
path: '/xxgk/news',
@ -25,25 +26,52 @@ export const route: Route = {
};
async function handler() {
const link = base_url;
const response = await got({
method: 'get',
url: link,
headers: {
Referer: base_url,
},
});
const link = baseUrl;
const { data: response } = await got(link);
const $ = load(response);
const items = await Promise.all(
$('.cols_list > li')
.toArray()
.map(async (item) => {
item = $(item);
const colsTitle = item.find('.cols_title > a');
const colsMeta = item.find('.cols_meta');
// article meta
const link = colsTitle.attr('href');
const title = colsTitle.text();
const pubDate = parseDate(colsMeta.text(), 'YYYY-MM-DD', 'zh-cn');
// fetch article content and return item using cache.tryGet
// url as cache key
const url = `${siteUrl}${link}`;
return await cache.tryGet(url, async () => {
// fetch article content
// some contents are only available for internal network
let description = '';
try {
const { data: response } = await got(url);
const $ = load(response);
description = $('.wp_articlecontent').first().html() ?? '';
} catch {
description = '';
}
return {
title,
link,
pubDate,
description,
};
});
})
);
const $ = load(response.data);
return {
link: base_url,
title: '东华大学信息公开网-最新公开信息',
item: $('.cols')
.map((_, elem) => ({
link: new URL($('a', elem).attr('href'), base_url),
title: $('a', elem).attr('title'),
pubDate: timezone(parseDate($('.cols_meta', elem).text()), +8),
}))
.get(),
link,
item: items,
};
}