From 3e492d0033ecc5e6aee3985af7403aca8fd67f38 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Fri, 22 Nov 2019 10:11:41 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20(#3465)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/algocasts/all.js | 4 ++-- lib/routes/cartoonmad/comic.js | 2 +- lib/routes/clickme/index.js | 2 +- lib/routes/cnu/selected.js | 2 +- lib/routes/dekudeals/index.js | 3 ++- lib/routes/dlsite/new.js | 3 ++- lib/routes/lol/newsindex.js | 19 ++++++------------- lib/routes/metred/fuli.js | 9 +++------ lib/routes/netease/dy.js | 3 +++ lib/routes/sketch/beta.js | 8 +++----- lib/routes/sspai/tag.js | 2 +- lib/routes/taptap/topic.js | 2 +- lib/routes/tencent/factcheck/index.js | 2 +- lib/routes/universities/bupt/utils.js | 4 ++-- lib/routes/universities/zju/list.js | 7 ++----- lib/routes/whb/zhuzhan.js | 2 +- lib/routes/wineyun/index.js | 8 +------- lib/routes/x-mol/paper.js | 2 +- 18 files changed, 34 insertions(+), 50 deletions(-) diff --git a/lib/routes/algocasts/all.js b/lib/routes/algocasts/all.js index 070072fc9..fb31efaa2 100644 --- a/lib/routes/algocasts/all.js +++ b/lib/routes/algocasts/all.js @@ -8,7 +8,7 @@ const getCacheParsed = async (ctx, key) => { } }; -const generateResponse = async (ctx, items) => ({ +const generateResponse = async (items) => ({ // 源标题 title: 'AlgoCasts', // 源链接 @@ -96,5 +96,5 @@ module.exports = async (ctx) => { infos.push({ id, title, episode, link }); }); - ctx.state.data = await generateResponse(ctx, await makeFull(ctx, infos)); + ctx.state.data = await generateResponse(await makeFull(ctx, infos)); }; diff --git a/lib/routes/cartoonmad/comic.js b/lib/routes/cartoonmad/comic.js index b2e11bb43..52557af20 100644 --- a/lib/routes/cartoonmad/comic.js +++ b/lib/routes/cartoonmad/comic.js @@ -33,7 +33,7 @@ const getChapters = async (id, $, caches) => { link, title, }; - const other = await caches.tryGet(link, async () => await load(a, `https://www.cartoonmad.com/home75458/${id}/${title.split(' ')[1]}`)); + const other = await caches.tryGet(link, () => load(a, `https://www.cartoonmad.com/home75458/${id}/${title.split(' ')[1]}`)); return Promise.resolve(Object.assign({}, single, other)); }) .toArray() diff --git a/lib/routes/clickme/index.js b/lib/routes/clickme/index.js index fb19d154d..ac35aff05 100644 --- a/lib/routes/clickme/index.js +++ b/lib/routes/clickme/index.js @@ -50,7 +50,7 @@ module.exports = async (ctx) => { try { const response = await got.get(link); const result = parseContent(response.data); - if (!result) { + if (!result.description) { return Promise.resolve(''); } rssitem.description = result.description; diff --git a/lib/routes/cnu/selected.js b/lib/routes/cnu/selected.js index f51b14d75..d8270577c 100644 --- a/lib/routes/cnu/selected.js +++ b/lib/routes/cnu/selected.js @@ -29,7 +29,7 @@ module.exports = async (ctx) => { try { const response = await got.get(link); const result = utils.parseContent(response.data); - if (!result) { + if (!result.description) { return Promise.resolve(''); } diff --git a/lib/routes/dekudeals/index.js b/lib/routes/dekudeals/index.js index 757ec0f65..ae3cac04b 100644 --- a/lib/routes/dekudeals/index.js +++ b/lib/routes/dekudeals/index.js @@ -4,6 +4,8 @@ const url = require('url'); module.exports = async (ctx) => { const type = ctx.params.type; + + const host = 'https://www.dekudeals.com/'; const link = url.resolve(host, type); const response = await got.get(link); @@ -12,7 +14,6 @@ module.exports = async (ctx) => { const title = $('#search-title').text(); const list = $('.search-main > .item-grid2 > div'); - const host = 'https://www.dekudeals.com/'; const out = await Promise.all( list .map(async (index, item) => { diff --git a/lib/routes/dlsite/new.js b/lib/routes/dlsite/new.js index 2115c81b5..2ef0d7d2d 100644 --- a/lib/routes/dlsite/new.js +++ b/lib/routes/dlsite/new.js @@ -50,12 +50,13 @@ const infos = { module.exports = async (ctx) => { const info = infos[ctx.params.type]; - const link = host + info.url; // 判断参数是否合理 if (info === undefined) { throw Error('不支持指定类型!'); } + const link = host + info.url; + const response = await got(link, { method: 'GET', baseUrl: host, diff --git a/lib/routes/lol/newsindex.js b/lib/routes/lol/newsindex.js index a6b00e90e..439b6f64d 100644 --- a/lib/routes/lol/newsindex.js +++ b/lib/routes/lol/newsindex.js @@ -68,17 +68,12 @@ module.exports = async (ctx) => { } function getUrl(sRedirectURL, iDocID, sVID) { // 由于数据源太多,具体的URL返回逻辑可以参考news/index.html页面里面的handleData方法 - let pageUrl; + if (sRedirectURL) { - pageUrl = sRedirectURL; - if (pageUrl.indexOf('docid') > 0) { - sRedirectURL = pageUrl; + if (sRedirectURL.indexOf('?') > 0) { + sRedirectURL = sRedirectURL + '&docid=' + iDocID; } else { - if (pageUrl.indexOf('?') > 0) { - sRedirectURL = pageUrl + '&docid=' + iDocID; - } else { - sRedirectURL = pageUrl + '?docid=' + iDocID; - } + sRedirectURL = sRedirectURL + '?docid=' + iDocID; } } else { if (sVID) { @@ -87,10 +82,8 @@ module.exports = async (ctx) => { sRedirectURL = 'http://lol.qq.com/news/detail.shtml?docid=' + iDocID; } } - pageUrl = sRedirectURL; - // console.log(iDocID,pageUrl); - // console.log('\niDocID:', iDocID, '\nsVID:', sVID, '\nsRedirectURL:', sRedirectURL, '\nsURL:', sUrl, '\npageurl:', pageUrl, '\n'); - return pageUrl; + + return sRedirectURL; } return list.map((item) => ({ title: `【${typeName}】` + item.sTitle, diff --git a/lib/routes/metred/fuli.js b/lib/routes/metred/fuli.js index 342e734bd..035a56f04 100644 --- a/lib/routes/metred/fuli.js +++ b/lib/routes/metred/fuli.js @@ -16,12 +16,9 @@ async function load(link, ctx) { $(images[k]).replaceWith(``); } const couponUrl = $('.layui-btn.layui-btn.layui-btn-lg').attr('href'); - let eventHtml; - if (!couponUrl || couponUrl === undefined) { - eventHtml = '

活动链接:无

'; - } else { - eventHtml = `
点我前往活动
`; - } + + const eventHtml = couponUrl ? '

活动链接:无

' : `
点我前往活动
`; + const description = eventHtml + $('.p-detail-html').html(); ctx.cache.set(link, description); return { description }; diff --git a/lib/routes/netease/dy.js b/lib/routes/netease/dy.js index 064cd6213..8443ef73f 100644 --- a/lib/routes/netease/dy.js +++ b/lib/routes/netease/dy.js @@ -16,6 +16,9 @@ module.exports = async (ctx) => { if (e.docid) { e.link = 'http://dy.163.com/v2/article/detail/' + e.docid + '.html'; } + + author = e.source ? e.source : ''; + const response = await ctx.cache.tryGet( e.link, async () => diff --git a/lib/routes/sketch/beta.js b/lib/routes/sketch/beta.js index 81cf62eaf..6465ff4fa 100644 --- a/lib/routes/sketch/beta.js +++ b/lib/routes/sketch/beta.js @@ -16,21 +16,21 @@ module.exports = async (ctx) => { const version = $('small.heading-caption') .text() .trim(); - // console.log(version); + // 判断是否已发布 const releaseString = $('.wrapper') .first() .find('h1') .text() .substr(-8); - // console.log(releaseString); + let isrelease = 0; if (releaseString === 'released') { isrelease = 1; } else { isrelease = 0; } - // console.log(isrelease); + const list = $('.wrapper').first(); // sketch update 提供的时间 年月反了.要重新调整 const pubday = list @@ -48,7 +48,6 @@ module.exports = async (ctx) => { const pubdateString = pubmonth + `-` + pubday + `-` + pubyear; if (isrelease === 1) { - // console.log("已经发布到正式版 "); ctx.state.data = { title: `Sketch Beta`, link: response.url, @@ -74,7 +73,6 @@ module.exports = async (ctx) => { .get(), }; } else { - // console.log("Beta版本发布 "); const content = $('.update-details').html(); ctx.state.data = { title: `Sketch Beta`, diff --git a/lib/routes/sspai/tag.js b/lib/routes/sspai/tag.js index bdc45a911..f8323e9bc 100644 --- a/lib/routes/sspai/tag.js +++ b/lib/routes/sspai/tag.js @@ -16,7 +16,7 @@ module.exports = async (ctx) => { const items = await Promise.all( data.map(async (item) => { const link = `https://sspai.com/post/${item.id}`; - let description = ``; + let description; const key = item.id; const value = await ctx.cache.get(key); if (value) { diff --git a/lib/routes/taptap/topic.js b/lib/routes/taptap/topic.js index d0589bcc3..f16e632e3 100644 --- a/lib/routes/taptap/topic.js +++ b/lib/routes/taptap/topic.js @@ -61,7 +61,7 @@ module.exports = async (ctx) => { try { const topic_response = await got.get(link); const result = parseContent(topic_response.data); - if (!result) { + if (!result.description) { return Promise.resolve(''); } diff --git a/lib/routes/tencent/factcheck/index.js b/lib/routes/tencent/factcheck/index.js index 401bbcaf2..7ecbf620a 100644 --- a/lib/routes/tencent/factcheck/index.js +++ b/lib/routes/tencent/factcheck/index.js @@ -42,7 +42,7 @@ module.exports = async (ctx) => { try { const response = await got.get(link); const result = parseContent(response.data); - if (!result) { + if (!result.description) { return Promise.resolve(''); } rssitem.description = result.description; diff --git a/lib/routes/universities/bupt/utils.js b/lib/routes/universities/bupt/utils.js index c3ff37742..9adcfbd27 100644 --- a/lib/routes/universities/bupt/utils.js +++ b/lib/routes/universities/bupt/utils.js @@ -34,7 +34,7 @@ const ProcessFeed = async (list, cache, current, type) => $(`${current.selector.content} img`).each((index, elem) => { const $elem = $(elem); const src = $elem.attr('src'); - if (src && src !== '') { + if (src) { $elem.attr('src', url.resolve(current.url, src)); } }); @@ -42,7 +42,7 @@ const ProcessFeed = async (list, cache, current, type) => $(`${current.selector.content} a`).each((index, elem) => { const $elem = $(elem); const src = $elem.attr('href'); - if (src && src !== '') { + if (src) { $elem.attr('href', url.resolve(current.url, src)); } }); diff --git a/lib/routes/universities/zju/list.js b/lib/routes/universities/zju/list.js index f8885522a..1be12b848 100644 --- a/lib/routes/universities/zju/list.js +++ b/lib/routes/universities/zju/list.js @@ -25,14 +25,11 @@ module.exports = async (ctx) => { return pubdateString; } function sortUrl(e) { - let linka = host; if (e.search('redirect') !== -1) { - linka = link; + return link; } else { - linka = e; + return e; } - - return linka; } const list = $('#wp_news_w7 ul.news li') .map(function() { diff --git a/lib/routes/whb/zhuzhan.js b/lib/routes/whb/zhuzhan.js index db3a2b06f..8948acd7a 100644 --- a/lib/routes/whb/zhuzhan.js +++ b/lib/routes/whb/zhuzhan.js @@ -54,7 +54,7 @@ module.exports = async (ctx) => { try { const response = await got.get(link); const result = parseContent(response.data); - if (!result) { + if (!result.description) { return Promise.resolve(''); } diff --git a/lib/routes/wineyun/index.js b/lib/routes/wineyun/index.js index 783022e68..c49b5e188 100644 --- a/lib/routes/wineyun/index.js +++ b/lib/routes/wineyun/index.js @@ -34,13 +34,7 @@ module.exports = async (ctx) => { .text() .trim() : $(item).attr('title'); - const itemPicUrl = !flag - ? $(item) - .find('div.fl img') - .attr('src') - : $(item) - .find('div.fl img') - .attr('src'); + const itemPicUrl = item.find('div.fl img').attr('src'); const href = !flag ? $(item) .find('dt.fl a') diff --git a/lib/routes/x-mol/paper.js b/lib/routes/x-mol/paper.js index ba1d92230..9b58440b4 100644 --- a/lib/routes/x-mol/paper.js +++ b/lib/routes/x-mol/paper.js @@ -45,7 +45,7 @@ module.exports = async (ctx) => { return result.data; })) || noPic; } - const image = imageUrl || noPic; + const image = imageUrl; const text = $(element) .find('.magazine-description') .text();