From 365f9fbca07329155ab7162e83761d13dfaf4f43 Mon Sep 17 00:00:00 2001 From: Indexyz Date: Mon, 3 Sep 2018 02:32:55 +0800 Subject: [PATCH] feat(curseforge): add curseforge support --- docs/README.md | 12 +++++++++++ router.js | 3 +++ routes/curseforge/files.js | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 routes/curseforge/files.js diff --git a/docs/README.md b/docs/README.md index e05f613e3a..26b7391ece 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2588,3 +2588,15 @@ GitHub 官方也提供了一些 RSS: ### App Store/Mac App Store 见 <#app-store-mac-app-store> + +## Minecraft CurseForge + +### Mod 更新 + +举例: + +路由: `/curseforge/files/:project` + +参数: + +- project: 项目的短名或者 `Project ID`. 项目的短名可以在地址栏获取到, 例如地址为 `https://minecraft.curseforge.com/projects/non-update`, 短名就为 `non-update`. `Project ID` 可在 `Overview` 中的 `About This Project` 中找到 diff --git a/router.js b/router.js index e5d1aa6429..ff196f4019 100755 --- a/router.js +++ b/router.js @@ -506,4 +506,7 @@ router.get('/zaobao/znews/:type?', require('./routes/zaobao/znews')); // Apple router.get('/apple/exchange_repair', require('./routes/apple/exchange_repair')); +// Minecraft CurseForge +router.get('/curseforge/files/:project', require('./routes/curseforge/files')); + module.exports = router; diff --git a/routes/curseforge/files.js b/routes/curseforge/files.js new file mode 100644 index 0000000000..3b4a61e99e --- /dev/null +++ b/routes/curseforge/files.js @@ -0,0 +1,42 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); + +const BASE_URL = 'https://minecraft.curseforge.com/'; + +module.exports = async (ctx) => { + const { project } = ctx.params; + const projectLink = `${BASE_URL}/projects/${project}/files`; + const projectFilesReq = await axios.get(projectLink); + const { data } = projectFilesReq; + const $ = cheerio.load(data); + const projectName = $('.project-title span').text(); + const reqs = $('.project-file-name-container > a').get() + .map(async (item) => { + const el = $(item); + const document = {}; + const link = el.attr('href'); + document.link = `${BASE_URL}${link}`; + const itemReq = await axios.get(document.link); + const $item = cheerio.load(itemReq.data); + const supportVersions = $item('.details-versions li') + .get() + .map((item) => $item(item).text()) + .join(', '); + document.author = $item('.user-tag a').text(); + document.title = $item('.details-header > h3').text(); + document.description = `${projectName} 的 ${document.author} 发布了新的文件: ${document.title}. 支持的版本为: ${supportVersions}`; + document.pubDate = new Date(Number($item('.standard-datetime').attr('data-epoch')) * 1000).toUTCString(); + document.guid = $item('.md5').text(); + + return document; + }); + + const item = await Promise.all(reqs); + + ctx.state.data = { + title: `CurseForge 更新 - ${projectName}`, + link: projectLink, + description: 'CurseForge Mod 更新', + item, + }; +};