From 4a71dc8af33e737512fc6b6e3bad10a242db64b2 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 6 May 2018 16:07:05 +0800 Subject: [PATCH] template module --- README.md | 2 +- docs/joinus/README.md | 4 ++-- routes/bilibili/bangumi.js | 6 ++---- routes/bilibili/coin.js | 6 ++---- routes/bilibili/dynamic.js | 6 ++---- routes/bilibili/fav.js | 7 +++---- routes/bilibili/followers.js | 6 ++---- routes/bilibili/followings.js | 6 ++---- routes/bilibili/partion.js | 6 ++---- routes/bilibili/video.js | 6 ++---- routes/biquge/chapter.js | 6 ++---- routes/dapenti/tugua.js | 6 ++---- routes/dockone/weekly.js | 6 ++---- routes/douban/later.js | 6 ++---- routes/douban/playing.js | 6 ++---- routes/douban/ustop.js | 6 ++---- routes/express/express.js | 6 ++---- routes/jandan/pic.js | 6 ++---- routes/jianshu/collection.js | 6 ++---- routes/jianshu/home.js | 6 ++---- routes/jianshu/monthly.js | 6 ++---- routes/jianshu/user.js | 6 ++---- routes/jianshu/weekly.js | 6 ++---- routes/juejin/category.js | 6 ++---- routes/mzitu/category.js | 6 ++---- routes/mzitu/post.js | 6 ++---- routes/mzitu/tag.js | 6 ++---- routes/mzitu/tags.js | 6 ++---- routes/ncm/artist.js | 6 ++---- routes/ncm/playlist.js | 6 ++---- routes/ncm/userplaylist.js | 6 ++---- routes/pixiv/ranking.js | 6 ++---- routes/pixiv/user.js | 6 ++---- routes/tieba/forum.js | 6 ++---- routes/tucaoqq/post.js | 6 ++---- routes/weibo/user.js | 6 ++---- routes/zhihu/activities.js | 6 ++---- routes/zhihu/collection.js | 6 ++---- routes/zhihu/zhuanlan.js | 6 ++---- routes/ziroom/room.js | 6 ++---- utils/template.js | 11 +++++++++++ views/rss.art | 2 +- 42 files changed, 92 insertions(+), 156 deletions(-) create mode 100644 utils/template.js diff --git a/README.md b/README.md index 2410a0eba..1315f9b3f 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 1. 在 [/routes/](https://github.com/DIYgod/RSSHub/tree/master/routes) 中的路由对应路径添加获取 RSS 内容的脚本 -1. 更新文档 [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) +1. 更新 README 和文档: [/README.md](https://github.com/DIYgod/RSSHub/blob/master/README.md) [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) ### 参与讨论 diff --git a/docs/joinus/README.md b/docs/joinus/README.md index b00a400af..8f3f39494 100644 --- a/docs/joinus/README.md +++ b/docs/joinus/README.md @@ -8,8 +8,8 @@ 1. 在 [/routes/](https://github.com/DIYgod/RSSHub/tree/master/routes) 中的路由对应路径添加获取 RSS 内容的脚本 -1. 更新文档 [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) +1. 更新 README 和文档: [/README.md](https://github.com/DIYgod/RSSHub/blob/master/README.md) [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) ## 参与讨论 -- [Telegram 群](https://t.me/rsshub) \ No newline at end of file +1. [Telegram 群](https://t.me/rsshub) \ No newline at end of file diff --git a/routes/bilibili/bangumi.js b/routes/bilibili/bangumi.js index c57559b98..030cd5ed6 100644 --- a/routes/bilibili/bangumi.js +++ b/routes/bilibili/bangumi.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -17,11 +16,10 @@ module.exports = async (ctx) => { const data = JSON.parse(response.data.match(/^seasonListCallback\((.*)\);$/)[1]).result || {}; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: data.title, link: `https://bangumi.bilibili.com/anime/${seasonid}/`, description: data.evaluate, - lastBuildDate: new Date().toUTCString(), item: data.episodes && data.episodes.map((item) => ({ title: `第${item.index}话 ${item.index_title}`, description: `更新时间:${item.update_time}`, diff --git a/routes/bilibili/coin.js b/routes/bilibili/coin.js index f06902b0e..7c7b7005e 100644 --- a/routes/bilibili/coin.js +++ b/routes/bilibili/coin.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -31,11 +30,10 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name} 的 bilibili 投币视频`, link: `https://space.bilibili.com/${uid}`, description: `${name} 的 bilibili 投币视频`, - lastBuildDate: new Date().toUTCString(), item: data.data && data.data.list && data.data.list.map((item) => ({ title: item.title, description: `${item.title}
`, diff --git a/routes/bilibili/dynamic.js b/routes/bilibili/dynamic.js index a922631b6..5cb6b2470 100644 --- a/routes/bilibili/dynamic.js +++ b/routes/bilibili/dynamic.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const JSONbig = require('json-bigint'); const config = require('../../config'); @@ -17,11 +16,10 @@ module.exports = async (ctx) => { }); const data = response.data.data.cards; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ 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 动态`, - lastBuildDate: new Date().toUTCString(), item: data.map((item) => { const parsed = JSONbig.parse(item.card); const data = parsed.item || parsed; diff --git a/routes/bilibili/fav.js b/routes/bilibili/fav.js index 94f769823..ce77ef9d7 100644 --- a/routes/bilibili/fav.js +++ b/routes/bilibili/fav.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -31,11 +30,11 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name} 的 bilibili 收藏夹`, link: `https://space.bilibili.com/${uid}/#/favlist`, description: `${name} 的 bilibili 收藏夹`, - lastBuildDate: new Date().toUTCString(), + item: data.data && data.data.archives && data.data.archives.map((item) => ({ title: item.title, description: `${item.desc}
`, diff --git a/routes/bilibili/followers.js b/routes/bilibili/followers.js index 5626f03bf..6f0a29ede 100644 --- a/routes/bilibili/followers.js +++ b/routes/bilibili/followers.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -41,11 +40,10 @@ module.exports = async (ctx) => { }); const data = response.data.data.list; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name} 的 bilibili 粉丝`, link: `https://space.bilibili.com/${uid}/#/fans/fans`, description: `${name} 的 bilibili 粉丝`, - lastBuildDate: new Date().toUTCString(), item: data.map((item) => ({ title: `${name} 新粉丝 ${item.uname}`, description: `${item.uname}
${item.sign}
总计${count}`, diff --git a/routes/bilibili/followings.js b/routes/bilibili/followings.js index e978773bc..7683255cc 100644 --- a/routes/bilibili/followings.js +++ b/routes/bilibili/followings.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -41,11 +40,10 @@ module.exports = async (ctx) => { }); const data = response.data.data.list; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name} 的 bilibili 关注`, link: `https://space.bilibili.com/${uid}/#/fans/follow`, description: `${name} 的 bilibili 关注`, - lastBuildDate: new Date().toUTCString(), item: data.map((item) => ({ title: `${name} 新关注 ${item.uname}`, description: `${item.uname}
${item.sign}
总计${count}`, diff --git a/routes/bilibili/partion.js b/routes/bilibili/partion.js index e589e921a..c9e68998f 100644 --- a/routes/bilibili/partion.js +++ b/routes/bilibili/partion.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -21,11 +20,10 @@ module.exports = async (ctx) => { name = list[0].tname; } - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `bilibili ${name}分区`, link: 'https://www.bilibili.com', description: `bilibili ${name}分区`, - lastBuildDate: new Date().toUTCString(), item: list && list.map((item) => ({ title: `${item.title} - ${item.owner.name}`, description: `${item.desc}`, diff --git a/routes/bilibili/video.js b/routes/bilibili/video.js index 7245fe47a..de79faed6 100644 --- a/routes/bilibili/video.js +++ b/routes/bilibili/video.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -31,11 +30,10 @@ module.exports = async (ctx) => { }); const data = response.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name} 的 bilibili 空间`, link: `https://space.bilibili.com/${uid}`, description: `${name} 的 bilibili 空间`, - lastBuildDate: new Date().toUTCString(), item: data.data && data.data.vlist && data.data.vlist.map((item) => ({ title: item.title, description: `${item.description}
`, diff --git a/routes/biquge/chapter.js b/routes/biquge/chapter.js index 0d45c436b..7cc29b265 100644 --- a/routes/biquge/chapter.js +++ b/routes/biquge/chapter.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); const iconv = require('iconv-lite'); @@ -38,12 +37,11 @@ module.exports = async (ctx) => { chapter_item.push(item); } // console.log('chapter_item',chapter_item) - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `笔趣阁 ${title}`, link: `${baseUrl}${id}/`, image: cover_url, description: description, - lastBuildDate: new Date().toUTCString(), item: chapter_item, }); }; diff --git a/routes/dapenti/tugua.js b/routes/dapenti/tugua.js index 94da8deac..1b49c491f 100644 --- a/routes/dapenti/tugua.js +++ b/routes/dapenti/tugua.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); const iconv = require('iconv-lite'); @@ -47,11 +46,10 @@ module.exports = async (ctx) => { result_item.push(item) } - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `喷嚏图卦`, link: `https://www.dapenti.com/blog/blog.asp?name=xilei&subjectid=70`, description: `喷嚏图卦`, - lastBuildDate: new Date().toUTCString(), item: result_item, }); }; diff --git a/routes/dockone/weekly.js b/routes/dockone/weekly.js index e1cb23bb9..f150e9a99 100644 --- a/routes/dockone/weekly.js +++ b/routes/dockone/weekly.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -27,11 +26,10 @@ module.exports = async (ctx) => { const list = $('ul.i.issues>li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((item,index) => { item=$(index); return { diff --git a/routes/douban/later.js b/routes/douban/later.js index 26adc5072..546434a20 100644 --- a/routes/douban/later.js +++ b/routes/douban/later.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); module.exports = async (ctx) => { const response = await axios({ @@ -9,10 +8,9 @@ module.exports = async (ctx) => { }); const movieList = response.data.subjects; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '即将上映的电影', link: 'https://movie.douban.com/cinema/later/', - lastBuildDate: new Date().toUTCString(), item: movieList.map((item) => ({ title: item.title, description: `标题:${item.title}
影片类型:${item.genres.join(' | ')}
评分:${item.rating.stars === '00' ? '无' : item.rating.average}
`, diff --git a/routes/douban/playing.js b/routes/douban/playing.js index 8735a1f0e..f82f9e694 100644 --- a/routes/douban/playing.js +++ b/routes/douban/playing.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); module.exports = async (ctx) => { const city = ctx.params.city; @@ -12,10 +11,9 @@ module.exports = async (ctx) => { }); const movieList = score ? response.data.subjects.filter((item) => item.rating.average >= score) : response.data.subjects; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${city ? city : ''}正在上映的${score ? `超过 ${score} 分的` : ''}电影`, link: 'https://movie.douban.com/cinema/nowplaying/', - lastBuildDate: new Date().toUTCString(), item: movieList.map((item) => ({ title: item.title, description: `标题:${item.title}
影片类型:${item.genres.join(' | ')}
评分:${item.rating.average}
`, diff --git a/routes/douban/ustop.js b/routes/douban/ustop.js index 2f635206f..e0f7655ac 100644 --- a/routes/douban/ustop.js +++ b/routes/douban/ustop.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); module.exports = async (ctx) => { const response = await axios({ @@ -9,10 +8,9 @@ module.exports = async (ctx) => { }); const movieList = response.data.subjects; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '豆瓣电影北美票房榜', link: 'https://movie.douban.com/chart', - lastBuildDate: new Date().toUTCString(), item: movieList.map((item) => { item = item.subject; return { diff --git a/routes/express/express.js b/routes/express/express.js index 0ed128fc2..893aab278 100644 --- a/routes/express/express.js +++ b/routes/express/express.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -18,11 +17,10 @@ module.exports = async (ctx) => { const data = response.data.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `快递 ${company}-${number}`, link: 'https://www.kuaidi100.com', description: `快递 ${company}-${number}`, - lastBuildDate: new Date().toUTCString(), item: data.map((item) => ({ title: item.context, description: item.context, diff --git a/routes/jandan/pic.js b/routes/jandan/pic.js index 0c9dbf01a..6bdc164b7 100644 --- a/routes/jandan/pic.js +++ b/routes/jandan/pic.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const crypto = require('crypto'); const config = require('../../config'); @@ -122,11 +121,10 @@ module.exports = async (ctx) => { }); }); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '煎蛋无聊图', link: 'http://jandan.net/pic', description: '煎蛋官方无聊图,无限活力的热门图区。', - lastBuildDate: new Date().toUTCString(), item: items }); }; diff --git a/routes/jianshu/collection.js b/routes/jianshu/collection.js index b288f3b96..91eacdf12 100644 --- a/routes/jianshu/collection.js +++ b/routes/jianshu/collection.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -21,11 +20,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: `https://www.jianshu.com/c/${id}`, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); return { diff --git a/routes/jianshu/home.js b/routes/jianshu/home.js index 720fe4167..dbb15e36b 100644 --- a/routes/jianshu/home.js +++ b/routes/jianshu/home.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -19,11 +18,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '简书首页', link: 'https://www.jianshu.com', description: $('meta[name="description"]').attr('content'), - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); return { diff --git a/routes/jianshu/monthly.js b/routes/jianshu/monthly.js index 621c81f01..8b1e1aef7 100644 --- a/routes/jianshu/monthly.js +++ b/routes/jianshu/monthly.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -19,11 +18,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '简书 30 日热门', link: 'https://www.jianshu.com/trending/monthly', description: '简书 30 日热门', - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); return { diff --git a/routes/jianshu/user.js b/routes/jianshu/user.js index 94ff5fcd1..7dc4b842b 100644 --- a/routes/jianshu/user.js +++ b/routes/jianshu/user.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -21,11 +20,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: `https://www.jianshu.com/u/${id}`, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); return { diff --git a/routes/jianshu/weekly.js b/routes/jianshu/weekly.js index 1fcf2d890..1edcb182e 100644 --- a/routes/jianshu/weekly.js +++ b/routes/jianshu/weekly.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -19,11 +18,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.note-list li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: '简书 7 日热门', link: 'https://www.jianshu.com/trending/weekly', description: '简书 7 日热门', - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); return { diff --git a/routes/juejin/category.js b/routes/juejin/category.js index 082a2a92d..efbf75903 100644 --- a/routes/juejin/category.js +++ b/routes/juejin/category.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -33,11 +32,10 @@ module.exports = async (ctx) => { const data = response.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `掘金${cat.name}`, link: `https://juejin.im/welcome/${category}`, description: `掘金${cat.name}`, - lastBuildDate: new Date().toUTCString(), item: data.d && data.d.entrylist && data.d.entrylist.map((item) => ({ title: item.title, description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`, diff --git a/routes/mzitu/category.js b/routes/mzitu/category.js index cb85367ba..90f2a7ad6 100644 --- a/routes/mzitu/category.js +++ b/routes/mzitu/category.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -24,11 +23,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('#pins li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((item, index) => { item = $(index); const linkA = item.find('a'); diff --git a/routes/mzitu/post.js b/routes/mzitu/post.js index 8a0262e64..a8de2435b 100644 --- a/routes/mzitu/post.js +++ b/routes/mzitu/post.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -50,11 +49,10 @@ 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 = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: title, link: url, description: $('meta[name="description"]').attr('content') || title, - lastBuildDate: new Date().toUTCString(), item: list && list.map((item) => { let date = new Date(); const dateStr = $('.main-meta > span:nth-child(2)').text(); diff --git a/routes/mzitu/tag.js b/routes/mzitu/tag.js index ea52b6871..290751258 100644 --- a/routes/mzitu/tag.js +++ b/routes/mzitu/tag.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -23,11 +22,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('#pins li'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((item, index) => { item = $(index); const linkA = item.find('a'); diff --git a/routes/mzitu/tags.js b/routes/mzitu/tags.js index 7ddb752c6..c29a9072a 100644 --- a/routes/mzitu/tags.js +++ b/routes/mzitu/tags.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -21,11 +20,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.main-content > div.postlist > dl > dd'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: url, description: $('meta[name="description"]').attr('content') || $('title').text(), - lastBuildDate: new Date().toUTCString(), item: list && list.map((item, index) => { item = $(index); const linkA = item.find('a'); diff --git a/routes/ncm/artist.js b/routes/ncm/artist.js index 1a8b7c534..657b53e23 100644 --- a/routes/ncm/artist.js +++ b/routes/ncm/artist.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -17,11 +16,10 @@ module.exports = async (ctx) => { const data = response.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: data.artist.name, link: `https://music.163.com/#/artist/album?id=${id}`, description: `网易云音乐歌手专辑 - ${data.artist.name}`, - lastBuildDate: new Date().toUTCString(), item: data.hotAlbums.map((item) => { const singer = item.artists.length === 1 ? item.artists[0].name : item.artists.reduce((prev, cur) => (prev.name || prev) + '/' + cur.name); return { diff --git a/routes/ncm/playlist.js b/routes/ncm/playlist.js index 663b50be9..555aed878 100644 --- a/routes/ncm/playlist.js +++ b/routes/ncm/playlist.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -22,11 +21,10 @@ module.exports = async (ctx) => { const data = response.data.playlist; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: data.name, link: `https://music.163.com/#/playlist?id=${id}`, description: `网易云音乐歌单 - ${data.name}`, - lastBuildDate: new Date().toUTCString(), item: data.tracks && data.tracks.map((item) => { const singer = item.ar.length === 1 ? item.ar[0].name : item.ar.reduce((prev, cur) => (prev.name || prev) + '/' + cur.name); return { diff --git a/routes/ncm/userplaylist.js b/routes/ncm/userplaylist.js index 0c0fc716c..af2f3e97d 100644 --- a/routes/ncm/userplaylist.js +++ b/routes/ncm/userplaylist.js @@ -1,7 +1,6 @@ const axios = require('axios'); const qs = require('querystring'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -28,11 +27,10 @@ module.exports = async (ctx) => { const { nickname, signature } = creator; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${nickname} 的所有歌单`, link: `http://music.163.com/user/home?id=${uid}`, description: signature, - lastBuildDate: new Date().toUTCString(), item: playlist.map((pl) => ({ title: pl.name, description: pl.description, diff --git a/routes/pixiv/ranking.js b/routes/pixiv/ranking.js index 24566d2cb..6bee576c4 100644 --- a/routes/pixiv/ranking.js +++ b/routes/pixiv/ranking.js @@ -9,8 +9,7 @@ if (!pixivConfig) { } const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const FormData = require('form-data'); const getToken = require('./token'); const maskHeader = require('./constants').maskHeader; @@ -82,11 +81,10 @@ module.exports = async (ctx) => { const dateStr = `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 `; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: (ctx.params.date ? dateStr : '') + titles[mode], link: links[mode], description: dateStr + titles[mode], - lastBuildDate: new Date().toUTCString(), item: illusts.map((illust, index) => { const images = []; if (illust.page_count === 1) { diff --git a/routes/pixiv/user.js b/routes/pixiv/user.js index b76d426c9..b13bf7d19 100644 --- a/routes/pixiv/user.js +++ b/routes/pixiv/user.js @@ -9,8 +9,7 @@ if (!pixivConfig) { } const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const FormData = require('form-data'); const getToken = require('./token'); const maskHeader = require('./constants').maskHeader; @@ -40,11 +39,10 @@ module.exports = async (ctx) => { const illusts = response.data.illusts; const username = illusts[0].user.name - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${username} 的 pixiv 动态`, link: `https://www.pixiv.net/member.php?id=${id}`, description: `${username} 的 pixiv 最新动态`, - lastBuildDate: new Date().toUTCString(), item: illusts.map((illust) => { const images = []; if (illust.page_count === 1) { diff --git a/routes/tieba/forum.js b/routes/tieba/forum.js index 4ecaed97d..e2f41ed8f 100644 --- a/routes/tieba/forum.js +++ b/routes/tieba/forum.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -55,10 +54,9 @@ module.exports = async (ctx) => { const $ = cheerio.load(threadListHTML); const list = $('#thread_list > .j_thread_list[data-field]'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${kw}吧`, link: `https://tieba.baidu.com/f?kw=${encodeURIComponent(kw)}`, - lastBuildDate: new Date().toUTCString(), item: list && list. diff --git a/routes/tucaoqq/post.js b/routes/tucaoqq/post.js index 8354bc04d..ea5cc1066 100644 --- a/routes/tucaoqq/post.js +++ b/routes/tucaoqq/post.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); const md5 = require('js-md5'); @@ -19,11 +18,10 @@ module.exports = async (ctx) => { }); const data = response.data.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${projectID} 的 吐个槽新帖`, link: `https://support.qq.com/product/${projectID}`, description: `${projectID} 的 吐个槽新帖`, - lastBuildDate: new Date().toUTCString(), item: data.map((item) => { let pubdate = new Date(item.created_at.replace(' ', 'T') + "+08:00"); let imgHTML = ''; diff --git a/routes/weibo/user.js b/routes/weibo/user.js index ed5129960..1bbe35988 100644 --- a/routes/weibo/user.js +++ b/routes/weibo/user.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); // 格式化每条微博的HTML @@ -92,11 +91,10 @@ module.exports = async (ctx) => { } }); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name}的微博`, link: `http://weibo.com/${uid}/`, description: `${name}的微博`, - lastBuildDate: new Date().toUTCString(), item: response.data.data.cards.filter((item) => item.mblog && !item.mblog.isTop).map((item) => { const title = item.mblog.text.replace(/<.*?>/g, ''); return { diff --git a/routes/zhihu/activities.js b/routes/zhihu/activities.js index 5caeaf33c..055c13e49 100644 --- a/routes/zhihu/activities.js +++ b/routes/zhihu/activities.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -24,11 +23,10 @@ module.exports = async (ctx) => { const name = data.users[id].name; const list = Object.values(data.activities).sort((a, b) => b.createdTime - a.createdTime); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `${name}的知乎动态`, link: `https://www.zhihu.com/people/${id}/activities`, description: data.users[id].headline || data.users[id].description, - lastBuildDate: new Date().toUTCString(), item: list && list.map((item) => { const type = item.target.schema; const detail = data[`${type}s`][item.target.id]; diff --git a/routes/zhihu/collection.js b/routes/zhihu/collection.js index c3375b48a..fccd22fff 100644 --- a/routes/zhihu/collection.js +++ b/routes/zhihu/collection.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const cheerio = require('cheerio'); const config = require('../../config'); @@ -20,11 +19,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('div.zm-item'); - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: $('title').text(), link: `https://www.zhihu.com/collection/${id}`, description: `${$('#zh-fav-head-description').text()}`, - lastBuildDate: new Date().toUTCString(), item: list && list.map((index, item) => { item = $(item); let linkUrl = item.find('.zm-item-title a').attr('href'); diff --git a/routes/zhihu/zhuanlan.js b/routes/zhihu/zhuanlan.js index e77e9c887..549c68781 100644 --- a/routes/zhihu/zhuanlan.js +++ b/routes/zhihu/zhuanlan.js @@ -1,6 +1,5 @@ const axios = require('axios'); -const art = require('art-template'); -const path = require('path'); +const template = require('../../utils/template'); const config = require('../../config'); module.exports = async (ctx) => { @@ -26,11 +25,10 @@ module.exports = async (ctx) => { const list = listRes.data; const info = infoRes.data; - ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + ctx.body = template({ title: `知乎专栏-${info.name}`, link: `https://zhuanlan.zhihu.com/${id}`, description: info.description, - lastBuildDate: new Date().toUTCString(), item: list.map((item) => ({ title: item.title, description: item.content.replace(/ ({ title: item.title, description: `${item.room_name}`, diff --git a/utils/template.js b/utils/template.js new file mode 100644 index 000000000..bc3ba1bf0 --- /dev/null +++ b/utils/template.js @@ -0,0 +1,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'), { + lastBuildDate: new Date().toUTCString(), + ttl: config.cacheExpire, + ...options, + }); +}; \ No newline at end of file diff --git a/views/rss.art b/views/rss.art index 375284955..f7e0f996e 100644 --- a/views/rss.art +++ b/views/rss.art @@ -15,7 +15,7 @@ {{ /if }} {{ lastBuildDate }} - 300 + {{ ttl }} {{ each item }} <![CDATA[{{ $value.title }}]]>