refactor: 代码优化 (#3465)

This commit is contained in:
Henry Wang 2019-11-22 10:11:41 +00:00 committed by DIYgod
parent c1843885e9
commit 3e492d0033
18 changed files with 34 additions and 50 deletions

View File

@ -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));
};

View File

@ -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()

View File

@ -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;

View File

@ -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('');
}

View File

@ -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) => {

View File

@ -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,

View File

@ -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,

View File

@ -16,12 +16,9 @@ async function load(link, ctx) {
$(images[k]).replaceWith(`<img src="${url.resolve(host, $(images[k]).attr('src'))}" />`);
}
const couponUrl = $('.layui-btn.layui-btn.layui-btn-lg').attr('href');
let eventHtml;
if (!couponUrl || couponUrl === undefined) {
eventHtml = '<div><p>活动链接:无</p></div>';
} else {
eventHtml = `<div><a href="${couponUrl}">点我前往活动</a></div>`;
}
const eventHtml = couponUrl ? '<div><p>活动链接:无</p></div>' : `<div><a href="${couponUrl}">点我前往活动</a></div>`;
const description = eventHtml + $('.p-detail-html').html();
ctx.cache.set(link, description);
return { description };

View File

@ -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 () =>

View File

@ -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`,

View File

@ -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) {

View File

@ -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('');
}

View File

@ -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;

View File

@ -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));
}
});

View File

@ -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() {

View File

@ -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('');
}

View File

@ -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')

View File

@ -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();