Skip to content

Commit f3e8fea

Browse files
committed
Test auto deployment
1 parent 01793f1 commit f3e8fea

File tree

4 files changed

+154
-4
lines changed

4 files changed

+154
-4
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class FullLibriVoxImport extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'librivox:full-import {--limit=100 : Number of audiobooks to fetch per batch} {--delay=1 : Delay in seconds between batches}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Continuously fetches audiobooks from LibriVox (Archive.org) until no new books are found.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return int
28+
*/
29+
public function handle()
30+
{
31+
$limit = (int) $this->option('limit');
32+
$delay = (int) $this->option('delay');
33+
$offset = 0;
34+
$totalFetched = 0;
35+
$continueFetching = true;
36+
37+
$this->info("Starting full LibriVox audiobook import...");
38+
39+
while ($continueFetching) {
40+
$this->info("Fetching batch from offset {$offset} with limit {$limit}...");
41+
42+
// Call the existing librivox:fetch command
43+
$resultCode = Artisan::call('librivox:fetch', [
44+
'--limit' => $limit,
45+
'--offset' => $offset,
46+
]);
47+
48+
$output = Artisan::output();
49+
$this->info($output);
50+
51+
// Check if any audiobooks were actually fetched in this batch
52+
// We need to parse the output to determine this.
53+
// The librivox:fetch command outputs "X audiobooks fetched from API."
54+
preg_match('/(\d+) audiobooks fetched from API\./', $output, $matches);
55+
$fetchedInBatch = isset($matches[1]) ? (int) $matches[1] : 0;
56+
57+
if ($fetchedInBatch > 0) {
58+
$totalFetched += $fetchedInBatch;
59+
$offset += $limit;
60+
$this->info("Fetched {$fetchedInBatch} audiobooks in this batch. Total fetched: {$totalFetched}.");
61+
sleep($delay); // Pause to respect API rate limits
62+
} else {
63+
$this->info("No new audiobooks found in this batch. Ending import.");
64+
$continueFetching = false;
65+
}
66+
67+
if ($resultCode !== 0) {
68+
$this->error("librivox:fetch command failed with code {$resultCode}. Aborting full import.");
69+
$continueFetching = false;
70+
}
71+
}
72+
73+
$this->info("Full LibriVox audiobook import complete. Total audiobooks processed: {$totalFetched}.");
74+
75+
return Command::SUCCESS;
76+
}
77+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class FullLibriVoxSectionsImport extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'librivox:full-sections-import {--limit=100 : Number of audiobooks to process per batch for sections} {--delay=1 : Delay in seconds between batches}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Continuously fetches and stores audiobook sections for existing audiobooks.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return int
28+
*/
29+
public function handle()
30+
{
31+
$limit = (int) $this->option('limit');
32+
$delay = (int) $this->option('delay');
33+
$totalProcessed = 0;
34+
$continueProcessing = true;
35+
36+
$this->info("Starting full LibriVox audiobook sections import...");
37+
38+
while ($continueProcessing) {
39+
$this->info("Processing batch with limit {$limit} for sections...");
40+
41+
// Call the existing librivox:fetch-sections command
42+
$resultCode = Artisan::call('librivox:fetch-sections', [
43+
'--limit' => $limit,
44+
]);
45+
46+
$output = Artisan::output();
47+
$this->info($output);
48+
49+
// Check if any audiobooks were processed in this batch
50+
// The librivox:fetch-sections command outputs "Processed X audiobooks."
51+
preg_match('/Processed (\d+) audiobooks\./', $output, $matches);
52+
$processedInBatch = isset($matches[1]) ? (int) $matches[1] : 0;
53+
54+
if ($processedInBatch > 0) {
55+
$totalProcessed += $processedInBatch;
56+
$this->info("Processed sections for {$processedInBatch} audiobooks in this batch. Total processed: {$totalProcessed}.");
57+
sleep($delay); // Pause to respect API rate limits
58+
} else {
59+
$this->info("No audiobooks found needing section processing in this batch. Ending import.");
60+
$continueProcessing = false;
61+
}
62+
63+
if ($resultCode !== 0) {
64+
$this->error("librivox:fetch-sections command failed with code {$resultCode}. Aborting full import.");
65+
$continueProcessing = false;
66+
}
67+
}
68+
69+
$this->info("Full LibriVox audiobook sections import complete. Total audiobooks whose sections were processed: {$totalProcessed}.");
70+
71+
return Command::SUCCESS;
72+
}
73+
}

app/Http/Controllers/AudiobookController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public function index(Request $request)
5858
->orderBy('language')
5959
->pluck('language');
6060

61-
// Fetch counts for the features widget
62-
$totalAudiobooks = Audiobook::count();
63-
$uniqueLanguages = Audiobook::distinct('language')->whereNotNull('language')->count();
61+
// Fetch counts for the features widget, only counting audiobooks with a slug
62+
$totalAudiobooks = Audiobook::whereNotNull('slug')->count();
63+
$uniqueLanguages = Audiobook::whereNotNull('slug')->distinct('language')->whereNotNull('language')->count();
6464
// Assuming narrators are stored in the 'narrator' column of the audiobooks table
65-
$uniqueReaders = Audiobook::distinct('narrator')->whereNotNull('narrator')->count();
65+
$uniqueReaders = Audiobook::whereNotNull('slug')->distinct('narrator')->whereNotNull('narrator')->count();
6666

6767

6868
return view('audiobooks.index', compact('audiobooks', 'categories', 'latestAudiobooks', 'languages', 'totalAudiobooks', 'uniqueLanguages', 'uniqueReaders'));

database/migrations/2025_06_04_140700_drop_old_section_unique_constraint.php

Whitespace-only changes.

0 commit comments

Comments
 (0)