feat(route): add Uwants (#8094)

Co-authored-by: DIYgod <diy.d.god@gmail.com>
This commit is contained in:
Ethan Shen
2021-11-27 07:33:27 +00:00
committed by GitHub
co-authored by DIYgod
parent 970db993ee
commit 2b8ec7f36c
3 changed files with 79 additions and 0 deletions
+6
View File
@@ -925,6 +925,12 @@ Provides all of the Thrillist articles with the specified tag.
<Route author="kt286" example="/topys/7" path="/topys/:category" :paramsDesc="['分类ID,可在对应页面的 URL 中找到']"/>
## Uwants
### 版塊
<Route author="nczitzk" example="/uwants/1520" path="/uwants/:fid" :paramsDesc="['fid,可在对应板块页的 URL 中找到']"/>
## VOA News
### Day in Photos
+3
View File
@@ -4207,6 +4207,9 @@ router.get('/right/forum/:id?', lazyloadRouteHandler('./routes/right/forum'));
// 香港經濟日報
router.get('/hket/:category?', lazyloadRouteHandler('./routes/hket/index'));
// Uwants
router.get('/uwants/:fid', lazyloadRouteHandler('./routes/uwants'));
// Now新聞
router.get('/now/news/rank', lazyloadRouteHandler('./routes/now/rank'));
+70
View File
@@ -0,0 +1,70 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const fid = ctx.params.fid;
const rootUrl = 'https://www.uwants.com';
const currentUrl = `${rootUrl}/archiver/?fid-${fid}.html`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('li a')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 15)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}/${item.attr('href')}`,
};
})
.get();
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const data = detailResponse.data
.replace(/\[(\w+)=([\w\d#]+)\]/gm, '<span style="$1: $2;">')
.replace(/\[url=(.*?)\]/gm, '<a href="$1">')
.replace(/\[\/(color|size)\]/gm, '</span>')
.replace(/\[\/url\]/gm, '</a>')
.replace(/\[(\w+)\]/gm, '<$1>')
.replace(/\[\/(\w+)\]/gm, '</$1>');
const content = cheerio.load(data);
const cite = content('cite');
item.author = cite.eq(0).text();
const firstAuthorParent = cite.eq(0).parent();
item.description = content('.archiver_post').html();
cite.remove();
item.link = `${rootUrl}/viewthread.php?tid=${item.link.match(/tid-(\d+)\.html/)[1]}`;
item.pubDate = timezone(parseDate(firstAuthorParent.text().trim(), 'YYYY-M-D hh:mm A'), +8);
return item;
})
)
);
ctx.state.data = {
title: `${$('h1 a').text().replace('查看完整版本: ', '')} - Uwants.com`,
link: `${rootUrl}/forumdisplay.php?fid=${fid}`,
item: items,
};
};