upd: import-bible
rename ingest-bible to import-bible
This commit is contained in:
@@ -12,13 +12,13 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
#[AsCommand(
|
#[AsCommand(
|
||||||
name: 'IngestBible',
|
name: 'ImportBible',
|
||||||
aliases: ['app:ingest-bible'],
|
aliases: ['app:import-bible'],
|
||||||
description: 'Script used to crawl a directory and ingest Bible passages and save them to the database.',
|
description: 'Script used to crawl a directory and ingest Bible passages and save them to the database.',
|
||||||
)]
|
)]
|
||||||
class IngestBibleCommand extends Command
|
class IngestBibleCommand extends Command
|
||||||
{
|
{
|
||||||
private array $files = [];
|
private ?object $json = null;
|
||||||
|
|
||||||
private SymfonyStyle $io;
|
private SymfonyStyle $io;
|
||||||
|
|
||||||
@@ -54,43 +54,40 @@ class IngestBibleCommand extends Command
|
|||||||
|
|
||||||
$this->io->note("Crawling {$this->dir} and ingesting Bible passages...");
|
$this->io->note("Crawling {$this->dir} and ingesting Bible passages...");
|
||||||
|
|
||||||
$json = json_decode(file_get_contents("{$this->dir}/esv-bible.json"));
|
$this->json = json_decode(file_get_contents("{$this->dir}/esv-bible.json"));
|
||||||
foreach ($json as $book => $data) {
|
foreach ($this->json as $book => $data) {
|
||||||
$index = $data->index;
|
$index = $data->index;
|
||||||
$chapters = $data->chapters;
|
$chapters = $data->chapters;
|
||||||
$label = $data->label;
|
$label = $data->label;
|
||||||
foreach ($data->text as $chapter => $verses) {
|
foreach ($data->text as $chapter => $verses) {
|
||||||
foreach ($verses as $idx => $verse) {
|
foreach ($verses as $idx => $verseText) {
|
||||||
print "$book $chapter:$idx\n";
|
print "$book $chapter:".($idx + 1).PHP_EOL;
|
||||||
|
$bible = $this->processFile($this->json, $book, $chapter, $idx);
|
||||||
|
|
||||||
|
$this->emi->persist($bible);
|
||||||
}
|
}
|
||||||
|
$this->emi->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processFile(string $file)
|
private function processFile(object &$json, string $book, int $chapter, int $verse): Bible
|
||||||
{
|
{
|
||||||
$bible = new Bible();
|
$bible = new Bible();
|
||||||
|
|
||||||
|
$bible->setChapter((int) $chapter);
|
||||||
|
$bible->setVerse((int) ($verse + 1));
|
||||||
|
$bible->setLabel($json->{$book}->label);
|
||||||
|
$bible->setBookIndex((int) $json->{$book}->index);
|
||||||
|
$bible->setContent($json->{$book}->text->$chapter[$verse]);
|
||||||
if (preg_match("/(\d)([^\d]+)/", $book, $match)) {
|
if (preg_match("/(\d)([^\d]+)/", $book, $match)) {
|
||||||
$book = $match[1]." ".$match[2];
|
$book = $match[1]." ".$match[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
$bible->setBook($book);
|
$bible->setBook($book);
|
||||||
$bible->setBookIndex($ndx);
|
|
||||||
$bible->setChapter($chapter);
|
|
||||||
|
|
||||||
$content = file_get_contents($file);
|
return $bible;
|
||||||
foreach(explode("\n", $content) as $verse) {
|
|
||||||
$match = [];
|
|
||||||
if (preg_match("/^([\d]+). ([^\d]+)/", $verse, $match)) {
|
|
||||||
$bible->setVerse($match[1]);
|
|
||||||
$bible->setContent($match[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->emi->persist($bible);
|
|
||||||
$this->emi->flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user