From c2d63704eedad1ac08046d0b1da043859c9e872f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 12 Jun 2021 13:34:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=BE=99=E8=85=BE=E7=BD=91=E7=BD=91?= =?UTF-8?q?=E5=B8=96=E7=BF=BB=E8=AF=91=20(#7652)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 10 +++--- lib/router.js | 2 +- lib/routes/ltaaa/_article.js | 35 ------------------ lib/routes/ltaaa/index.js | 70 ++++++++++++++++++++++++++++++++++++ lib/routes/ltaaa/main.js | 58 ------------------------------ 5 files changed, 76 insertions(+), 99 deletions(-) delete mode 100644 lib/routes/ltaaa/_article.js create mode 100644 lib/routes/ltaaa/index.js delete mode 100644 lib/routes/ltaaa/main.js diff --git a/docs/bbs.md b/docs/bbs.md index bcef757ca9..074c655afe 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -390,13 +390,13 @@ pageClass: routes ## 龙腾网 -### 转译网贴 +### 网帖翻译 - + -| 最新 | 每周 | 每月 | 全年 | -| ---- | ---- | ----- | ---- | -| (空) | week | month | year | +| 最新 | 科技 | 娱乐 | 文化 | 社会 | 体育 | 历史 | 趣闻 | 图说世界 | +| ------ | ---------- | ----- | ------- | --------- | ----- | ------- | ----------- | -------- | +| latest | technology | funny | culture | community | sport | history | curiosities | picture | diff --git a/lib/router.js b/lib/router.js index 8cbbe2cf54..284da22cff 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/ltaaa/_article.js b/lib/routes/ltaaa/_article.js deleted file mode 100644 index 6f117a9d11..0000000000 --- a/lib/routes/ltaaa/_article.js +++ /dev/null @@ -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() + '


' + $('div.post-comment').html(); - - const item = { - title: title, - pubDate: date.toUTCString(), - author: author, - link: url, - description: content, - }; - return item; -}; diff --git a/lib/routes/ltaaa/index.js b/lib/routes/ltaaa/index.js new file mode 100644 index 0000000000..6224375625 --- /dev/null +++ b/lib/routes/ltaaa/index.js @@ -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, + }; +}; diff --git a/lib/routes/ltaaa/main.js b/lib/routes/ltaaa/main.js deleted file mode 100644 index d4e231dc47..0000000000 --- a/lib/routes/ltaaa/main.js +++ /dev/null @@ -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, - }; -};