Skip to content

Commit

Permalink
feat: 增加北航集成电路学院 (#13924)
Browse files Browse the repository at this point in the history
* feat: 增加北航集成电路学院

* refactor: 直接使用路径作为参数

* refactor: migrate to v2

---------
  • Loading branch information
MeanZhang authored Dec 1, 2023
1 parent a35b10c commit c1c757c
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 129 deletions.
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ router.get('/henu/:type?', lazyloadRouteHandler('./routes/universities/henu/news
router.get('/nku/jwc/:type?', lazyloadRouteHandler('./routes/universities/nku/jwc/index'));

// 北京航空航天大学
router.get('/buaa/news/:type', lazyloadRouteHandler('./routes/universities/buaa/news/index'));
// router.get('/buaa/news/:type', lazyloadRouteHandler('./routes/universities/buaa/news/index'));

// 浙江工业大学
router.get('/zjut/:type', lazyloadRouteHandler('./routes/universities/zjut/index'));
Expand Down
66 changes: 0 additions & 66 deletions lib/routes/universities/buaa/news/index.js

This file was deleted.

57 changes: 0 additions & 57 deletions lib/routes/universities/buaa/utils.js

This file was deleted.

4 changes: 4 additions & 0 deletions lib/v2/buaa/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
'/news/:type': ['AlanDecode'],
'/sme/:path*': ['MeanZhang'],
};
46 changes: 46 additions & 0 deletions lib/v2/buaa/news/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
const baseUrl = 'https://news.buaa.edu.cn';
const { type } = ctx.params;

const { data: response, url: link } = await got(`${baseUrl}/${type}.htm`);

const $ = cheerio.load(response);
const title = $('.subnav span').text().trim();
const list = $('.mainleft > .listlefttop > .listleftop1')
.toArray()
.map((item) => {
item = $(item);
const title = item.find('h2 > a');
return {
title: title.text(),
link: new URL(title.attr('href'), baseUrl).href,
pubDate: timezone(parseDate(item.find('h2 em').text(), '[YYYY-MM-DD]'), +8),
};
});

const result = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got(item.link);
const $ = cheerio.load(response.data);

item.description = $('.v_news_content').html();
item.author = $('.vsbcontent_end').text().trim();

return item;
})
)
);

ctx.state.data = {
title: `北航新闻 - ${title}`,
link,
description: `北京航空航天大学新闻网 - ${title}`,
item: result,
};
};
21 changes: 21 additions & 0 deletions lib/v2/buaa/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
'buaa.edu.cn': {
_name: '北京航空航天大学',
news: [
{
title: '新闻网',
docs: 'https://docs.rsshub.app/routes/university#bei-jing-hang-kong-hang-tian-da-xue',
source: ['/*'],
target: (_, url) => `/buaa/news${new URL(url).pathname.replace('.htm', '')}`,
},
],
'www.sme': [
{
title: '集成电路科学与工程学院',
docs: 'https://docs.rsshub.app/routes/university#bei-jing-hang-kong-hang-tian-da-xue-ji-cheng-dian-lu-ke-xue-yu-gong-cheng-xue-yuan',
source: ['/*'],
target: (_, url) => `/buaa/sme${new URL(url).pathname.replace('.htm', '')}`,
},
],
},
};
4 changes: 4 additions & 0 deletions lib/v2/buaa/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (router) => {
router.get('/news/:type', require('./news/index'));
router.get('/sme/:path*', require('./sme'));
};
59 changes: 59 additions & 0 deletions lib/v2/buaa/sme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

const BASE_URL = 'http://www.sme.buaa.edu.cn';

module.exports = async (ctx) => {
const { path = 'tzgg' } = ctx.params;
const url = `${BASE_URL}/${path}.htm`;
const { title, list } = await getList(url);
ctx.state.data = {
// 源标题
title,
// 源链接
link: url,
// 源文章
item: await getItems(ctx, list),
};
};

async function getList(url) {
const { data } = await got(url);
const $ = cheerio.load(data);
const title = $('.nytit .fr a')
.toArray()
.slice(1)
.map((item) => $(item).text().trim())
.join(' - ');
const list = $("div[class='Newslist'] > ul > li")
.toArray()
.map((item) => {
item = $(item);
const $a = item.find('a');
const link = $a.attr('href');
return {
title: item.find('a').text(),
link: link.startsWith('http') ? link : `${BASE_URL}/${link}`, // 有些链接是相对路径
pubDate: timezone(parseDate(item.find('span').text()), +8),
};
});
return {
title,
list,
};
}

function getItems(ctx, list) {
return Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: descrptionResponse } = await got(item.link);
const $descrption = cheerio.load(descrptionResponse);
item.description = $descrption('div[class="v_news_content"]').html();
return item;
})
)
);
}
33 changes: 28 additions & 5 deletions website/docs/routes/university.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,36 @@ Note:[Source website](https://ece.umass.edu/seminar) may be empty when there's

## 北京航空航天大学 {#bei-jing-hang-kong-hang-tian-da-xue}

### 北京航空航天大学 {#bei-jing-hang-kong-hang-tian-da-xue-bei-jing-hang-kong-hang-tian-da-xue}
### 新闻网 {#bei-jing-hang-kong-hang-tian-da-xue-bei-jing-hang-kong-hang-tian-da-xue}

<Route author="AlanDecode" example="/buaa/news/zonghe" path="/buaa/news/:type" paramsDesc={['新闻版块']}>
<Route author="AlanDecode" example="/buaa/news/zhxw" path="/buaa/news/:type" paramsDesc={['新闻版块']} radar="1">

| 综合新闻 | 信息公告 | 学术文化 | 校园风采 | 科教在线 | 媒体北航 | 专题新闻 | 北航人物 |
| -------- | -------- | ------------ | -------- | -------- | -------- | -------- | -------- |
| zonghe | gonggao | xueshuwenhua | fengcai | kejiao | meiti | zhuanti | renwu |
| 综合新闻 | 信息公告 | 学术文化 | 校园风采 | 科教在线 | 媒体北航 | 专题新闻 | 北航人物 |
| ------ | -------- | ----------- | -------- | -------- | -------- | ------- | ------- |
| zhxw | xxgg_new | xsjwhhd_new | xyfc_new | kjzx_new | mtbh_new | ztxw | bhrw |

</Route>

### 集成电路科学与工程学院 {#bei-jing-hang-kong-hang-tian-da-xue-ji-cheng-dian-lu-ke-xue-yu-gong-cheng-xue-yuan}

<Route author="MeanZhang" example="/buaa/sme/tzgg" path="/buaa/sme/:path*" paramsDesc={['版块路径,默认为 `gzgg`']} radar="1">

:::tip

路径参数应填写板块 URL 中 `http://www.sme.buaa.edu.cn/``.htm` 之间的字段。

示例:

1. [通知公告](http://www.sme.buaa.edu.cn/tzgg.htm) 页面的 URL 为 `http://www.sme.buaa.edu.cn/tzgg.htm`,对应的路径参数为 `tzgg`,完整路由为 `/buaa/sme/tzgg`
2. [就业信息](http://www.sme.buaa.edu.cn/zsjy/jyxx.htm) 页面的 URL 为 `http://www.sme.buaa.edu.cn/zsjy/jyxx.htm`,对应的路径参数为 `zsjy/jyxx`,完整路由为 `/buaa/sme/zsjy/jyxx`

:::

:::warning

部分页面(如[学院介绍](http://www.sme.buaa.edu.cn/xygk/xyjs.htm)[微纳中心](http://www.sme.buaa.edu.cn/wnzx.htm)[院学生会](http://www.sme.buaa.edu.cn/xsgz/yxsh.htm))存在无内容、内容跳转至外站等情况,因此可能出现解析失败的现象。

:::

</Route>

Expand Down

0 comments on commit c1c757c

Please sign in to comment.