Skip to content

Commit

Permalink
feat(route): sysu ygafz (DIYgod#12482)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony authored May 9, 2023
1 parent 49946b6 commit c6c8ac0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/university.md
Original file line number Diff line number Diff line change
Expand Up @@ -3750,3 +3750,17 @@ jsjxy.hbut.edu.cn 证书链不全,自建 RSSHub 可设置环境变量 NODE_TLS
### 数据科学与计算机学院动态

<Route author="Neutrino3316 MegrezZhu nczitzk" example="/sysu/cse" path="/sysu/cse"/>

### 粤港澳发展研究院

<Route author="TonyRL" example="/sysu/ygafz" path="/sysu/ygafz/:type?" :paramsDesc="['分类,见下表,默认为 `notice`']" radar="1" puppeteer="1">

| 人才招聘 | 人才培养 | 新闻动态 | 通知公告 | 专家观点 |
| ---------- | ------------- | -------- | -------- | -------- |
| jobopening | personnelplan | news | notice | opinion |

| 研究成果 | 研究论文 | 学术著作 | 形势政策 |
| -------- | -------- | -------- | -------- |
| results | papers | writings | policy |

</Route>
1 change: 1 addition & 0 deletions lib/v2/sysu/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
'/sysu/cse': ['Neutrino3316', 'MegrezZhu', 'nczitzk'],
'/ygafz/:type?': ['TonyRL'],
};
8 changes: 8 additions & 0 deletions lib/v2/sysu/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ module.exports = {
target: '/sysu/cse',
},
],
ygafz: [
{
title: '粤港澳发展研究院',
docs: 'https://docs.rsshub.app/universities.html#zhong-shan-da-xue',
source: ['/:type?'],
target: '/sysu/ygafz/:type?',
},
],
},
};
1 change: 1 addition & 0 deletions lib/v2/sysu/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = function (router) {
router.get('/cse', require('./cse'));
router.get('/ygafz/:type?', require('./ygafz'));
};
69 changes: 69 additions & 0 deletions lib/v2/sysu/ygafz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const logger = require('@/utils/logger');
const { CookieJar } = require('tough-cookie');

module.exports = async (ctx) => {
const { type = 'notice' } = ctx.params;
const baseUrl = 'https://ygafz.sysu.edu.cn';
const url = `${baseUrl}/${type}`;

const browser = await require('@/utils/puppeteer')();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort();
});

logger.debug(`Requesting ${url}`);
await page.goto(url, {
waitUntil: 'domcontentloaded',
});
await page.waitForSelector('[data-block-plugin-id]');
const response = await page.content();

const cookieJar = new CookieJar();
const cookies = await page.cookies();
cookies.reduce((jar, cookie) => {
jar.setCookie(`${cookie.name}=${cookie.value}`, url);
return jar;
}, cookieJar);

browser.close();

const $ = cheerio.load(response);
const list = $('.list-content a')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('p').text(),
link: `${baseUrl}${item.attr('href')}`,
pubDate: parseDate(item.find('.date').text()), // 2023-03-22
};
});

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

item.author = $('.article-submit')
.text()
.match(/(.*)/)[1];
item.description = $('div[data-block-plugin-id="entity_field:node:body"]').html();
return item;
})
)
);

ctx.state.data = {
title: $('title').text(),
link: url,
item: items,
};
};

0 comments on commit c6c8ac0

Please sign in to comment.