80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
const axios = require('axios');
|
||
const fs = require('fs');
|
||
const readline = require('readline');
|
||
|
||
// Modrinth API URL
|
||
const API_URL = 'https://api.modrinth.com/v2/search';
|
||
|
||
// 本地服务器URL
|
||
const LOCAL_SERVER_URL = 'http://192.168.1.7:5000/modid';
|
||
|
||
// 用于存储请求结果的数组
|
||
let results = [];
|
||
|
||
// 用于记录请求的次数
|
||
let requestCount = 0;
|
||
|
||
// 用于记录请求的间隔时间(毫秒)
|
||
const requestInterval = 3000; // 3秒
|
||
|
||
// 用于读取Modrinth API返回的响应
|
||
async function fetchModrinthData(page) {
|
||
try {
|
||
// 构建查询参数
|
||
const facets = [["project_type:mod"]];
|
||
const params = {
|
||
facets: JSON.stringify(facets),
|
||
limit: 96,
|
||
offset: page * 96 // 更新offset以获取下一页数据
|
||
};
|
||
// 发送请求
|
||
const response = await axios.get(API_URL, {
|
||
params: params
|
||
});
|
||
|
||
|
||
// 处理响应数据
|
||
response.data.hits.forEach(async (hit) => {
|
||
const modid = hit.slug;
|
||
const clientSide = hit.client_side;
|
||
const serverSide = hit.server_side;
|
||
|
||
// 根据client_side和server_side的值计算client参数
|
||
let client = false;
|
||
if (clientSide === "required" && serverSide === "required") {
|
||
client = "trlse"; // 两者都要求
|
||
} else if (clientSide === "required") {
|
||
client = true; // 只有client_side要求
|
||
} else if (serverSide === "required") {
|
||
client = false; // 只有server_side要求
|
||
} else {
|
||
client = false; // 两者都不要求或为optional
|
||
}
|
||
|
||
// 构建本地服务器请求的URL,包含根据client_side和server_side计算出的client值
|
||
const localUrl = `${LOCAL_SERVER_URL}?modid=${modid}&client=${client}`;
|
||
|
||
// 发送请求到本地服务器
|
||
try {
|
||
const localResponse = await axios.get(localUrl);
|
||
// 处理本地服务器的响应
|
||
console.log(`处理了modid: ${modid}`);
|
||
console.log(`本地服务器响应: ${localResponse.data}`);
|
||
} catch (localError) {
|
||
// 忽略本地服务器请求的错误
|
||
console.log(`处理modid ${modid}时本地服务器请求失败: ${localError.message}`);
|
||
}
|
||
});
|
||
|
||
// 如果有下一页,继续请求
|
||
if (response.data.offset < response.data.total_hits) {
|
||
await fetchModrinthData(page + 1);
|
||
}
|
||
} catch (error) {
|
||
// 忽略Modrinth API请求的错误
|
||
console.log(`处理Modrinth API请求时发生错误: ${error.message}`);
|
||
}
|
||
}
|
||
|
||
// 开始请求Modrinth API
|
||
fetchModrinthData(0); // 从第0页开始
|