-
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.
* fix(route): 武汉大学新闻网 * fix typo * docs: fix docs ---------
- Loading branch information
Showing
7 changed files
with
407 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
'/cs/:type': ['ttyfly'], | ||
'/gs/:type?': ['Delreyaa'], | ||
'/news/:type*': ['SChen1024'], | ||
'/news/:category?': ['SChen1024', 'nczitzk'], | ||
}; |
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 |
---|---|---|
@@ -1,102 +1,70 @@ | ||
const got = require('@/utils/got'); | ||
const cheerio = require('cheerio'); | ||
const { parseDate } = require('@/utils/parse-date'); | ||
const timezone = require('@/utils/timezone'); | ||
const { art } = require('@/utils/render'); | ||
const path = require('path'); | ||
|
||
// 参考 bit/jwc 北京理工大学的的页面写成 | ||
const { domain, processMeta, getMeta, processItems } = require('./util'); | ||
|
||
const baseUrl = 'https://news.whu.edu.cn/'; | ||
|
||
// 专门定义一个function用于加载文章内容 | ||
async function load(link) { | ||
let description = ''; | ||
let pubDate = ''; | ||
|
||
let response; | ||
|
||
// 外部链接访问不到不处理, 校内页面访问不到 记录错误 | ||
try { | ||
// 异步请求文章 | ||
response = await got(link); | ||
} catch (err) { | ||
// 如果网络问题 直接出错 | ||
if (err.name && (err.name === 'HTTPError' || err.name === 'RequestError')) { | ||
description = 'Page 404 Please Check!'; | ||
} | ||
return { description }; | ||
} | ||
// 加载文章内容 | ||
const $ = cheerio.load(response.data); | ||
module.exports = async (ctx) => { | ||
const { category = 'wdzx/wdyw' } = ctx.params; | ||
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 10; | ||
|
||
pubDate = $('meta[name="PubDate"]').length ? timezone(parseDate($('meta[name="PubDate"]').attr('content')), 8) : undefined; | ||
const rootUrl = `https://news.${domain}`; | ||
const currentUrl = new URL(`${category}.htm`, rootUrl).href; | ||
|
||
$('div.v_news_content') | ||
.find('img') | ||
.each((_, e) => { | ||
e = $(e); | ||
if (e.attr('orisrc')) { | ||
e.attr('src', new URL(e.attr('orisrc'), response.url).href); | ||
e.removeAttr('orisrc'); | ||
e.removeAttr('vurl'); | ||
} | ||
}); | ||
const { data: response } = await got(currentUrl); | ||
|
||
// 提取文章内容 | ||
description = $('div.v_news_content').html(); | ||
// 返回解析的结果 | ||
return { | ||
description, | ||
pubDate, | ||
author: $('meta[name="ContentSource"]').attr('content'), | ||
}; | ||
} | ||
const $ = cheerio.load(response); | ||
|
||
module.exports = async (ctx) => { | ||
// 默认 武大要闻 然后获取列表页面 | ||
const type = ctx.params.type || 'wdzx/wdyw'; | ||
const listPageUrl = baseUrl + type + '.htm'; | ||
const response = await got(listPageUrl); | ||
const $ = cheerio.load(response.data); | ||
|
||
// 获取当前页面的 list | ||
const list = $('.nyleft ul li') | ||
// The elements where the information is located vary with the category. | ||
// 武大资讯 https://news.whu.edu.cn/wdzx/wdyw.htm => ul.wdzxList li a[title] | ||
// 学术动态 https://news.whu.edu.cn/kydt.htm => ul.xsdtList li a | ||
// 珞珈影像 https://news.whu.edu.cn/stkj/ljyx.htm => div.topPic a[title], ul.nypicList li a[title] | ||
// 武大视频 https://news.whu.edu.cn/stkj/wdsp.htm => div.topVid a[title], ul.nyvidList li a[title] | ||
let items = $('ul.wdzxList li a[title], ul.xsdtList li a, div.topPic a[title], ul.nypicList li a[title], div.topVid a[title], ul.nyvidList li a[title]') | ||
.slice(0, limit) | ||
.toArray() | ||
.map((item) => { | ||
item = $(item); | ||
const a = item.find('a').first(); | ||
|
||
const image = item.find('div.img img'); | ||
|
||
return { | ||
title: a.attr('title') || item.find('.eclips').text(), | ||
link: new URL(a.attr('href'), listPageUrl).href, | ||
description: item.find('.line2').text(), | ||
pubDate: item.find('time').length ? parseDate(item.find('time').text(), 'YYYY.MM.DD') : item.find('.line3').length ? parseDate(item.find('.line3').text()) : undefined, | ||
title: item.prop('title') ?? item.find('h4.eclips').text(), | ||
link: new URL(item.prop('href'), rootUrl).href, | ||
pubDate: parseDate(item.find('time').text(), ['YYYY.MM.DD', 'DDYYYY.MM']), | ||
description: art(path.join(__dirname, 'templates/description.art'), { | ||
description: item.find('div.txt p').html(), | ||
image: image.prop('src') | ||
? { | ||
src: new URL(image.prop('src'), rootUrl).href, | ||
alt: image.prop('alt'), | ||
} | ||
: undefined, | ||
}), | ||
}; | ||
}); | ||
|
||
const result = await Promise.all( | ||
// 遍历每一篇文章 | ||
list.map((item) => | ||
// 合并解析后的结果集作为该篇文章最终的输出结果 | ||
ctx.cache.tryGet(item.link, async () => { | ||
// 如果不是 武汉大学的站点, 直接返回简单的标题即可 | ||
// 判断 是否外站链接,如果是 则直接返回页面 不做单独的解析 | ||
if (!item.link.startsWith(baseUrl)) { | ||
return item; | ||
} | ||
|
||
const { description, pubDate, author } = await load(item.link); | ||
items = await processItems(items, ctx.cache.tryGet, rootUrl); | ||
|
||
item.description = description; | ||
item.pubDate = pubDate; | ||
item.author = author; | ||
const meta = processMeta(response); | ||
const siteName = getMeta(meta, 'SiteName'); | ||
const columnName = getMeta(meta, 'ColumnName'); | ||
|
||
return item; | ||
}) | ||
) | ||
); | ||
const icon = new URL($('link[rel="shortcut icon"]').prop('href'), rootUrl).href; | ||
|
||
ctx.state.data = { | ||
title: $('title').first().text(), | ||
link: listPageUrl, | ||
item: result, | ||
item: items, | ||
title: `${siteName} - ${columnName}`, | ||
link: currentUrl, | ||
description: getMeta(meta, 'description'), | ||
language: $('html').prop('lang'), | ||
image: new URL($('div.logo img').prop('src'), rootUrl).href, | ||
icon, | ||
logo: icon, | ||
subtitle: columnName, | ||
author: siteName, | ||
allowEmpty: true, | ||
}; | ||
}; |
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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = function (router) { | ||
router.get('/cs/:type', require('./cs')); | ||
router.get('/gs/:type?', require('./gs/index.js')); | ||
router.get('/news/:type*', require('./news')); | ||
router.get('/news/:category*', require('./news')); | ||
}; |
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,39 @@ | ||
{{ if description }} | ||
{{@ description }} | ||
{{ /if }} | ||
|
||
{{ if image }} | ||
<figure> | ||
<img src="{{ image.src }}" | ||
{{ if image.alt }} | ||
alt={{ image.alt }} | ||
{{ /if }} | ||
{{ if image.width }} | ||
width={{ image.width }} | ||
{{ /if }} | ||
> | ||
</figure> | ||
{{ /if }} | ||
|
||
{{ if video }} | ||
<video controls | ||
{{ if video.width }} | ||
width={{ video.width }} | ||
{{ /if }} | ||
{{ if video.height }} | ||
height={{ video.height }} | ||
{{ /if }}> | ||
<source src="{{ video.src }}" type="video/{{ video.src.split('.').pop() }}"> | ||
</video> | ||
{{ /if }} | ||
|
||
{{ if attachments && attachments.length > 0 }} | ||
<b>附件</b> | ||
<ul> | ||
{{ each attachments attachment }} | ||
<li> | ||
<a href="{{ attachment.link }}">{{ attachment.title }}</a> | ||
</li> | ||
{{ /each }} | ||
</ul> | ||
{{ /if }} |
Oops, something went wrong.