copy_manga: Avoid loading chapters too quickly

* Update copy_manga.js

修复loadEp逻辑,防止访问过快风控

* format

---------

Co-authored-by: nyne <me@nyne.dev>
This commit is contained in:
Shapaper
2025-01-30 17:39:36 +08:00
committed by GitHub
parent 71fa8716f9
commit 139288c064
2 changed files with 91 additions and 48 deletions

View File

@@ -4,7 +4,7 @@ class CopyManga extends ComicSource {
key = "copy_manga"
version = "1.0.4"
version = "1.0.5"
minAppVersion = "1.0.0"
@@ -347,10 +347,11 @@ class CopyManga extends ComicSource {
if (keyword.startsWith("作者:")) {
author = keyword.substring("作者:".length).trim();
}
let res;
// 通过onClickTag传入时有"作者:"前缀,处理这种情况
if (author && author in this.author_path_word_dict) {
let path_word = encodeURIComponent(this.author_path_word_dict[author]);
var res = await Network.get(
res = await Network.get(
`https://api.copymanga.tv/api/v3/comics?limit=21&offset=${(page - 1) * 21}&ordering=-datetime_updated&author=${path_word}&platform=3`,
this.headers
)
@@ -362,8 +363,8 @@ class CopyManga extends ComicSource {
q_type = options[0];
}
keyword = encodeURIComponent(keyword)
let search_url = this.loadSetting('search_api') == "webAPI" ? "https://www.copymanga.tv/api/kb/web/searchbc/comics" : "https://api.copymanga.tv/api/v3/search/comic"
var res = await Network.get(
let search_url = this.loadSetting('search_api') === "webAPI" ? "https://www.copymanga.tv/api/kb/web/searchbc/comics" : "https://api.copymanga.tv/api/v3/search/comic"
res = await Network.get(
`${search_url}?limit=21&offset=${(page - 1) * 21}&q=${keyword}&q_type=${q_type}&platform=3`,
this.headers
)
@@ -495,8 +496,8 @@ class CopyManga extends ComicSource {
comic = {
loadInfo: async (id) => {
async function getChapters(id) {
var res = await Network.get(
let getChapters = async (id) => {
let res = await Network.get(
`https://api.copymanga.tv/api/v3/comic/${id}/group/default/chapters?limit=500&offset=0&platform=3`,
this.headers
);
@@ -533,7 +534,7 @@ class CopyManga extends ComicSource {
return eps;
}
async function getFavoriteStatus(id) {
let getFavoriteStatus = async (id) => {
let res = await Network.get(`https://api.copymanga.tv/api/v3/comic2/${id}/query?platform=3`, this.headers);
if (res.status !== 200) {
throw `Invalid status code: ${res.status}`;
@@ -585,29 +586,71 @@ class CopyManga extends ComicSource {
}
},
loadEp: async (comicId, epId) => {
let res = await Network.get(
let attempt = 0;
const maxAttempts = 5;
let res;
let data;
while (attempt < maxAttempts) {
try {
res = await Network.get(
`https://api.copymanga.tv/api/v3/comic/${comicId}/chapter2/${epId}?platform=3`,
this.headers
);
if (res.status === 210) {
// 210 indicates too frequent access, extract wait time
let waitTime = 40000; // Default wait time 40s
try {
let responseBody = JSON.parse(res.body);
if (
responseBody.message &&
responseBody.message.includes("Expected available in")
) {
let match = responseBody.message.match(/(\d+)\s*seconds/);
if (match && match[1]) {
waitTime = parseInt(match[1]) * 1000;
}
}
} catch (e) {
console.log(
"Unable to parse wait time, using default wait time 40s"
);
}
console.log(`Chapter${epId} access too frequent, waiting ${waitTime / 1000}s`);
await new Promise((resolve) => setTimeout(resolve, waitTime));
throw "Retry";
}
if (res.status !== 200) {
throw `Invalid status code: ${res.status}`;
}
let data = JSON.parse(res.body);
data = JSON.parse(res.body);
// console.log(data.results.chapter);
// Handle image link sorting
let imagesUrls = data.results.chapter.contents.map((e) => e.url);
let orders = data.results.chapter.words;
let imagesUrls = data.results.chapter.contents.map(e => e.url)
let orders = data.results.chapter.words
let images = imagesUrls.map(e => "")
let images = new Array(imagesUrls.length).fill(""); // Initialize an array with the same length as imagesUrls
// Arrange images according to orders
for (let i = 0; i < imagesUrls.length; i++) {
images[orders[i]] = imagesUrls[i]
images[orders[i]] = imagesUrls[i];
}
return {
images: images
images: images,
};
} catch (error) {
if (error !== "Retry") {
throw error;
}
attempt++;
if (attempt >= maxAttempts) {
throw error;
}
}
}
},
loadComments: async (comicId, subId, page, replyTo) => {
@@ -671,7 +714,7 @@ class CopyManga extends ComicSource {
}
},
onClickTag: (namespace, tag) => {
if(namespace == "标签"){
if (namespace === "标签") {
return {
// 'search' or 'category'
action: 'category',
@@ -680,7 +723,7 @@ class CopyManga extends ComicSource {
param: null,
}
}
if(namespace == "作者"){
if (namespace === "作者") {
return {
// 'search' or 'category'
action: 'search',

View File

@@ -3,7 +3,7 @@
"name": "拷贝漫画",
"fileName": "copy_manga.js",
"key": "copy_manga",
"version": "1.0.4"
"version": "1.0.5"
},
{
"name": "Komiic",