Skip to content

Commit

Permalink
fix(route): minecraft forge mod 使用新 api (DIYgod#7807)
Browse files Browse the repository at this point in the history
  • Loading branch information
Discreater authored Oct 5, 2021
1 parent cb4a882 commit c954d14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/game.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pageClass: routes

### CurseForge Mod 更新

<Route author="Indexyz" example="/curseforge/files/jei" path="/curseforge/files/:project" :paramsDesc="['项目的短名或者 `Project ID`. 项目的短名可以在地址栏获取到, 例如地址为 `https://minecraft.curseforge.com/projects/non-update`, 短名就为 `non-update`. `Project ID` 可在 `Overview` 中的 `About This Project` 中找到']"/>
<Route author="Indexyz Discreater" example="/curseforge/files/jei" path="/curseforge/files/:project" :paramsDesc="['项目的 ID, 可在 mod 主页的 `About This Project` 中找到']"/>

### Feed The Beast (FTB) 模组包更新

Expand Down
62 changes: 23 additions & 39 deletions lib/routes/curseforge/files.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');

const BASE_URL = 'https://minecraft.curseforge.com/';
const API_BASE_URL = 'https://addons-ecs.forgesvc.net/api/v2/addon';

module.exports = async (ctx) => {
const { project } = ctx.params;
const projectLink = `${BASE_URL}/projects/${project}/files`;
const projectFilesReq = await got.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 cache = await ctx.cache.get(document.link);

if (cache) {
return JSON.parse(cache);
}

const itemReq = await got.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}. ` + `</br> 支持的版本为: ${supportVersions}`;

document.pubDate = new Date(Number($item('.standard-datetime').attr('data-epoch')) * 1000).toUTCString();
document.guid = $item('.md5').text();

ctx.cache.set(document.link, JSON.stringify(document));

return document;
});

const item = await Promise.all(reqs);

const projectAPILink = `${API_BASE_URL}/${project}`;
const projectFilesAPILink = `${projectAPILink}/files`;

const [projectDesc, projectFiles] = (await Promise.all([got.get(projectAPILink), got.get(projectFilesAPILink)])).map((resp) => resp.data);

const projectName = projectDesc.name;
const projectLink = projectDesc.websiteUrl;
const author = projectDesc.authors[0].name;

const item = projectFiles.map((file) => {
const doc = {};
doc.link = projectLink;
doc.author = author;
doc.title = file.displayName;
const supportVersions = file.gameVersion;

doc.description = `${projectName} 发布了新的文件: ${file.displayName}. </br> 支持的版本为: ${supportVersions}`;
doc.pubDate = new Date(file.fileDate).toUTCString();
doc.guid = file.id;
return doc;
});

ctx.state.data = {
title: `CurseForge 更新 - ${projectName}`,
Expand Down

0 comments on commit c954d14

Please sign in to comment.