diff --git a/README.md b/README.md index c6a32951b5..e4fa5dd9ed 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,10 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 新闻早报 - UU 看书 - 小说章节 +- 3dm + - 新闻 + - 攻略 + - 下载 - 喜马拉雅 - 专辑 - EZTV diff --git a/docs/README.md b/docs/README.md index e273428093..267cc1362c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1085,6 +1085,32 @@ language,语言,可在 [Trending 页](https://github.com/trending/javascript 参数: id,小说 id,可在对应小说页 URL 中找到 +## 3dm + +### 新闻 + +举例: [https://rsshub.app/3dm/detroitbecomehuman/news](https://rsshub.app/3dm/detroitbecomehuman/news) + +路由: `/3dm/:name/news` + +参数: name,游戏的编号可以在专题页的 url 中找到 + +### 攻略 + +举例: [https://rsshub.app/3dm/detroitbecomehuman/gl](https://rsshub.app/3dm/detroitbecomehuman/gl) + +路由: `/3dm/:name/gl` + +参数: name,游戏的编号可以在专题页的 url 中找到 + +### 下载 + +举例: [https://rsshub.app/3dm/detroitbecomehuman/download](https://rsshub.app/3dm/detroitbecomehuman/download) + +路由: `/3dm/:name/download` + +参数: name,游戏的编号可以在专题页的 url 中找到 + ## 喜马拉雅 ### 专辑 diff --git a/router.js b/router.js index 9b279e8856..39e7fe884e 100644 --- a/router.js +++ b/router.js @@ -284,6 +284,9 @@ router.get('/nytimes/morning_post', require('./routes/nytimes/morning_post')); // UU看书 router.get('/uukanshu/chapter/:uid', require('./routes/uukanshu/chapter')); +// 3dm +router.get('/3dm/:name/:type', require('./routes/3dm/news')); + // 喜马拉雅 router.get('/ximalaya/album/:classify/:id', require('./routes/ximalaya/album')); diff --git a/routes/3dm/news.js b/routes/3dm/news.js new file mode 100644 index 0000000000..dbc00cb72c --- /dev/null +++ b/routes/3dm/news.js @@ -0,0 +1,118 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const name = ctx.params.name; + const type = ctx.params.type; + const url = `http://www.3dmgame.com/games/${name}/${type}`; + + const response = await axios({ + method: 'get', + url: url, + headers: { + 'User-Agent': config.ua, + }, + }); + + const data = response.data; + + const $ = cheerio.load(data); + const list = $('.dowlnewslist a'); + const items = []; + + for (let i = 0; i < list.length; i++) { + let item = $(list[i]); + const url = item.attr('href'); + + let content = ''; + const urlBase = url.replace(/.html/, ''); + let total = 1; + + const value = await ctx.cache.get(url); + if (value) { + item = value; + } else { + // 抓取分页 + for (let j = 1; ; j++) { + let response; + + const u = j === 1 ? url : urlBase + '_' + j + '.html'; + + try { + response = await axios({ + method: 'get', + url: u, + headers: { + 'User-Agent': config.ua, + }, + }); + } catch (e) { + break; + } + + const page = cheerio.load(response.data, { + decodeEntities: false, + }); + + if (type === 'download') { + content = page('.jieshao').html(); + } else { + // 提取页数 + if (j === 1) { + if (page('.pagelistbox').length === 0) { + total = 1; + } else { + total = parseInt( + page('.pagelistbox') + .find('span') + .html() + .match(/共 (\S*) 页/)[1] + ); + } + } + + // 去除不需要的元素 + page('.page_fenye').remove(); // 翻页 + page('.con p') + .last() + .remove(); // 专题跳转 + if (total > 1) { + page('.con p') + .last() + .remove(); // 快速翻页提示 + } + + content += page('.con div') + .next() + .html(); + } + + if (j >= total) { + break; + } + } + + item = { + title: item.find('p').text(), + description: content, + pubDate: item.find('span').text(), + link: url, + guid: url, + }; + + ctx.cache.set(url, item, 24 * 60 * 60); + } + + items.push(item); + } + + ctx.state.data = { + title: $('title') + .text() + .split('_')[0], + link: url, + description: $('.game-pc>p').text(), + item: items, + }; +};