-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to generate external player on each video imported …
…from YouTube
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pumukit\YoutubeBundle\Command; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Pumukit\SchemaBundle\Document\MultimediaObject; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class ImportYouTubeExternalPlayerCommand extends Command | ||
{ | ||
private $documentManager; | ||
|
||
public function __construct(DocumentManager $documentManager) | ||
{ | ||
$this->documentManager = $documentManager; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('pumukit:youtube:import:externalplayer') | ||
->setDescription('Add external player to multimedia objects to show Youtube videos') | ||
->setHelp( | ||
<<<'EOT' | ||
Add external player to multimedia objects to show Youtube videos | ||
Usage: php bin/console pumukit:youtube:import:externalplayer | ||
|
||
EOT | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$multimediaObjects = $this->obtainMultimediaObjects(); | ||
|
||
$progressBar = new ProgressBar($output, count($multimediaObjects)); | ||
$progressBar->start(); | ||
|
||
$count = 0; | ||
foreach ($multimediaObjects as $multimediaObject) { | ||
$progressBar->advance(); | ||
++$count; | ||
|
||
$this->updateMultimediaObject($multimediaObject); | ||
|
||
if (0 == $count % 50) { | ||
$this->documentManager->flush(); | ||
} | ||
} | ||
|
||
$progressBar->finish(); | ||
|
||
$this->documentManager->flush(); | ||
$this->documentManager->clear(); | ||
|
||
return 0; | ||
} | ||
|
||
private function obtainMultimediaObjects() | ||
{ | ||
return $this->documentManager->getRepository(MultimediaObject::class)->findBy([ | ||
'properties.youtube_import_id' => ['$exists' => true], | ||
'properties.externalplayer' => ['$exists' => false], | ||
]); | ||
} | ||
|
||
private function updateMultimediaObject(MultimediaObject $multimediaObject): void | ||
{ | ||
$externalPlayer = 'https://www.youtube.com/embed/'.$multimediaObject->getProperty('youtube_import_id'); | ||
$multimediaObject->setProperty('externalplayer', $externalPlayer); | ||
$multimediaObject->setType(MultimediaObject::TYPE_EXTERNAL); | ||
} | ||
} |