From e1f34f8e664fcfb8818506af2cc69d1377620b0c Mon Sep 17 00:00:00 2001 From: OldCityNight Date: Tue, 12 Mar 2024 13:31:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=9A=8F=E4=BE=BF=E5=86=99=E4=BA=86?= =?UTF-8?q?=E4=B8=AA=20API=20=E8=B0=83=E7=94=A8=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/api.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 model/api.js diff --git a/model/api.js b/model/api.js new file mode 100644 index 0000000..8e19dbc --- /dev/null +++ b/model/api.js @@ -0,0 +1,80 @@ +const axios = require('axios'); + +const sleep = ( time ) => { + return new Promise((resolve) => { + setTimeout(resolve, time); + }); +}; + +export default new class ApiMode { + constructor() { + this.ApiUrl = 'https://img-api.justrobot.dev'; + this.ratelimit = 1000; // 速率限制,单位毫秒 + this.lastReqTime = 0; // 最后一次请求时间 + }; + + /** 检查频率是否过快 */ + async checkTimeLimit () { + timenow = Date.now(); + if ( timenow - this.lastReqTime < this.ratelimit ) { + await sleep(this.ratelimit - (timenow - this.lastReqTime)); + }; + return; + }; + + /** 以名称方式抽取一张图片 */ + async getImgByName ( name ) { + + await this.checkTimeLimit(); // 防止太快被封 IP + let res = await axios.get(`${this.ApiUrl}/${name}`); + this.lastReqTime = Date.now(); // 更新最后一次请求时间 + + if ( res.data.code === 200 ) { + return res.data.data; + } else { + throw new Error(res.data.code); + }; + }; + + /** 以别名方式抽取一张图片(推荐),功能上覆盖了名称方式 */ + async getImgByAlia ( alia ) { + + await this.checkTimeLimit();// 防止太快被封 IP + let res = await axios.get(`${this.ApiUrl}/${alia}`); + this.lastReqTime = Date.now(); // 更新最后一次请求时间 + + if ( res.data.code === 200 ) { + return res.data.data; + } else { + throw new Error(res.data.code); + }; + }; + + /** 随机抽取一张图片 */ + async getRandImg () { + + await this.checkTimeLimit();// 防止太快被封 IP + let res = await axios.get(`${this.ApiUrl}/`); + this.lastReqTime = Date.now(); // 更新最后一次请求时间 + + if ( res.data.code === 200 ) { + return res.data.data; + } else { + throw new Error(res.data.code); + }; + }; + + /** 获取 API 端当前的别名映射表 */ + async getAliaMap () { + + await this.checkTimeLimit();// 防止太快被封 IP + let res = await axios.get(`${this.ApiUrl}/AliasMap`); + this.lastReqTime = Date.now(); // 更新最后一次请求时间 + + if ( res.data.code === 200 ) { + return res.data.data; + } else { + throw new Error(res.data.code); + }; + }; +}; \ No newline at end of file -- Gitee From 389c55d72f299937870deb52962ee108250bede7 Mon Sep 17 00:00:00 2001 From: OldCityNight Date: Tue, 12 Mar 2024 21:42:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=BA=93=E4=B8=BAnode-fetch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/api.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/model/api.js b/model/api.js index 8e19dbc..bc4318b 100644 --- a/model/api.js +++ b/model/api.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +const fetch = require('node-fetch'); const sleep = ( time ) => { return new Promise((resolve) => { @@ -26,55 +26,55 @@ export default new class ApiMode { async getImgByName ( name ) { await this.checkTimeLimit(); // 防止太快被封 IP - let res = await axios.get(`${this.ApiUrl}/${name}`); + let res = await fetch(`${this.ApiUrl}/${name}`); this.lastReqTime = Date.now(); // 更新最后一次请求时间 - if ( res.data.code === 200 ) { - return res.data.data; - } else { - throw new Error(res.data.code); + if ( !res.ok ) { + throw new Error(res.status); // 检查是否成功 }; + + return await res.buffer(); }; /** 以别名方式抽取一张图片(推荐),功能上覆盖了名称方式 */ async getImgByAlia ( alia ) { await this.checkTimeLimit();// 防止太快被封 IP - let res = await axios.get(`${this.ApiUrl}/${alia}`); + let res = await fetch(`${this.ApiUrl}/${alia}`); this.lastReqTime = Date.now(); // 更新最后一次请求时间 - if ( res.data.code === 200 ) { - return res.data.data; - } else { - throw new Error(res.data.code); + if ( !res.ok ) { + throw new Error(res.status); // 检查是否成功 }; + + return await res.buffer(); }; /** 随机抽取一张图片 */ async getRandImg () { await this.checkTimeLimit();// 防止太快被封 IP - let res = await axios.get(`${this.ApiUrl}/`); + let res = await fetch(`${this.ApiUrl}/`); this.lastReqTime = Date.now(); // 更新最后一次请求时间 - if ( res.data.code === 200 ) { - return res.data.data; - } else { - throw new Error(res.data.code); + if ( !res.ok ) { + throw new Error(res.status); // 检查是否成功 }; + + return await res.buffer(); }; /** 获取 API 端当前的别名映射表 */ async getAliaMap () { await this.checkTimeLimit();// 防止太快被封 IP - let res = await axios.get(`${this.ApiUrl}/AliasMap`); + let res = await fetch(`${this.ApiUrl}/AliasMap`); this.lastReqTime = Date.now(); // 更新最后一次请求时间 - if ( res.data.code === 200 ) { - return res.data.data; - } else { - throw new Error(res.data.code); + if ( !res.ok ) { + throw new Error(res.status); // 检查是否成功 }; + + return await res.json(); }; }; \ No newline at end of file -- Gitee