fix: 龙腾网网帖翻译 (#7652)

This commit is contained in:
Ethan Shen
2021-06-11 22:34:46 -07:00
committed by GitHub
parent 9bbb3f6e71
commit c2d63704ee
5 changed files with 76 additions and 99 deletions
+5 -5
View File
@@ -390,13 +390,13 @@ pageClass: routes
## 龙腾网
### 转译网贴
### 网帖翻译
<Route author="sgqy" example="/ltaaa" path="/ltaaa/:type?" :paramsDesc="['热门类型.']">
<Route author="sgqy nczitzk" example="/ltaaa" path="/ltaaa/:category?" :paramsDesc="['分类,见下表,默认为最新']">
| 最新 | 每周 | 每月 | 全年 |
| ---- | ---- | ----- | ---- |
| (空) | week | month | year |
| 最新 | 科技 | 娱乐 | 文化 | 社会 | 体育 | 历史 | 趣闻 | 图说世界 |
| ------ | ---------- | ----- | ------- | --------- | ----- | ------- | ----------- | -------- |
| latest | technology | funny | culture | community | sport | history | curiosities | picture |
</Route>
+1 -1
View File
@@ -1068,7 +1068,7 @@ router.get('/nhentai/search/:keyword/:mode?', require('./routes/nhentai/search')
router.get('/nhentai/:key/:keyword/:mode?', require('./routes/nhentai/other'));
// 龙腾网
router.get('/ltaaa/:type?', require('./routes/ltaaa/main'));
router.get('/ltaaa/:category?', require('./routes/ltaaa/index'));
// AcFun
router.get('/acfun/bangumi/:id', require('./routes/acfun/bangumi'));
-35
View File
@@ -1,35 +0,0 @@
const got = require('@/utils/got'); // get web content
const cheerio = require('cheerio'); // html parser
const domain = 'http://www.ltaaa.com';
module.exports = async function get_article(url) {
if (/^\/.*$/.test(url)) {
url = domain + url;
}
const response = await got({
method: 'get',
url: url,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
responseType: 'buffer',
});
const data = response.data;
const $ = cheerio.load(data);
const title = $('div.post-title > h1').text();
const author = $('div.post-param > a').text();
const pub_date_raw = $('div.post-param').clone().children().remove().end().text();
let date = new Date(pub_date_raw);
date.setHours(date.getHours() - 8);
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
const content = $('div.post-content').html() + '<br/><hr/><br/>' + $('div.post-comment').html();
const item = {
title: title,
pubDate: date.toUTCString(),
author: author,
link: url,
description: content,
};
return item;
};
+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 category = ctx.params.category || 'latest';
const rootUrl = 'http://www.ltaaa.cn';
const currentUrl = `${rootUrl}/${category === 'picture' ? category : `article${category === 'latest' ? '' : `/${category}`}`}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $(category === 'picture' || category === 'curiosities' ? 'dd .title' : '.li-title a')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
if (category === 'picture') {
item.description = '';
content('.show li').each(function () {
item.description += content(this).find('a').html() + (content(this).find('.pic-p').html() || '');
});
item.pubDate = parseDate(
content('.view a img')
.attr('src')
.match(/http:\/\/img\.ltaaa\.cn\/uploadfile\/(.*)\/\d+\.jpg/)[1],
'YYYY/MM/DD'
);
} else {
content('.post-param').find('a, span').remove();
item.pubDate = timezone(new Date(content('.post-param').text().trim()), +8);
content('.post-title, .post-param, .post-keywords, .like-post, .clear, .hook').remove();
item.description = content('.post-body').html();
}
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};
-58
View File
@@ -1,58 +0,0 @@
// Warning: The author still knows nothing about javascript!
// params:
// type: notification type
const got = require('@/utils/got'); // get web content
const cheerio = require('cheerio'); // html parser
const get_article = require('./_article');
const base_url = 'http://www.ltaaa.com';
module.exports = async (ctx) => {
const type = ctx.params.type || 'news'; // week, month or year
let target = '';
switch (type) {
case 'week':
target = 'ul.vweek';
break;
case 'month':
target = 'ul.vmonth';
break;
case 'year':
target = 'ul.vyear';
break;
default:
target = 'ul.wlist';
}
const list_url = base_url + '/wtfy.html';
const response = await got({
method: 'get',
url: list_url,
});
const data = response.data; // content is html format
const $ = cheerio.load(data);
// get urls
const detail_urls = [];
let a = $(target).find('a.rtitle');
if (!a || a.length <= 0) {
a = $(target).find('div.li-title > a');
}
for (let i = 0; i < a.length; ++i) {
const tmp = $(a[i]).attr('href');
detail_urls.push(tmp);
}
// get articles
const article_list = await Promise.all(detail_urls.map((url) => get_article(url)));
// feed the data
ctx.state.data = {
title: '龙腾网转译网贴',
link: list_url,
item: article_list,
};
};