diff --git a/README.md b/README.md index 744046ea4..570a5fa86 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 杭州市 - 萧山区 - 大连市 +- MIUI + - 更新 ## 鸣谢 diff --git a/docs/README.md b/docs/README.md index 3d31589e6..074745e95 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1317,3 +1317,23 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到 路由: `/tingshuitz/dalian` 参数: 无 + +### MIUI 更新 + +举例: [https://rsshub.app/miui/aries/](https://rsshub.app/miui/aries/) + +路由: `/miui/:device/:type?` + +参数 + +**device** + +你的设备的 `codename` 例如 小米 2s 为 `aries` + +**type** + +可选参数 + +| 稳定版 | 开发版 | +| ------- | ------ | +| release | dev | diff --git a/router.js b/router.js index 15e419951..abb31498a 100644 --- a/router.js +++ b/router.js @@ -314,4 +314,7 @@ router.get('/tingshuitz/hangzhou', require('./routes/tingshuitz/hangzhou')); router.get('/tingshuitz/xiaoshan', require('./routes/tingshuitz/xiaoshan')); router.get('/tingshuitz/dalian', require('./routes/tingshuitz/dalian')); +// MIUI 更新 +router.get('/miui/:device/:type?', require('./routes/miui/index')); + module.exports = router; diff --git a/routes/miui/index.js b/routes/miui/index.js new file mode 100644 index 000000000..f2985427c --- /dev/null +++ b/routes/miui/index.js @@ -0,0 +1,58 @@ +const axios = require('../../utils/axios'); +const config = require('../../config'); + +const cacheLength = 5; +const cacheDays = 30; + +module.exports = async (ctx) => { + const { type = 'release', device } = ctx.params; + const releaseType = type === 'release' ? 'F' : 'X'; + const localeTypeName = type === 'release' ? '稳定版' : '开发版'; + const cacheName = `RSSHubMIUIUpdate|${device}|${releaseType}`; + + const response = await axios({ + method: 'get', + baseURL: 'http://update.miui.com', + url: '/updates/miota-fullrom.php', + headers: { + 'User-Agent': config.ua, + }, + params: { + d: device, + b: releaseType, + r: 'cn', + l: 'zh_CN', + n: '', + }, + }); + + const responseData = response.data; + let oldPosts = []; + try { + oldPosts = JSON.parse(await ctx.cache.get(cacheName)); + } catch (_e) { + /** no need handle here: parseError */ + } + + let item = oldPosts; + + if (oldPosts.length === 0 || oldPosts[0].description !== responseData.LatestFullRom.filename) { + item = [ + { + title: `${device} 有新的 ${localeTypeName}本: ${responseData.LatestFullRom.version}`, + guid: responseData.LatestFullRom.md5, + description: responseData.LatestFullRom.filename, + link: responseData.LatestFullRom.descriptionUrl, + }, + ...oldPosts, + ]; + + await ctx.cache.set(cacheName, item.slice(0, cacheLength), cacheDays * 24 * 60 * 60); + } + + ctx.state.data = { + title: `MIUI 更新 - ${device} - ${type === 'release' ? '稳定版' : '开发版'}`, + link: 'http://www.miui.com/download.html', + item, + }; +};