From 1fb172045c4cad49fdc8e3368f712e4b05e053e6 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 11 May 2018 18:50:41 +0800 Subject: [PATCH] generate template after filter for a better performance --- index.js | 5 +++++ middleware/cache.js | 34 +++++++++---------------------- middleware/filter.js | 21 +++++++------------ {utils => middleware}/template.js | 7 ++++--- routes/bilibili/bangumi.js | 5 ++--- routes/bilibili/coin.js | 5 ++--- routes/bilibili/dynamic.js | 5 ++--- routes/bilibili/fav.js | 5 ++--- routes/bilibili/followers.js | 5 ++--- routes/bilibili/followings.js | 5 ++--- routes/bilibili/linkNews.js | 6 ++---- routes/bilibili/partion.js | 5 ++--- routes/bilibili/reply.js | 5 ++--- routes/bilibili/video.js | 5 ++--- routes/biquge/chapter.js | 5 ++--- routes/dapenti/tugua.js | 5 ++--- routes/disqus/posts.js | 5 ++--- routes/dockone/weekly.js | 5 ++--- routes/douban/later.js | 5 ++--- routes/douban/playing.js | 5 ++--- routes/douban/ustop.js | 5 ++--- routes/express/express.js | 5 ++--- routes/instagram/user.js | 5 ++--- routes/jandan/pic.js | 5 ++--- routes/jianshu/collection.js | 5 ++--- routes/jianshu/home.js | 5 ++--- routes/jianshu/monthly.js | 5 ++--- routes/jianshu/user.js | 5 ++--- routes/jianshu/weekly.js | 5 ++--- routes/juejin/category.js | 5 ++--- routes/mzitu/category.js | 5 ++--- routes/mzitu/post.js | 5 ++--- routes/mzitu/tag.js | 5 ++--- routes/mzitu/tags.js | 5 ++--- routes/ncm/artist.js | 5 ++--- routes/ncm/playlist.js | 5 ++--- routes/ncm/userplaylist.js | 5 ++--- routes/pixiv/bookmarks.js | 5 ++--- routes/pixiv/ranking.js | 5 ++--- routes/pixiv/user.js | 5 ++--- routes/tieba/forum.js | 5 ++--- routes/toutiao/today.js | 5 ++--- routes/toutiao/user.js | 5 ++--- routes/tucaoqq/post.js | 5 ++--- routes/twitter/user.js | 5 ++--- routes/weibo/keyword.js | 5 ++--- routes/weibo/user.js | 5 ++--- routes/youtube/channel.js | 5 ++--- routes/youtube/user.js | 5 ++--- routes/zhihu/activities.js | 5 ++--- routes/zhihu/answers.js | 5 ++--- routes/zhihu/collection.js | 5 ++--- routes/zhihu/zhuanlan.js | 5 ++--- routes/ziroom/room.js | 5 ++--- 54 files changed, 126 insertions(+), 192 deletions(-) rename {utils => middleware}/template.js (56%) diff --git a/index.js b/index.js index 5bd488dfb..3aeea2e4a 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const utf8 = require('./middleware/utf8'); const memoryCache = require('./middleware/cache.js'); const redisCache = require('koa-redis-cache'); const filter = require('./middleware/filter.js'); +const template = require('./middleware/template.js'); const router = require('./router'); @@ -29,6 +30,10 @@ app.use(header); // fix incorrect `utf-8` characters app.use(utf8); +// generate body +app.use(template); + +// filter content app.use(filter); // cache diff --git a/middleware/cache.js b/middleware/cache.js index 88e9c293c..9b1242059 100644 --- a/middleware/cache.js +++ b/middleware/cache.js @@ -88,7 +88,11 @@ module.exports = function (options = {}) { if (Buffer.isBuffer(type)) {type = type.toString();} ctx.response.set('X-Koa-Memory-Cache', 'true'); ctx.response.type = type; - ctx.response.body = value; + try { + ctx.state.data = JSON.parse(value); + } catch (e) { + ctx.state.data = {}; + } ok = true; } @@ -99,34 +103,16 @@ module.exports = function (options = {}) { * setCache */ async function setCache (ctx, key, tkey) { - let body = ctx.response.body; + ctx.state.data.lastBuildDate = new Date().toUTCString(); + const body = JSON.stringify(ctx.state.data); - if (ctx.request.method !== 'GET' || ctx.response.status !== 200 || !body) { + if (ctx.request.method !== 'GET' || !body) { return; } - - if (typeof body === 'string') { - // string - if (Buffer.byteLength(body) > maxLength) {return;} - memoryCache.set(key, body); - } else if (Buffer.isBuffer(body)) { - // buffer - if (body.length > maxLength) {return;} - memoryCache.set(key, body); - } else if (typeof body === 'object' && ctx.response.type === 'application/json') { - // json - body = JSON.stringify(body); - if (Buffer.byteLength(body) > maxLength) {return;} - memoryCache.set(key, body); - } else if (typeof body.pipe === 'function') { - // stream - body = await read(body); - ctx.response.body = body; - if (Buffer.byteLength(body) > maxLength) {return;} - memoryCache.set(key, body); - } else { + if (Buffer.byteLength(body) > maxLength) { return; } + memoryCache.set(key, body); await cacheType(ctx, tkey); } diff --git a/middleware/filter.js b/middleware/filter.js index d72cf6f52..272620945 100644 --- a/middleware/filter.js +++ b/middleware/filter.js @@ -1,20 +1,13 @@ -const cheerio = require('cheerio'); - module.exports = async (ctx, next) => { await next(); - if (ctx.body && ctx.query && (ctx.query.filter || ctx.query.filter_title || ctx.query.filter_description)) { - const $ = cheerio.load(ctx.body, { - xmlMode: true - }); - $('item').filter((index, item) => { - const title = $(item).find('title').text(); - const description = $(item).find('description').text(); - return ctx.query.filter && !title.match(ctx.query.filter) && !description.match(ctx.query.filter) + if (ctx.state.data && ctx.query && (ctx.query.filter || ctx.query.filter_title || ctx.query.filter_description)) { + ctx.state.data.item = ctx.state.data.item.filter((item) => { + const title = item.title; + const description = item.description; + return !(ctx.query.filter && !title.match(ctx.query.filter) && !description.match(ctx.query.filter) || ctx.query.filter_title && !title.match(ctx.query.filter_title) - || ctx.query.filter_description && !description.match(ctx.query.filter_description); - }).remove(); - - ctx.body = $.xml(); + || ctx.query.filter_description && !description.match(ctx.query.filter_description)); + }); } }; diff --git a/utils/template.js b/middleware/template.js similarity index 56% rename from utils/template.js rename to middleware/template.js index bc3ba1bf0..7d4e11d97 100644 --- a/utils/template.js +++ b/middleware/template.js @@ -2,10 +2,11 @@ const art = require('art-template'); const path = require('path'); const config = require('../config'); -module.exports = function (options) { - return art(path.resolve(__dirname, '../views/rss.art'), { +module.exports = async (ctx, next) => { + await next(); + ctx.body = art(path.resolve(__dirname, '../views/rss.art'), { lastBuildDate: new Date().toUTCString(), ttl: config.cacheExpire, - ...options, + ...ctx.state.data, }); }; \ No newline at end of file diff --git a/routes/bilibili/bangumi.js b/routes/bilibili/bangumi.js index 030cd5ed6..3885a8852 100644 --- a/routes/bilibili/bangumi.js +++ b/routes/bilibili/bangumi.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -16,7 +15,7 @@ module.exports = async (ctx) => { const data = JSON.parse(response.data.match(/^seasonListCallback\((.*)\);$/)[1]).result || {}; - ctx.body = template({ + ctx.state.data = { title: data.title, link: `https://bangumi.bilibili.com/anime/${seasonid}/`, description: data.evaluate, @@ -26,5 +25,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.update_time).toUTCString(), link: item.webplay_url })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/coin.js b/routes/bilibili/coin.js index 7c7b7005e..a7a230834 100644 --- a/routes/bilibili/coin.js +++ b/routes/bilibili/coin.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -30,7 +29,7 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 bilibili 投币视频`, link: `https://space.bilibili.com/${uid}`, description: `${name} 的 bilibili 投币视频`, @@ -39,5 +38,5 @@ module.exports = async (ctx) => { description: `${item.title}
`, link: `https://www.bilibili.com/video/av${item.stat.aid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/dynamic.js b/routes/bilibili/dynamic.js index 5cb6b2470..d5694488e 100644 --- a/routes/bilibili/dynamic.js +++ b/routes/bilibili/dynamic.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const JSONbig = require('json-bigint'); const config = require('../../config'); @@ -16,7 +15,7 @@ module.exports = async (ctx) => { }); const data = response.data.data.cards; - ctx.body = template({ + ctx.state.data = { title: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`, link: `https://space.bilibili.com/${uid}/#/dynamic`, description: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`, @@ -54,5 +53,5 @@ module.exports = async (ctx) => { link: link }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/fav.js b/routes/bilibili/fav.js index ce77ef9d7..2e93f0ffc 100644 --- a/routes/bilibili/fav.js +++ b/routes/bilibili/fav.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -30,7 +29,7 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 bilibili 收藏夹`, link: `https://space.bilibili.com/${uid}/#/favlist`, description: `${name} 的 bilibili 收藏夹`, @@ -41,5 +40,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.fav_at * 1000).toUTCString(), link: `https://www.bilibili.com/video/av${item.aid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/followers.js b/routes/bilibili/followers.js index 6f0a29ede..b4bc6734c 100644 --- a/routes/bilibili/followers.js +++ b/routes/bilibili/followers.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -40,7 +39,7 @@ module.exports = async (ctx) => { }); const data = response.data.data.list; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 bilibili 粉丝`, link: `https://space.bilibili.com/${uid}/#/fans/fans`, description: `${name} 的 bilibili 粉丝`, @@ -50,5 +49,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.mtime * 1000).toUTCString(), link: `https://space.bilibili.com/${item.mid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/followings.js b/routes/bilibili/followings.js index 7683255cc..c8990d245 100644 --- a/routes/bilibili/followings.js +++ b/routes/bilibili/followings.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -40,7 +39,7 @@ module.exports = async (ctx) => { }); const data = response.data.data.list; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 bilibili 关注`, link: `https://space.bilibili.com/${uid}/#/fans/follow`, description: `${name} 的 bilibili 关注`, @@ -50,5 +49,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.mtime * 1000).toUTCString(), link: `https://space.bilibili.com/${item.mid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/linkNews.js b/routes/bilibili/linkNews.js index 1ec73d4da..de9b5ad65 100644 --- a/routes/bilibili/linkNews.js +++ b/routes/bilibili/linkNews.js @@ -1,6 +1,4 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); const config = require('../../config'); module.exports = async (ctx) => { @@ -31,7 +29,7 @@ module.exports = async (ctx) => { }); const data = response.data.data.items; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.state.data = { title: `bilibili ${productTitle}公告`, link: `https://link.bilibili.com/p/eden/news#/?tab=${product}&tag=all&page_no=1`, description: `bilibili ${productTitle}公告`, @@ -42,5 +40,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.ctime.replace(' ', 'T') + "+08:00").toUTCString(), link: item.announce_link ? item.announce_link : `https://link.bilibili.com/p/eden/news#/newsdetail?id=${item.id}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/partion.js b/routes/bilibili/partion.js index c9e68998f..f11828f3c 100644 --- a/routes/bilibili/partion.js +++ b/routes/bilibili/partion.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -20,7 +19,7 @@ module.exports = async (ctx) => { name = list[0].tname; } - ctx.body = template({ + ctx.state.data = { title: `bilibili ${name}分区`, link: 'https://www.bilibili.com', description: `bilibili ${name}分区`, @@ -30,5 +29,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.pubdate * 1000).toUTCString(), link: `https://www.bilibili.com/video/av${item.aid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/reply.js b/routes/bilibili/reply.js index 16759d2c1..bfc5457c3 100644 --- a/routes/bilibili/reply.js +++ b/routes/bilibili/reply.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); @@ -31,7 +30,7 @@ module.exports = async (ctx) => { const data = response.data.data.replies; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 评论`, link: `https://www.bilibili.com/video/av${aid}`, description: `${name} 的评论`, @@ -41,5 +40,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.ctime * 1000).toUTCString(), link: `https://www.bilibili.com/video/av${aid}/#reply${item.rpid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/bilibili/video.js b/routes/bilibili/video.js index de79faed6..4f2494b87 100644 --- a/routes/bilibili/video.js +++ b/routes/bilibili/video.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -30,7 +29,7 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = template({ + ctx.state.data = { title: `${name} 的 bilibili 空间`, link: `https://space.bilibili.com/${uid}`, description: `${name} 的 bilibili 空间`, @@ -40,5 +39,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.created * 1000).toUTCString(), link: `https://www.bilibili.com/video/av${item.aid}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/biquge/chapter.js b/routes/biquge/chapter.js index 7cc29b265..97b0b70be 100644 --- a/routes/biquge/chapter.js +++ b/routes/biquge/chapter.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); const iconv = require('iconv-lite'); @@ -37,11 +36,11 @@ module.exports = async (ctx) => { chapter_item.push(item); } // console.log('chapter_item',chapter_item) - ctx.body = template({ + ctx.state.data = { title: `笔趣阁 ${title}`, link: `${baseUrl}${id}/`, image: cover_url, description: description, item: chapter_item, - }); + }; }; diff --git a/routes/dapenti/tugua.js b/routes/dapenti/tugua.js index 1b49c491f..79b36c73f 100644 --- a/routes/dapenti/tugua.js +++ b/routes/dapenti/tugua.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); const iconv = require('iconv-lite'); @@ -46,10 +45,10 @@ module.exports = async (ctx) => { result_item.push(item) } - ctx.body = template({ + ctx.state.data = { title: `喷嚏图卦`, link: `https://www.dapenti.com/blog/blog.asp?name=xilei&subjectid=70`, description: `喷嚏图卦`, item: result_item, - }); + }; }; diff --git a/routes/disqus/posts.js b/routes/disqus/posts.js index 914960e47..05b91e319 100644 --- a/routes/disqus/posts.js +++ b/routes/disqus/posts.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -36,7 +35,7 @@ module.exports = async (ctx) => { const threads = responseThreads.data.response; - ctx.body = template({ + ctx.state.data = { title: `${forum} 的评论`, link: `https://disqus.com/home/forums/${forum}`, description: `${forum} 的 disqus 评论`, @@ -49,5 +48,5 @@ module.exports = async (ctx) => { link: `${thread.link}/#comment-${item.id}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/dockone/weekly.js b/routes/dockone/weekly.js index f150e9a99..cdcc0d96b 100644 --- a/routes/dockone/weekly.js +++ b/routes/dockone/weekly.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -26,7 +25,7 @@ module.exports = async (ctx) => { const list = $('ul.i.issues>li'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -38,5 +37,5 @@ module.exports = async (ctx) => { link:`${baseUrl}${item.find('a').attr('href')}` }; }).get(), - }); + }; }; diff --git a/routes/douban/later.js b/routes/douban/later.js index 546434a20..a5728ffb2 100644 --- a/routes/douban/later.js +++ b/routes/douban/later.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); module.exports = async (ctx) => { const response = await axios({ @@ -8,7 +7,7 @@ module.exports = async (ctx) => { }); const movieList = response.data.subjects; - ctx.body = template({ + ctx.state.data = { title: '即将上映的电影', link: 'https://movie.douban.com/cinema/later/', item: movieList.map((item) => ({ @@ -16,5 +15,5 @@ module.exports = async (ctx) => { description: `标题:${item.title}
影片类型:${item.genres.join(' | ')}
评分:${item.rating.stars === '00' ? '无' : item.rating.average}
`, link: item.alt })), - }); + }; }; \ No newline at end of file diff --git a/routes/douban/playing.js b/routes/douban/playing.js index f82f9e694..621846590 100644 --- a/routes/douban/playing.js +++ b/routes/douban/playing.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); module.exports = async (ctx) => { const city = ctx.params.city; @@ -11,7 +10,7 @@ module.exports = async (ctx) => { }); const movieList = score ? response.data.subjects.filter((item) => item.rating.average >= score) : response.data.subjects; - ctx.body = template({ + ctx.state.data = { title: `${city ? city : ''}正在上映的${score ? `超过 ${score} 分的` : ''}电影`, link: 'https://movie.douban.com/cinema/nowplaying/', item: movieList.map((item) => ({ @@ -19,5 +18,5 @@ module.exports = async (ctx) => { description: `标题:${item.title}
影片类型:${item.genres.join(' | ')}
评分:${item.rating.average}
`, link: item.alt })), - }); + }; }; \ No newline at end of file diff --git a/routes/douban/ustop.js b/routes/douban/ustop.js index e0f7655ac..40f6b1907 100644 --- a/routes/douban/ustop.js +++ b/routes/douban/ustop.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); module.exports = async (ctx) => { const response = await axios({ @@ -8,7 +7,7 @@ module.exports = async (ctx) => { }); const movieList = response.data.subjects; - ctx.body = template({ + ctx.state.data = { title: '豆瓣电影北美票房榜', link: 'https://movie.douban.com/chart', item: movieList.map((item) => { @@ -19,5 +18,5 @@ module.exports = async (ctx) => { link: item.alt }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/express/express.js b/routes/express/express.js index 893aab278..2950f580e 100644 --- a/routes/express/express.js +++ b/routes/express/express.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -17,7 +16,7 @@ module.exports = async (ctx) => { const data = response.data.data; - ctx.body = template({ + ctx.state.data = { title: `快递 ${company}-${number}`, link: 'https://www.kuaidi100.com', description: `快递 ${company}-${number}`, @@ -27,5 +26,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.time || item.ftime).toUTCString(), link: item.context })), - }); + }; }; \ No newline at end of file diff --git a/routes/instagram/user.js b/routes/instagram/user.js index 237431c45..ee6f242da 100644 --- a/routes/instagram/user.js +++ b/routes/instagram/user.js @@ -1,6 +1,5 @@ const axios = require('axios'); const cheerio = require('cheerio'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -21,7 +20,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); - ctx.body = template({ + ctx.state.data = { title: `${name}(@${id}) 的 Instagram`, link: `https://www.instagram.com/${id}/`, description: $('meta[name="description"]').attr('content'), @@ -46,5 +45,5 @@ module.exports = async (ctx) => { link: `https://www.instagram.com/p/${item.shortcode}/` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/jandan/pic.js b/routes/jandan/pic.js index 6bdc164b7..45cfe15b3 100644 --- a/routes/jandan/pic.js +++ b/routes/jandan/pic.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const crypto = require('crypto'); const config = require('../../config'); @@ -121,10 +120,10 @@ module.exports = async (ctx) => { }); }); - ctx.body = template({ + ctx.state.data = { title: '煎蛋无聊图', link: 'http://jandan.net/pic', description: '煎蛋官方无聊图,无限活力的热门图区。', item: items - }); + }; }; diff --git a/routes/jianshu/collection.js b/routes/jianshu/collection.js index 91eacdf12..f149078f8 100644 --- a/routes/jianshu/collection.js +++ b/routes/jianshu/collection.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -20,7 +19,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: `https://www.jianshu.com/c/${id}`, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -33,5 +32,5 @@ module.exports = async (ctx) => { link: `https://www.jianshu.com${item.find('.title').attr('href')}` }; }).get(), - }); + }; }; \ No newline at end of file diff --git a/routes/jianshu/home.js b/routes/jianshu/home.js index dbb15e36b..da54941a8 100644 --- a/routes/jianshu/home.js +++ b/routes/jianshu/home.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -18,7 +17,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = template({ + ctx.state.data = { title: '简书首页', link: 'https://www.jianshu.com', description: $('meta[name="description"]').attr('content'), @@ -31,5 +30,5 @@ module.exports = async (ctx) => { link: `https://www.jianshu.com${item.find('.title').attr('href')}` }; }).get(), - }); + }; }; \ No newline at end of file diff --git a/routes/jianshu/monthly.js b/routes/jianshu/monthly.js index 8b1e1aef7..f19030ad2 100644 --- a/routes/jianshu/monthly.js +++ b/routes/jianshu/monthly.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -18,7 +17,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = template({ + ctx.state.data = { title: '简书 30 日热门', link: 'https://www.jianshu.com/trending/monthly', description: '简书 30 日热门', @@ -31,5 +30,5 @@ module.exports = async (ctx) => { link: `https://www.jianshu.com${item.find('.title').attr('href')}` }; }).get(), - }); + }; }; \ No newline at end of file diff --git a/routes/jianshu/user.js b/routes/jianshu/user.js index 7dc4b842b..3e6d7f8a6 100644 --- a/routes/jianshu/user.js +++ b/routes/jianshu/user.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -20,7 +19,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: `https://www.jianshu.com/u/${id}`, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -33,5 +32,5 @@ module.exports = async (ctx) => { link: `https://www.jianshu.com${item.find('.title').attr('href')}` }; }).get(), - }); + }; }; \ No newline at end of file diff --git a/routes/jianshu/weekly.js b/routes/jianshu/weekly.js index 1edcb182e..67712ffc8 100644 --- a/routes/jianshu/weekly.js +++ b/routes/jianshu/weekly.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -18,7 +17,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = template({ + ctx.state.data = { title: '简书 7 日热门', link: 'https://www.jianshu.com/trending/weekly', description: '简书 7 日热门', @@ -31,5 +30,5 @@ module.exports = async (ctx) => { link: `https://www.jianshu.com${item.find('.title').attr('href')}` }; }).get(), - }); + }; }; \ No newline at end of file diff --git a/routes/juejin/category.js b/routes/juejin/category.js index efbf75903..a123fb0b3 100644 --- a/routes/juejin/category.js +++ b/routes/juejin/category.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -32,7 +31,7 @@ module.exports = async (ctx) => { const data = response.data; - ctx.body = template({ + ctx.state.data = { title: `掘金${cat.name}`, link: `https://juejin.im/welcome/${category}`, description: `掘金${cat.name}`, @@ -42,5 +41,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.createdAt).toUTCString(), link: item.originalUrl })), - }); + }; }; \ No newline at end of file diff --git a/routes/mzitu/category.js b/routes/mzitu/category.js index 90f2a7ad6..10eb14a73 100644 --- a/routes/mzitu/category.js +++ b/routes/mzitu/category.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -23,7 +22,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('#pins li'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -38,5 +37,5 @@ module.exports = async (ctx) => { link: linkA.attr('href') }; }).get(), - }); + }; }; diff --git a/routes/mzitu/post.js b/routes/mzitu/post.js index a8de2435b..25d97ca41 100644 --- a/routes/mzitu/post.js +++ b/routes/mzitu/post.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -49,7 +48,7 @@ module.exports = async (ctx) => { const reg = /[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d/; - ctx.body = template({ + ctx.state.data = { title: title, link: url, description: $('meta[name="description"]').attr('content') || title, @@ -67,5 +66,5 @@ module.exports = async (ctx) => { link: item.url }; }) - }); + }; }; diff --git a/routes/mzitu/tag.js b/routes/mzitu/tag.js index 290751258..46be706d4 100644 --- a/routes/mzitu/tag.js +++ b/routes/mzitu/tag.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -22,7 +21,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('#pins li'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -37,5 +36,5 @@ module.exports = async (ctx) => { link: linkA.attr('href') }; }).get(), - }); + }; }; diff --git a/routes/mzitu/tags.js b/routes/mzitu/tags.js index c29a9072a..a194f1304 100644 --- a/routes/mzitu/tags.js +++ b/routes/mzitu/tags.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -20,7 +19,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.main-content > div.postlist > dl > dd'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), @@ -35,5 +34,5 @@ module.exports = async (ctx) => { link: linkA.attr('href') }; }).get(), - }); + }; }; diff --git a/routes/ncm/artist.js b/routes/ncm/artist.js index 657b53e23..9704e97f7 100644 --- a/routes/ncm/artist.js +++ b/routes/ncm/artist.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -16,7 +15,7 @@ module.exports = async (ctx) => { const data = response.data; - ctx.body = template({ + ctx.state.data = { title: data.artist.name, link: `https://music.163.com/#/artist/album?id=${id}`, description: `网易云音乐歌手专辑 - ${data.artist.name}`, @@ -28,5 +27,5 @@ module.exports = async (ctx) => { link: `https://music.163.com/#/album?id=${item.id}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/ncm/playlist.js b/routes/ncm/playlist.js index 555aed878..4534880f0 100644 --- a/routes/ncm/playlist.js +++ b/routes/ncm/playlist.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -21,7 +20,7 @@ module.exports = async (ctx) => { const data = response.data.playlist; - ctx.body = template({ + ctx.state.data = { title: data.name, link: `https://music.163.com/#/playlist?id=${id}`, description: `网易云音乐歌单 - ${data.name}`, @@ -33,5 +32,5 @@ module.exports = async (ctx) => { link: `https://music.163.com/#/song?id=${item.id}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/ncm/userplaylist.js b/routes/ncm/userplaylist.js index af2f3e97d..865661bb7 100644 --- a/routes/ncm/userplaylist.js +++ b/routes/ncm/userplaylist.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -27,7 +26,7 @@ module.exports = async (ctx) => { const { nickname, signature } = creator; - ctx.body = template({ + ctx.state.data = { title: `${nickname} 的所有歌单`, link: `http://music.163.com/user/home?id=${uid}`, description: signature, @@ -37,5 +36,5 @@ module.exports = async (ctx) => { pubDate: new Date(pl.createTime).toUTCString(), link: `http://music.163.com/playlist?id=${pl.id}` })) - }); + }; }; \ No newline at end of file diff --git a/routes/pixiv/bookmarks.js b/routes/pixiv/bookmarks.js index 8b2ff56e7..bc9dcc213 100644 --- a/routes/pixiv/bookmarks.js +++ b/routes/pixiv/bookmarks.js @@ -7,7 +7,6 @@ if (!pixivConfig) { return; } -const template = require('../../utils/template'); const getToken = require('./token'); const getBookmarks = require('./api/getBookmarks'); const getUserDetail = require('./api/getUserDetail'); @@ -28,7 +27,7 @@ module.exports = async (ctx) => { const illusts = bookmarksResponse.data.illusts; const username = userDetailResponse.data.user.name - ctx.body = template({ + ctx.state.data = { title: `${username} 的收藏`, link: `https://www.pixiv.net/bookmark.php?id=${id}`, description: `${username} 的 pixiv 最新收藏`, @@ -47,5 +46,5 @@ module.exports = async (ctx) => { link: `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${illust.id}` }; }) - }); + }; }; \ No newline at end of file diff --git a/routes/pixiv/ranking.js b/routes/pixiv/ranking.js index f6eed3387..7e4a4d0ac 100644 --- a/routes/pixiv/ranking.js +++ b/routes/pixiv/ranking.js @@ -8,7 +8,6 @@ if (!pixivConfig) { return; } -const template = require('../../utils/template'); const getToken = require('./token'); const getRanking = require('./api/getRanking'); @@ -57,7 +56,7 @@ module.exports = async (ctx) => { const dateStr = `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 `; - ctx.body = template({ + ctx.state.data = { title: (ctx.params.date ? dateStr : '') + titles[mode], link: links[mode], description: dateStr + titles[mode], @@ -76,5 +75,5 @@ module.exports = async (ctx) => { link: `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${illust.id}` }; }) - }); + }; }; \ No newline at end of file diff --git a/routes/pixiv/user.js b/routes/pixiv/user.js index 644bb2ce4..948ee99d7 100644 --- a/routes/pixiv/user.js +++ b/routes/pixiv/user.js @@ -8,7 +8,6 @@ if (!pixivConfig) { return; } -const template = require('../../utils/template'); const getToken = require('./token'); const getIllusts = require('./api/getIllusts'); @@ -25,7 +24,7 @@ module.exports = async (ctx) => { const illusts = response.data.illusts; const username = illusts[0].user.name - ctx.body = template({ + ctx.state.data = { title: `${username} 的 pixiv 动态`, link: `https://www.pixiv.net/member.php?id=${id}`, description: `${username} 的 pixiv 最新动态`, @@ -44,5 +43,5 @@ module.exports = async (ctx) => { link: `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${illust.id}` }; }) - }); + }; }; \ No newline at end of file diff --git a/routes/tieba/forum.js b/routes/tieba/forum.js index e2f41ed8f..7f68e8598 100644 --- a/routes/tieba/forum.js +++ b/routes/tieba/forum.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -54,7 +53,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(threadListHTML); const list = $('#thread_list > .j_thread_list[data-field]'); - ctx.body = template({ + ctx.state.data = { title: `${kw}吧`, link: `https://tieba.baidu.com/f?kw=${encodeURIComponent(kw)}`, item: @@ -83,5 +82,5 @@ module.exports = async (ctx) => { }; }). get(), - }); + }; }; diff --git a/routes/toutiao/today.js b/routes/toutiao/today.js index e2fb65b1a..cd205dfaa 100644 --- a/routes/toutiao/today.js +++ b/routes/toutiao/today.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -27,9 +26,9 @@ module.exports = async (ctx)=>{ }; article_item.push(item); }; - ctx.body = template({ + ctx.state.data = { title: '开发者头条:' + title, link: baseUrl, item: article_item, - }); + }; }; diff --git a/routes/toutiao/user.js b/routes/toutiao/user.js index 2b81de04d..82e69002f 100644 --- a/routes/toutiao/user.js +++ b/routes/toutiao/user.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -32,11 +31,11 @@ module.exports = async (ctx)=>{ }; article_item.push(item); }; - ctx.body = template({ + ctx.state.data = { title: '开发者头条独家号:'+title, description:description, image: image, link: baseUrl, item: article_item, - }); + }; }; diff --git a/routes/tucaoqq/post.js b/routes/tucaoqq/post.js index 17521b1ff..ca4838468 100644 --- a/routes/tucaoqq/post.js +++ b/routes/tucaoqq/post.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); const md5 = require('../../utils/md5'); @@ -19,7 +18,7 @@ module.exports = async (ctx) => { }); const data = response.data.data; - ctx.body = template({ + ctx.state.data = { title: `${projectID} 的 吐个槽新帖`, link: `https://support.qq.com/product/${projectID}`, description: `${projectID} 的 吐个槽新帖`, @@ -38,5 +37,5 @@ module.exports = async (ctx) => { link: `https://support.qq.com/products/${projectID}` }; }), - }); + }; }; diff --git a/routes/twitter/user.js b/routes/twitter/user.js index 0e63cad45..9ea6f8f32 100644 --- a/routes/twitter/user.js +++ b/routes/twitter/user.js @@ -1,4 +1,3 @@ -const template = require('../../utils/template'); const Twit = require('twit'); const config = require('../../config'); @@ -13,7 +12,7 @@ module.exports = async (ctx) => { const data = result.data; - ctx.body = template({ + ctx.state.data = { title: `${data[0].user.name} 的 Twitter`, link: `https://twitter.com/${id}/`, description: data[0].user.description, @@ -23,5 +22,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.createdTime).toUTCString(), link: `https://twitter.com/${id}/status/${item.id_str}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/weibo/keyword.js b/routes/weibo/keyword.js index 7a02a5700..2604214b9 100644 --- a/routes/weibo/keyword.js +++ b/routes/weibo/keyword.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); const weiboUtils = require('./utils'); @@ -16,7 +15,7 @@ module.exports = async (ctx) => { }); const data = response.data.data.cards[0].card_group; - ctx.body = template({ + ctx.state.data = { title: `又有人在微博提到${keyword}了`, link: `http://s.weibo.com/weibo/${encodeURIComponent(keyword)}&b=1&nodup=1`, description: `又有人在微博提到${keyword}了`, @@ -29,5 +28,5 @@ module.exports = async (ctx) => { link: `https://weibo.com/${item.mblog.user.id}/${item.mblog.bid}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/weibo/user.js b/routes/weibo/user.js index f35ec9a20..3dd528f41 100644 --- a/routes/weibo/user.js +++ b/routes/weibo/user.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); const weiboUtils = require('./utils'); @@ -26,7 +25,7 @@ module.exports = async (ctx) => { } }); - ctx.body = template({ + ctx.state.data = { title: `${name}的微博`, link: `http://weibo.com/${uid}/`, description: `${name}的微博`, @@ -39,5 +38,5 @@ module.exports = async (ctx) => { link: `https://weibo.com/${uid}/${item.mblog.bid}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/youtube/channel.js b/routes/youtube/channel.js index c5e78bd95..316dcf9c1 100644 --- a/routes/youtube/channel.js +++ b/routes/youtube/channel.js @@ -1,6 +1,5 @@ const { google } = require('googleapis'); const config = require('../../config'); -const template = require('../../utils/template'); const youtube = google.youtube({ version: 'v3', @@ -22,7 +21,7 @@ module.exports = async (ctx) => { }); const data = responst.data.items; - ctx.body = template({ + ctx.state.data = { title: `${data[0].snippet.channelTitle} 的 Youtube 视频`, link: `https://www.youtube.com/channel/${id}`, description: `${data[0].snippet.channelTitle} 的 Youtube 视频`, @@ -36,5 +35,5 @@ module.exports = async (ctx) => { link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/youtube/user.js b/routes/youtube/user.js index eb5eee1f6..ad8261939 100644 --- a/routes/youtube/user.js +++ b/routes/youtube/user.js @@ -1,6 +1,5 @@ const { google } = require('googleapis'); const config = require('../../config'); -const template = require('../../utils/template'); const youtube = google.youtube({ version: 'v3', @@ -22,7 +21,7 @@ module.exports = async (ctx) => { }); const data = responst.data.items; - ctx.body = template({ + ctx.state.data = { title: `${username} 的 Youtube 视频`, link: `https://www.youtube.com/user/${username}`, description: `${username} 的 Youtube 视频`, @@ -36,5 +35,5 @@ module.exports = async (ctx) => { link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}` }; }), - }); + }; }; \ No newline at end of file diff --git a/routes/zhihu/activities.js b/routes/zhihu/activities.js index f9e869868..034a807f3 100644 --- a/routes/zhihu/activities.js +++ b/routes/zhihu/activities.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -18,7 +17,7 @@ module.exports = async (ctx) => { const data = response.data.data; - ctx.body = template({ + ctx.state.data = { title: `${data[0].actor.name}的知乎动态`, link: `https://www.zhihu.com/people/${id}/activities`, description: data[0].actor.headline || data[0].actor.description, @@ -75,5 +74,5 @@ module.exports = async (ctx) => { link: url }; }), - }); + }; }; diff --git a/routes/zhihu/answers.js b/routes/zhihu/answers.js index 288c55be8..a4d16c8a9 100644 --- a/routes/zhihu/answers.js +++ b/routes/zhihu/answers.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -18,7 +17,7 @@ module.exports = async (ctx) => { const data = response.data.data; - ctx.body = template({ + ctx.state.data = { title: `${data[0].author.name}的知乎回答`, link: `https://www.zhihu.com/people/${id}/answers`, description: data[0].author.headline || data[0].author.description, @@ -28,5 +27,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.created_time * 1000).toUTCString(), link: `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}` })), - }); + }; }; diff --git a/routes/zhihu/collection.js b/routes/zhihu/collection.js index 2f0dbecfa..6e2cb7fab 100644 --- a/routes/zhihu/collection.js +++ b/routes/zhihu/collection.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -19,7 +18,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('div.zm-item'); - ctx.body = template({ + ctx.state.data = { title: $('title').text(), link: `https://www.zhihu.com/collection/${id}`, description: `${$('#zh-fav-head-description').text()}`, @@ -33,5 +32,5 @@ module.exports = async (ctx) => { link: linkUrl }; }).get(), - }); + }; }; diff --git a/routes/zhihu/zhuanlan.js b/routes/zhihu/zhuanlan.js index 549c68781..89f3be862 100644 --- a/routes/zhihu/zhuanlan.js +++ b/routes/zhihu/zhuanlan.js @@ -1,5 +1,4 @@ const axios = require('axios'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -25,7 +24,7 @@ module.exports = async (ctx) => { const list = listRes.data; const info = infoRes.data; - ctx.body = template({ + ctx.state.data = { title: `知乎专栏-${info.name}`, link: `https://zhuanlan.zhihu.com/${id}`, description: info.description, @@ -35,5 +34,5 @@ module.exports = async (ctx) => { pubDate: new Date(item.publishedTime).toUTCString(), link: `https://zhuanlan.zhihu.com${item.url}` })), - }); + }; }; \ No newline at end of file diff --git a/routes/ziroom/room.js b/routes/ziroom/room.js index d5ea0c99f..ce2ac2347 100644 --- a/routes/ziroom/room.js +++ b/routes/ziroom/room.js @@ -1,6 +1,5 @@ const axios = require('axios'); const qs = require('querystring'); -const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -30,7 +29,7 @@ module.exports = async (ctx) => { //判断数据的类型,如果有数据就是数组类型的,没有数据的话,就赋值为空数组 data = (data instanceof Array) ? data : [{title:"我们找不到任何与您的搜索条件匹配的结果,但是调整您的搜索条件可能会有所帮助",room_name:"",list_img:"",city:"",id:""}]; - ctx.body = template({ + ctx.state.data = { title: `自如的${keyword}${iswhole ? '整租' : '合租'}${room}室房源`, link: `http://${domain}`, description: `自如的${keyword}${iswhole ? '整租' : '合租'}${room}室房源`, @@ -39,5 +38,5 @@ module.exports = async (ctx) => { description: `${item.room_name}`, link: `http://${domain}/${city.toUpperCase()}/room/${item.id}.html` })), - }); + }; };