master
cody 4 months ago
parent 609096a13f
commit 90e9e06e10

@ -58,16 +58,39 @@ class UpdateBookIsbnData extends Command
} }
$this->info("找到 {$books->count()} 本书需要处理"); $this->info("找到 {$books->count()} 本书需要处理");
$this->info("API限制每秒最多10次请求预计耗时约 " . ceil($books->count() / 10) . " 秒");
$bar = $this->output->createProgressBar($books->count()); $bar = $this->output->createProgressBar($books->count());
$bar->start(); $bar->start();
$successCount = 0; $successCount = 0;
$failCount = 0; $failCount = 0;
$requestCount = 0;
$startTime = microtime(true);
$lastResetTime = $startTime;
foreach ($books as $book) { foreach ($books as $book) {
try { try {
// 每秒重置请求计数器
$currentTime = microtime(true);
if ($currentTime - $lastResetTime >= 1.0) {
$requestCount = 0;
$lastResetTime = $currentTime;
}
// API频率限制控制每秒最多10次请求
if ($requestCount >= 10) {
$waitTime = 1.0 - ($currentTime - $lastResetTime);
if ($waitTime > 0) {
usleep(intval($waitTime * 1000000));
$requestCount = 0;
$lastResetTime = microtime(true);
}
}
$result = $this->processBook($book, $apiKey); $result = $this->processBook($book, $apiKey);
$requestCount++;
if ($result) { if ($result) {
$successCount++; $successCount++;
$this->line("\n✓ 成功处理书籍: {$book->title} (ISBN: {$book->isbn})"); $this->line("\n✓ 成功处理书籍: {$book->title} (ISBN: {$book->isbn})");
@ -78,18 +101,31 @@ class UpdateBookIsbnData extends Command
} catch (\Exception $e) { } catch (\Exception $e) {
$failCount++; $failCount++;
$this->line("\n✗ 处理异常: {$book->title} - {$e->getMessage()}"); $this->line("\n✗ 处理异常: {$book->title} - {$e->getMessage()}");
// 如果是API相关错误增加等待时间
if (strpos($e->getMessage(), 'API') !== false || strpos($e->getMessage(), 'HTTP') !== false) {
$this->line("检测到API错误等待2秒后继续...");
sleep(2);
$requestCount = 0;
$lastResetTime = microtime(true);
}
} }
$bar->advance(); $bar->advance();
// 添加延迟避免API请求过快
sleep(1);
} }
$bar->finish(); $bar->finish();
$totalTime = microtime(true) - $startTime;
$avgTimePerBook = $totalTime / $books->count();
$actualRequestsPerSecond = $books->count() / $totalTime;
$this->line(''); $this->line('');
$this->info("处理完成!成功: {$successCount}, 失败: {$failCount}"); $this->info("处理完成!");
$this->info("成功: {$successCount}, 失败: {$failCount}");
$this->info("总耗时: " . round($totalTime, 2) . " 秒");
$this->info("平均每本书耗时: " . round($avgTimePerBook, 2) . " 秒");
$this->info("实际请求频率: " . round($actualRequestsPerSecond, 2) . " 次/秒");
return 0; return 0;
} }
@ -103,40 +139,64 @@ class UpdateBookIsbnData extends Command
*/ */
private function processBook(Book $book, string $apiKey): bool private function processBook(Book $book, string $apiKey): bool
{ {
// 调用ISBN接口 $attempt = 0;
$response = Http::timeout(30)->get('https://api.tanshuapi.com/api/isbn/v2/index', [ $maxRetries = 3;
'key' => $apiKey,
'isbn' => $book->isbn
]);
if (!$response->successful()) {
$this->error("API请求失败: HTTP {$response->status()}");
return false;
}
$data = $response->json(); while ($attempt < $maxRetries) {
try {
$attempt++;
if (!$data || $data['code'] !== 1) { // 调用ISBN接口
$this->error("API返回错误: " . ($data['msg'] ?? '未知错误')); $response = Http::timeout(30)->get('https://api.tanshuapi.com/api/isbn/v2/index', [
return false; 'key' => $apiKey,
} 'isbn' => $book->isbn
]);
$bookData = $data['data']; if (!$response->successful()) {
throw new \Exception("API请求失败: HTTP {$response->status()}");
}
$data = $response->json();
if (!$data || $data['code'] !== 1) {
// 如果是API密钥错误或其他不可重试的错误直接返回失败
if (isset($data['code']) && in_array($data['code'], [10001, 10002, 10003])) {
$this->error("API返回不可重试错误: " . ($data['msg'] ?? '未知错误'));
return false;
}
throw new \Exception("API返回错误: " . ($data['msg'] ?? '未知错误'));
}
$bookData = $data['data'];
// 更新书籍的other_data字段
$book->other_data = $bookData;
// 更新书籍的other_data字段 // 如果有图片URL下载图片
$book->other_data = $bookData; if (!empty($bookData['img'])) {
$coverId = $this->downloadAndSaveImage($bookData['img'], $book);
if ($coverId) {
$book->cover_id = $coverId;
}
}
$book->save();
return true;
} catch (\Exception $e) {
if ($attempt >= $maxRetries) {
$this->error("重试 {$maxRetries} 次后仍然失败: {$e->getMessage()}");
return false;
}
// 如果有图片URL下载图片 // 指数退避第1次重试等待1秒第2次等待2秒第3次等待4秒
if (!empty($bookData['img'])) { $waitTime = pow(2, $attempt - 1);
$coverId = $this->downloadAndSaveImage($bookData['img'], $book); $this->line("第 {$attempt} 次尝试失败,{$waitTime} 秒后重试: {$e->getMessage()}");
if ($coverId) { sleep($waitTime);
$book->cover_id = $coverId;
} }
} }
$book->save(); return false;
return true;
} }
/** /**

Loading…
Cancel
Save