Skip to content

Commit

Permalink
fix: mt refresh fails due to rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
jiang925 committed Jul 4, 2024
1 parent b23ae94 commit 253334d
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions resource/sites/xp.m-team.cc/getUserSeedingTorrents.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
(function(options, User) {
class Parser {
static MAX_RETRIES = 2;
static RETRY_DELAY_MS = 8000;

constructor(options, dataURL) {
this.options = options;
this.dataURL = dataURL;
this.body = null;
this.rawData = "";
this.pageInfo = {
count: 0,
current: 0
current: 0,
};
this.result = {
seeding: 0,
seedingSize: 0
seedingSize: 0,
};
this.load();
}
Expand All @@ -20,8 +23,8 @@
* 完成
*/
done() {
this.result.messageCount = this.getUnReadMessageCount()
console.log(`[mt] getUserSeedingTorrents done`, this.result)
this.result.messageCount = this.getUnReadMessageCount();
console.log(`[mt] getUserSeedingTorrents done`, this.result);
this.options.resolve(this.result);
}

Expand All @@ -34,10 +37,10 @@
let datas = this.rawData.data.data;
let results = {
seeding: 0,
seedingSize: 0
seedingSize: 0,
};
if (datas) {
datas.forEach(item => {
datas.forEach((item) => {
results.seeding++;
results.seedingSize += Number(item.torrent.size);
});
Expand Down Expand Up @@ -74,57 +77,75 @@
let postData = this.options.rule.requestData;
postData.pageNumber = this.pageInfo.current + 1;

$.ajax({
url,
method: "POST",
dataType: "JSON",
data: JSON.stringify(postData),
contentType: "application/json",
headers: this.options.rule.headers
})
.done(result => {
this.rawData = result;
if (this.rawData.data.data.length > 0) {
this.parse();
} else {
this.done();
}
})
.fail(() => {
this.done();
});
function makeRequest(retryCount = 0) {
setTimeout(() => {
$.ajax({
url,
method: "POST",
dataType: "JSON",
data: JSON.stringify(postData),
contentType: "application/json",
headers: this.options.rule.headers,
})
.done((result) => {
try {
this.rawData = result;
if (this.rawData.data.data.length > 0) {
this.parse();
} else {
this.done();
}
} catch (error) {
console.error("[mt] Error processing result:", error);
if (retryCount < Parser.MAX_RETRIES) {
makeRequest.call(this, retryCount + 1);
} else {
this.done();
}
}
})
.fail(() => {
this.done();
});
}, Parser.RETRY_DELAY_MS);
}

// Call the function for the first time
makeRequest.call(this);
}

/**
* 获取未读消息数量, 包括站内信和系统通知
* 这是两个接口, 直接写 config.json 是无法实现的. 在这里加点魔法
*/
getUnReadMessageCount() {
return this.getMailBoxCnt() + this.getSystemNoticeCnt()
return this.getMailBoxCnt() + this.getSystemNoticeCnt();
}

/**
* 获取站内信未读数量
*/
getMailBoxCnt() {
return this.getNotifyCnt(resolveURL(activeURL, '/api/msg/statistic'))
return this.getNotifyCnt(resolveURL(activeURL, "/api/msg/statistic"));
}

/**
* 获取系统通知未读数量
*/
getSystemNoticeCnt() {
return this.getNotifyCnt(resolveURL(activeURL, '/api/msg/notify/statistic'))
return this.getNotifyCnt(
resolveURL(activeURL, "/api/msg/notify/statistic")
);
}

getNotifyCnt(url) {
const res = $.ajax(url, {
method: "POST",
data: {},
headers: this.options.rule.headers,
async: false
})
return parseInt(res.responseJSON.data.unMake) || 0
async: false,
});
return parseInt(res.responseJSON.data.unMake) || 0;
}
}

Expand Down

0 comments on commit 253334d

Please sign in to comment.