MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / downloadSingleImage

Function downloadSingleImage

zpr/zpr.ts:333–425  ·  view source on GitHub ↗
(
    item: SetuData, 
    index: number, 
    r18: number, 
    currentProxy: string,
    allProxies: string[]
)

Source from the content-addressed store, hash-verified

331 index: number,
332 r18: number,
333 currentProxy: string,
334 allProxies: string[]
335): Promise<DownloadResult> {
336 const { pid, title, width, height, urls } = item;
337 const imgName = `${pid}_${index}.jpg`;
338 const filePath = path.join(dataPath, imgName);
339
340 // 将当前配置的代理放在第一位,其他代理作为备选
341 const proxyList = [currentProxy, ...allProxies.filter(proxy => proxy !== currentProxy)];
342
343 let lastError: string = "";
344 let has404Error = false;
345 let hadNetworkFailures = false; // 跟踪是否遇到过网络错误
346 let failedProxies: string[] = []; // 记录失败的代理
347
348 for (const proxyHost of proxyList) {
349 try {
350 const imgController = new AbortController();
351 const clearImgTimeout = scheduleAbort(imgController, 30000, "zpr:image-timeout");
352
353 try {
354 const imgResponse = await axios.get(urls.regular, {
355 headers: getHeaders(proxyHost),
356 timeout: 30000,
357 responseType: 'arraybuffer',
358 signal: imgController.signal
359 });
360
361 if (imgResponse.status === 200) {
362 await fs.writeFile(filePath, imgResponse.data as any);
363
364 return {
365 mediaGroup: {
366 type: 'photo',
367 media: filePath,
368 caption: `<b>🎨 ${htmlEscape(title)}</b>
369
370🆔 <b>作品ID:</b> <a href="https://www.pixiv.net/artworks/${pid}">${pid}</a>
371🔗 <b>原图:</b> <a href="${htmlEscape(urls.original)}">高清查看</a>
372📐 <b>尺寸:</b> <code>${width}×${height}</code>
373
374<i>📡 来源: Pixiv</i>`,
375 hasSpoiler: r18 === 1
376 },
377 usedProxy: proxyHost,
378 hadNetworkFailures: hadNetworkFailures, // 报告是否之前有网络错误
379 failedProxies: failedProxies // 报告失败的代理
380 };
381 } else if (imgResponse.status === 404) {
382 has404Error = handle404Error(proxyHost, failedProxies);
383 continue; // 尝试下一个代理
384 } else {
385 lastError = `HTTP ${imgResponse.status}`;
386 failedProxies.push(proxyHost);
387 continue; // 尝试下一个代理
388 }
389 } finally {
390 clearImgTimeout();

Callers 1

getResultFunction · 0.85

Calls 6

scheduleAbortFunction · 0.85
getHeadersFunction · 0.85
handle404ErrorFunction · 0.85
isTimeoutErrorFunction · 0.85
htmlEscapeFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected