(count: number)
| 93 | } |
| 94 | |
| 95 | async fetchImageUrls(count: number): Promise<ImageResult> { |
| 96 | const maxAttempts = CONFIG.MAX_RETRIES * 2; |
| 97 | let lastError: Error | null = null; |
| 98 | |
| 99 | for (let attempt = 0; attempt < maxAttempts; attempt++) { |
| 100 | try { |
| 101 | const photoSet = await this.getRandomPhotoSet(); |
| 102 | const html = await withRetry(() => |
| 103 | this.client.get(photoSet.url).then((r) => r.data) |
| 104 | ); |
| 105 | const images = this.extractGalleryImages(html); |
| 106 | |
| 107 | if (images.length === 0) { |
| 108 | console.warn(`套图 ${photoSet.title} 未找到图片,重试`); |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | if (images.length < count) { |
| 113 | console.warn( |
| 114 | `套图 ${photoSet.title} 只有${images.length}张,需要${count}张,重试` |
| 115 | ); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | const selected = this.pickRandom(images, count); |
| 120 | return { imageUrls: selected, photoSet }; |
| 121 | } catch (error) { |
| 122 | lastError = error as Error; |
| 123 | const errorMessage = error instanceof Error ? error.message : ""; |
| 124 | if ( |
| 125 | attempt < maxAttempts - 1 && |
| 126 | !errorMessage.includes("只有") && |
| 127 | !errorMessage.includes("未找到") |
| 128 | ) { |
| 129 | await new Promise((r) => setTimeout(r, Math.min((attempt + 1) * 1000, 3000))); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | throw new Error( |
| 134 | `获取图片失败,已尝试${maxAttempts}次: ${lastError?.message || "未知错误"}` |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | async downloadImages(imageUrls: string[]): Promise<string[]> { |
| 139 | const tasks = imageUrls.map((url) => |
no test coverage detected