| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 290 |
| Code Lines | 187 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | public function handle(): int |
||
| 50 | { |
||
| 51 | $limit = (int) $this->option('limit'); |
||
| 52 | $chunkSize = (int) $this->option('chunk'); |
||
| 53 | $missingOnly = $this->option('missing-only'); |
||
| 54 | $retryFailed = $this->option('retry-failed'); |
||
| 55 | $force = $this->option('force'); |
||
| 56 | |||
| 57 | $this->info('Starting AniList data refresh for anime releases...'); |
||
| 58 | if ($retryFailed) { |
||
| 59 | $this->info('Mode: Retrying failed releases (anidbid <= 0)...'); |
||
| 60 | } elseif ($missingOnly) { |
||
| 61 | $this->info('Mode: Missing AniList data only...'); |
||
| 62 | } else { |
||
| 63 | $this->info('Mode: All releases...'); |
||
| 64 | } |
||
| 65 | $this->info('Matching releases by searchname to AniList API...'); |
||
| 66 | $this->newLine(); |
||
| 67 | |||
| 68 | // Build query for releases in TV_ANIME category |
||
| 69 | $query = Release::query() |
||
| 70 | ->select(['releases.id', 'releases.anidbid', 'releases.searchname']) |
||
| 71 | ->where('categories_id', Category::TV_ANIME); |
||
| 72 | |||
| 73 | // If retry-failed, only get releases with anidbid <= 0 (failed processing) |
||
| 74 | if ($retryFailed) { |
||
| 75 | $query->where('releases.anidbid', '<=', 0); |
||
| 76 | } |
||
| 77 | |||
| 78 | // If missing-only, only get releases without anilist_id |
||
| 79 | if ($missingOnly) { |
||
| 80 | $query->leftJoin('anidb_info as ai', 'ai.anidbid', '=', 'releases.anidbid') |
||
| 81 | ->whereNull('ai.anilist_id'); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Get releases (not distinct anidbids, since we're matching by searchname) |
||
| 85 | $releases = $query->orderBy('releases.id') |
||
|
|
|||
| 86 | ->get(); |
||
| 87 | |||
| 88 | $totalCount = $releases->count(); |
||
| 89 | |||
| 90 | if ($totalCount === 0) { |
||
| 91 | $this->warn('No anime releases found to process.'); |
||
| 92 | return self::SUCCESS; |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->info("Found {$totalCount} anime releases to process."); |
||
| 96 | |||
| 97 | if ($limit > 0) { |
||
| 98 | $releases = $releases->take($limit); |
||
| 99 | $totalCount = $releases->count(); |
||
| 100 | $this->info("Processing {$totalCount} releases (limited)."); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->newLine(); |
||
| 104 | |||
| 105 | $populateAniList = new PopulateAniList; |
||
| 106 | $processed = 0; |
||
| 107 | $successful = 0; |
||
| 108 | $failed = 0; |
||
| 109 | $skipped = 0; |
||
| 110 | $notFound = 0; |
||
| 111 | $failedSearchnames = []; // Track failed searchnames for summary |
||
| 112 | |||
| 113 | // Process in chunks |
||
| 114 | $chunks = $releases->chunk($chunkSize); |
||
| 115 | $progressBar = $this->output->createProgressBar($totalCount); |
||
| 116 | $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% -- %message%'); |
||
| 117 | $progressBar->setMessage('Starting...'); |
||
| 118 | $progressBar->start(); |
||
| 119 | |||
| 120 | foreach ($chunks as $chunk) { |
||
| 121 | foreach ($chunk as $release) { |
||
| 122 | $searchname = $release->searchname ?? ''; |
||
| 123 | $progressBar->setMessage("Processing: " . substr($searchname, 0, 50) . "..."); |
||
| 124 | |||
| 125 | try { |
||
| 126 | // Extract clean title from searchname |
||
| 127 | $titleData = $this->extractTitleFromSearchname($searchname); |
||
| 128 | |||
| 129 | if (empty($titleData) || empty($titleData['title'])) { |
||
| 130 | $notFound++; |
||
| 131 | $failedSearchnames[] = [ |
||
| 132 | 'searchname' => $searchname, |
||
| 133 | 'reason' => 'Failed to extract title', |
||
| 134 | 'cleaned_title' => null, |
||
| 135 | ]; |
||
| 136 | if ($this->getOutput()->isVerbose()) { |
||
| 137 | $this->newLine(); |
||
| 138 | $this->warn("Failed to extract title from searchname: {$searchname}"); |
||
| 139 | } |
||
| 140 | $processed++; |
||
| 141 | $progressBar->advance(); |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | $cleanTitle = $titleData['title']; |
||
| 146 | |||
| 147 | // Check if we should skip (if not forcing and data exists) |
||
| 148 | // Don't skip if we're retrying failed releases (anidbid <= 0) |
||
| 149 | if (! $force && ! $missingOnly && ! $retryFailed) { |
||
| 150 | // Check if release already has complete AniList data |
||
| 151 | if ($release->anidbid > 0) { |
||
| 152 | $anidbInfo = DB::table('anidb_info') |
||
| 153 | ->where('anidbid', $release->anidbid) |
||
| 154 | ->whereNotNull('anilist_id') |
||
| 155 | ->whereNotNull('country') |
||
| 156 | ->whereNotNull('media_type') |
||
| 157 | ->first(); |
||
| 158 | |||
| 159 | if ($anidbInfo) { |
||
| 160 | $skipped++; |
||
| 161 | $processed++; |
||
| 162 | $progressBar->advance(); |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // Search AniList for this title (with rate limiting) |
||
| 169 | $this->enforceRateLimit(); |
||
| 170 | $searchResults = $populateAniList->searchAnime($cleanTitle, 1); |
||
| 171 | |||
| 172 | if (! $searchResults || empty($searchResults)) { |
||
| 173 | // Try with spaces replaced for broader matching |
||
| 174 | $altTitle = preg_replace('/\s+/', ' ', $cleanTitle); |
||
| 175 | if ($altTitle !== $cleanTitle) { |
||
| 176 | $this->enforceRateLimit(); |
||
| 177 | $searchResults = $populateAniList->searchAnime($altTitle, 1); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | if (! $searchResults || empty($searchResults)) { |
||
| 182 | $notFound++; |
||
| 183 | $failedSearchnames[] = [ |
||
| 184 | 'searchname' => $searchname, |
||
| 185 | 'reason' => 'No AniList match found', |
||
| 186 | 'cleaned_title' => $cleanTitle, |
||
| 187 | ]; |
||
| 188 | if ($this->getOutput()->isVerbose()) { |
||
| 189 | $this->newLine(); |
||
| 190 | $this->warn("No AniList match found for:"); |
||
| 191 | $this->line(" Searchname: {$searchname}"); |
||
| 192 | $this->line(" Cleaned title: {$cleanTitle}"); |
||
| 193 | } |
||
| 194 | $processed++; |
||
| 195 | $progressBar->advance(); |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | $anilistData = $searchResults[0]; |
||
| 200 | $anilistId = $anilistData['id'] ?? null; |
||
| 201 | |||
| 202 | if (! $anilistId) { |
||
| 203 | $notFound++; |
||
| 204 | $failedSearchnames[] = [ |
||
| 205 | 'searchname' => $searchname, |
||
| 206 | 'reason' => 'AniList result missing ID', |
||
| 207 | 'cleaned_title' => $cleanTitle, |
||
| 208 | ]; |
||
| 209 | if ($this->getOutput()->isVerbose()) { |
||
| 210 | $this->newLine(); |
||
| 211 | $this->warn("AniList search returned result but no ID for:"); |
||
| 212 | $this->line(" Searchname: {$searchname}"); |
||
| 213 | $this->line(" Cleaned title: {$cleanTitle}"); |
||
| 214 | } |
||
| 215 | $processed++; |
||
| 216 | $progressBar->advance(); |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Fetch full data from AniList and insert/update (with rate limiting) |
||
| 221 | // This will create/update anidb_info entry using anilist_id as anidbid if needed |
||
| 222 | $this->enforceRateLimit(); |
||
| 223 | $populateAniList->populateTable('info', $anilistId); |
||
| 224 | |||
| 225 | // Get the anidbid that was created/updated (it uses anilist_id as anidbid) |
||
| 226 | $anidbid = AnidbInfo::query() |
||
| 227 | ->where('anilist_id', $anilistId) |
||
| 228 | ->value('anidbid'); |
||
| 229 | |||
| 230 | if (! $anidbid) { |
||
| 231 | // Fallback: use anilist_id as anidbid |
||
| 232 | $anidbid = (int) $anilistId; |
||
| 233 | } |
||
| 234 | |||
| 235 | // Update release with the anidbid |
||
| 236 | Release::query() |
||
| 237 | ->where('id', $release->id) |
||
| 238 | ->update(['anidbid' => $anidbid]); |
||
| 239 | |||
| 240 | $successful++; |
||
| 241 | } catch (\Exception $e) { |
||
| 242 | // Check if this is a 429 rate limit error |
||
| 243 | if (str_contains($e->getMessage(), '429') || str_contains($e->getMessage(), 'rate limit exceeded')) { |
||
| 244 | $this->newLine(); |
||
| 245 | $this->error('AniList API rate limit exceeded (429). Stopping processing for 15 minutes.'); |
||
| 246 | $this->warn('Please wait 15 minutes before running this command again.'); |
||
| 247 | $progressBar->finish(); |
||
| 248 | $this->newLine(); |
||
| 249 | |||
| 250 | // Show summary of what was processed before the error |
||
| 251 | $this->info('Summary (before rate limit error):'); |
||
| 252 | $this->table( |
||
| 253 | ['Status', 'Count'], |
||
| 254 | [ |
||
| 255 | ['Total Processed', $processed], |
||
| 256 | ['Successful', $successful], |
||
| 257 | ['Failed', $failed], |
||
| 258 | ['Not Found', $notFound], |
||
| 259 | ['Skipped', $skipped], |
||
| 260 | ] |
||
| 261 | ); |
||
| 262 | |||
| 263 | // Show failed searchnames if any |
||
| 264 | if (!empty($failedSearchnames)) { |
||
| 265 | $this->newLine(); |
||
| 266 | $this->warn("Failed searchnames (before rate limit error):"); |
||
| 267 | $this->line("Showing up to 10 examples:"); |
||
| 268 | $examples = array_slice($failedSearchnames, 0, 10); |
||
| 269 | foreach ($examples as $item) { |
||
| 270 | $cleanedTitle = $item['cleaned_title'] ?? '(extraction failed)'; |
||
| 271 | $this->line(" - {$item['searchname']} -> {$cleanedTitle} ({$item['reason']})"); |
||
| 272 | } |
||
| 273 | if (count($failedSearchnames) > 10) { |
||
| 274 | $this->line(" ... and " . (count($failedSearchnames) - 10) . " more."); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | return self::FAILURE; |
||
| 279 | } |
||
| 280 | |||
| 281 | $failed++; |
||
| 282 | if ($this->getOutput()->isVerbose()) { |
||
| 283 | $this->newLine(); |
||
| 284 | $this->error("Error processing release ID {$release->id}: " . $e->getMessage()); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | $processed++; |
||
| 289 | $progressBar->advance(); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | $progressBar->setMessage('Complete!'); |
||
| 294 | $progressBar->finish(); |
||
| 295 | $this->newLine(2); |
||
| 296 | |||
| 297 | // Summary |
||
| 298 | $this->info('Summary:'); |
||
| 299 | $this->table( |
||
| 300 | ['Status', 'Count'], |
||
| 301 | [ |
||
| 302 | ['Total Processed', $processed], |
||
| 303 | ['Successful', $successful], |
||
| 304 | ['Failed', $failed], |
||
| 305 | ['Not Found', $notFound], |
||
| 306 | ['Skipped', $skipped], |
||
| 307 | ] |
||
| 308 | ); |
||
| 309 | |||
| 310 | // Show failed searchnames if any |
||
| 311 | if (!empty($failedSearchnames) && $notFound > 0) { |
||
| 312 | $this->newLine(); |
||
| 313 | $this->warn("Failed to fetch data for {$notFound} release(s):"); |
||
| 314 | $this->newLine(); |
||
| 315 | |||
| 316 | // Show up to 20 examples |
||
| 317 | $examples = array_slice($failedSearchnames, 0, 20); |
||
| 318 | $rows = []; |
||
| 319 | foreach ($examples as $item) { |
||
| 320 | $cleanedTitle = $item['cleaned_title'] ?? '(extraction failed)'; |
||
| 321 | $rows[] = [ |
||
| 322 | substr($item['searchname'], 0, 60) . (strlen($item['searchname']) > 60 ? '...' : ''), |
||
| 323 | substr($cleanedTitle, 0, 40) . (strlen($cleanedTitle) > 40 ? '...' : ''), |
||
| 324 | $item['reason'], |
||
| 325 | ]; |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->table( |
||
| 329 | ['Searchname', 'Cleaned Title', 'Reason'], |
||
| 330 | $rows |
||
| 331 | ); |
||
| 332 | |||
| 333 | if (count($failedSearchnames) > 20) { |
||
| 334 | $this->line("... and " . (count($failedSearchnames) - 20) . " more. Use --verbose to see all."); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | return self::SUCCESS; |
||
| 339 | } |
||
| 569 |