From 43c5c95690c2f13eed75af4664193ebd56544a35 Mon Sep 17 00:00:00 2001 From: SettingDust Date: Tue, 12 Mar 2019 14:24:49 +0800 Subject: [PATCH] MP4BA (#1711) --- docs/README.md | 17 ++++++ lib/router.js | 3 + lib/routes/mp4ba/index.js | 124 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 lib/routes/mp4ba/index.js diff --git a/docs/README.md b/docs/README.md index c376315d6..4f08a974d 100755 --- a/docs/README.md +++ b/docs/README.md @@ -1096,6 +1096,23 @@ GitHub 官方也提供了一些 RSS: +### Mp4Ba + +**类型参考这里** +| 1 | 2 | 3 | 4 | +| - | - | - | - | +| 电影 | 连续剧 | 综艺 | 动画 | + +| 5 | 6 | 7 | 8 | 9 | +| ------ | ------ | ------ | ------ | ------ | +| 动作片 | 喜剧片 | 爱情片 | 科幻片 | 恐怖片 | + +| 10 | 11 | 12 | 13 | 14 | 15 | +| ------ | ------ | ------ | ------ | ------ | ------ | +| 剧情片 | 战争片 | 国产剧 | 港台剧 | 日韩剧 | 欧美剧 | + + + ## 图片 ### 妹子图 diff --git a/lib/router.js b/lib/router.js index 839b9be3d..85388e1e8 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1133,4 +1133,7 @@ router.get('/blogs/jingwei.link', require('./routes/blogs/jingwei_link')); router.get('/uraaka-joshi', require('./routes/uraaka-joshi/uraaka-joshi')); router.get('/uraaka-joshi/:id', require('./routes/uraaka-joshi/uraaka-joshi-user')); +// MP4吧 +router.get('/mp4ba/:param', require('./routes/mp4ba')); + module.exports = router; diff --git a/lib/routes/mp4ba/index.js b/lib/routes/mp4ba/index.js new file mode 100644 index 000000000..5fd4f3680 --- /dev/null +++ b/lib/routes/mp4ba/index.js @@ -0,0 +1,124 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); + +const baseUrl = 'http://www.mp4ba.com'; + +class ParamType { + static get paramType() { + return { type: 0, search: 1 }; + } + + constructor(param) { + this._param = param; + + if (param.match(/\d{1,2}/)) { + this._type = ParamType.paramType.type; // 参数为1-2位数字即为类型RSS + } else { + this._type = ParamType.paramType.search; // 其他情况为搜索 + } + } + + get param() { + return this._param; + } + + get type() { + return this._type; + } + + get url() { + let url; + if (!this._url) { + switch (this._type) { + case ParamType.paramType.type: + url = `${baseUrl}/?m=vod-type-id-${this._param}.html`; + break; + case ParamType.paramType.search: + url = `${baseUrl}/index.php?m=vod-search&wd=${this._param}`; + break; + } + this._url = url; + } + return this._url; + } +} + +async function buildRss($, paramType, ctx) { + const $list = (selector) => $('#data_list').find(selector); // 列表对象 + const title = $('title').text(); + const typeString = title.substring(0, title.indexOf('-')); + + function buildDesc() { + let desc = ''; + switch (paramType.type) { + case ParamType.paramType.type: + desc = `${typeString}类型`; + break; + case ParamType.paramType.search: + desc = `${typeString}的搜索结果`; + break; + } + return desc; + } + + async function buildItem($) { + return await Promise.all( + $('.alt1') + .map(async (_, result) => { + const $ = cheerio.load(result); + const $a = $('td') + .eq(2) + .children('a'); + const pageUrl = `${baseUrl}${$a.attr('href')}`; + const { cache } = ctx; + const title = $a.text().replace('.Mp4Ba', ''); + const link = pageUrl; + const guid = pageUrl.match(/\d+/); + let description = null; + const enclosure_type = 'application/x-bittorrent'; + let $magnet = null; + + async function getDescription() { + const key = `Mp4Ba${pageUrl.match(/\d+/)}`; + const value = await ctx.cache.get(key); + if (value) { + description = value; + const $ = cheerio.load(value); + $magnet = $('a[href^="magnet:?xt=urn:btih:"]'); + } else { + const pageData = (await axios.get(pageUrl)).data; + const $page = cheerio.load(pageData); + $magnet = $page('.main') + .find('.down_list') + .find('a[href^="magnet:?xt=urn:btih:"]'); + description = $page('.main') + .find('.intro') + .html(); + cache.set(key, description, 24 * 60 * 60); + } + } + + await getDescription(); + const enclosure_url = $magnet.attr('href'); + return { title, link, description, enclosure_url, enclosure_type, guid }; + }) + .get() + ); + } + + return { + title: `${typeString} - 高清MP4吧`, + link: paramType.url, + description: buildDesc(), + item: await buildItem($list), + }; +} + +module.exports = async (ctx) => { + const { params } = ctx; + const { param } = params; + const paramType = new ParamType(param); + const { data } = await axios.get(encodeURI(paramType.url)); + const $ = cheerio.load(data); + ctx.state.data = await buildRss($, paramType, ctx); +};