From 67009c8cb427324b47266471519f7bdf9917b097 Mon Sep 17 00:00:00 2001 From: hoilc Date: Tue, 3 Dec 2019 20:04:36 +0800 Subject: [PATCH] feat: add bahamut creation (#3532) --- docs/social-media.md | 26 ++++++++++ lib/router.js | 4 ++ lib/routes/bahamut/creation.js | 15 ++++++ lib/routes/bahamut/creation_index.js | 73 ++++++++++++++++++++++++++++ lib/routes/bahamut/utils.js | 67 +++++++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 lib/routes/bahamut/creation.js create mode 100644 lib/routes/bahamut/creation_index.js create mode 100644 lib/routes/bahamut/utils.js diff --git a/docs/social-media.md b/docs/social-media.md index bd9b0314cd..5edd71f27f 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -410,6 +410,32 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 +## 巴哈姆特 + +### 个人小屋 + + + +### 创作大厅 + + + +分类 ID 参考如下 + +| 不限 | 日誌 | 小說 | 繪圖 | Cosplay | 同人商品 | +| ---- | ---- | ---- | ---- | ------- | -------- | +| 0 | 1 | 2 | 3 | 4 | 5 | + +子分类 ID 比较多不作列举 + +排行类型参考如下 + +| 達人專欄 | 最新創作 | 最新推薦 | 熱門創作 | 精選閣樓 | +| -------- | -------- | -------- | -------- | -------- | +| 1 | 2 | 3 | 4 | 5 | + + + ## 币乎 ### 用户动态 diff --git a/lib/router.js b/lib/router.js index 4b193e253a..f22d600747 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2013,6 +2013,10 @@ router.get('/nyaa/search/:query?', require('./routes/nyaa/search')); // 片源网 router.get('/pianyuan/:media?', require('./routes/pianyuan/app')); +// 巴哈姆特 +router.get('/bahamut/creation/:author/:category?', require('./routes/bahamut/creation')); +router.get('/bahamut/creation_index/:category?/:subcategory?/:type?', require('./routes/bahamut/creation_index')); + // CentBrowser router.get('/centbrowser/history', require('./routes/centbrowser/history')); diff --git a/lib/routes/bahamut/creation.js b/lib/routes/bahamut/creation.js new file mode 100644 index 0000000000..72684f8a3b --- /dev/null +++ b/lib/routes/bahamut/creation.js @@ -0,0 +1,15 @@ +const utils = require('./utils'); + +module.exports = async (ctx) => { + const author = ctx.params.author; + + const url = ctx.params.category ? `https://home.gamer.com.tw/creationCategory.php?owner=${author}&c=${ctx.params.category}` : `https://home.gamer.com.tw/creation.php?owner=${author}`; + + const { title, items } = await utils.ProcessFeed(url, ctx); + + ctx.state.data = { + title: title, + link: url, + item: items, + }; +}; diff --git a/lib/routes/bahamut/creation_index.js b/lib/routes/bahamut/creation_index.js new file mode 100644 index 0000000000..b04370a290 --- /dev/null +++ b/lib/routes/bahamut/creation_index.js @@ -0,0 +1,73 @@ +const utils = require('./utils'); + +const type_map = { + 0: '達人專欄', + 1: '達人專欄', + 2: '最新創作', + 3: '最新推薦', + 4: '熱門創作', + 5: '精選閣樓', +}; + +const category_map = { + 0: '不限', + 1: '日誌', + 2: '小說', + 3: '繪圖', + 4: 'Cosplay', + 5: '同人商品', +}; + +const subcategory_map = { + 0: '不限', + 1: 'ACG相關', + 2: '生活休閒', + 3: '巴哈相關', + 4: '歡樂惡搞', + 5: '心情日記', + 6: '模型/公仔', + 7: '視聽娛樂', + 8: '興趣嗜好', + 9: '其他', + 10: '愛情', + 11: '奇幻', + 12: '科幻', + 13: '武俠', + 14: '推理驚悚', + 15: '歡樂惡搞', + 16: 'BL/GL', + 17: '輕小說', + 18: '其他', + 19: '紙上塗鴉', + 20: '草稿/線稿', + 21: '單色人物', + 22: '彩色人物', + 23: '完稿作品', + 24: '漫畫', + 25: '勇者造型', + 26: '其他', + 27: 'ACG相關', + 28: '特攝', + 29: '布袋戲', + 30: '電影', + 31: '其他', + 32: '男性向', + 33: '女性向', + 34: '其他', +}; + +module.exports = async (ctx) => { + const type = ctx.params.type ? ctx.params.type : '1'; + const category = ctx.params.category ? ctx.params.category : '0'; + const subcategory = ctx.params.subcategory ? ctx.params.subcategory : '0'; + + const url = `https://home.gamer.com.tw/index.php?k1=${category}&k2=${subcategory}&vt=${type}&sm=3`; + + const { items } = await utils.ProcessFeed(url, ctx); + + ctx.state.data = { + title: `巴哈姆特创作大厅${category !== '0' ? ' - ' + category_map[category] : ''}${subcategory !== '0' ? ' - ' + subcategory_map[subcategory] : ''} - ${type_map[type]}`, + link: url, + item: items, + }; +}; diff --git a/lib/routes/bahamut/utils.js b/lib/routes/bahamut/utils.js new file mode 100644 index 0000000000..a769ece937 --- /dev/null +++ b/lib/routes/bahamut/utils.js @@ -0,0 +1,67 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const base = 'https://home.gamer.com.tw/'; + +module.exports = { + ProcessFeed: async (url, ctx) => { + const response = await got.get(url); + const $ = cheerio.load(response.data); + + const title = $('title').text(); + const list = $('.BH-lbox > .HOME-mainbox1').toArray(); + + const parseContent = (htmlString) => { + const $ = cheerio.load(htmlString); + const content = $('.MSG-list8C'); + + const images = $('img'); + for (let k = 0; k < images.length; k++) { + $(images[k]).attr('src', $(images[k]).attr('data-src')); + } + + return { + description: content.html(), + }; + }; + + const items = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const title = $('.HOME-mainbox1b > h1 > a'); + const link = base + title.attr('href'); + const author = $('.HOME-mainbox1b > .ST1 > a').text(); + const time = $('.HOME-mainbox1b > .ST1') + .text() + .split('│')[1]; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const topic = { + title: title.text().trim(), + link: link, + author: author, + pubDate: new Date(time), + }; + + try { + const detail_response = await got.get(link); + const result = parseContent(detail_response.data); + if (!result.description) { + return Promise.resolve(''); + } + topic.description = result.description; + } catch (err) { + return Promise.resolve(''); + } + ctx.cache.set(link, JSON.stringify(topic)); + return Promise.resolve(topic); + }) + ); + + return { title: title, items: items }; + }, +};