From 6f4da7c6cfb257221a67c20967e429aa00483a94 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 11 Apr 2018 17:52:03 +0800 Subject: [PATCH] juejin module --- .eslintrc | 3 +- index.js | 7 ++-- routes/juejin/category.js | 70 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 routes/juejin/category.js diff --git a/.eslintrc b/.eslintrc index d56516ed266837..329beaf50d69f3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -50,6 +50,7 @@ "no-useless-rename": 1, "rest-spread-spacing": 1, "no-trailing-spaces": 1, - "quotes": [1, "single"] + "quotes": [1, "single"], + "no-control-regex": 0 } } \ No newline at end of file diff --git a/index.js b/index.js index a539f179574ece..e78acb6e9cd5fe 100644 --- a/index.js +++ b/index.js @@ -15,10 +15,13 @@ app.get('/bilibili/user/dynamic/:uid', require('./routes/bilibili/dynamic')); app.get('/bilibili/partion/:tid', require('./routes/bilibili/partion')); app.get('/bilibili/bangumi/:seasonid', require('./routes/bilibili/bangumi')); -// Weibo +// 微博 app.get('/weibo/user/:uid', require('./routes/weibo/user')); -// NetEaseCloudMusic +// 网易云音乐 app.get('/ncm/playlist/:id', require('./routes/ncm/playlist')); +// 掘金 +app.get('/juejin/category/:category', require('./routes/juejin/category')); + app.listen(1200); \ No newline at end of file diff --git a/routes/juejin/category.js b/routes/juejin/category.js new file mode 100644 index 00000000000000..471d6807c8cfa9 --- /dev/null +++ b/routes/juejin/category.js @@ -0,0 +1,70 @@ +const request = require('request'); +const art = require('art-template'); +const path = require('path'); +const base = require('../base'); +const mix = require('../../utils/mix'); + +module.exports = (req, res) => { + const category = req.params.category; + + base({ + req: req, + res: res, + getHTML: (callback) => { + request.get({ + url: 'https://gold-tag-ms.juejin.im/v1/categories', + headers: { + 'User-Agent': mix.ua, + 'Referer': `https://juejin.im/welcome/${category}`, + 'X-Juejin-Client': '', + 'X-Juejin-Src': 'web', + 'X-Juejin-Token': '', + 'X-Juejin-Uid': '' + } + }, function (err, httpResponse, body) { + let data; + try { + data = JSON.parse(body); + } + catch (e) { + data = {}; + } + const cat = data.d && data.d.categoryList && data.d.categoryList.filter((item) => item.title === category)[0]; + if (cat && cat.id) { + request.get({ + url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_timeline?src=web&limit=20&category=${cat.id}`, + headers: { + 'User-Agent': mix.ua, + 'Referer': `https://juejin.im/welcome/${category}`, + } + }, function (err, httpResponse, body) { + let data; + try { + data = JSON.parse(body); + } + catch (e) { + data = {}; + } + + const html = art(path.resolve(__dirname, '../../views/rss.art'), { + 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, '')}`, + pubDate: new Date(item.createdAt).toUTCString(), + link: item.originalUrl + })), + }); + callback(html); + }); + } + else { + callback(''); + } + }); + } + }); +}; \ No newline at end of file