-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 增加北航集成电路学院 * refactor: 直接使用路径作为参数 * refactor: migrate to v2 ---------
- Loading branch information
Showing
9 changed files
with
163 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
'/news/:type': ['AlanDecode'], | ||
'/sme/:path*': ['MeanZhang'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '')}`, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters