clean up a bit download service
This commit is contained in:
parent
3d355039fe
commit
e97bbc1bc1
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace App\Command;
|
namespace App\Command;
|
||||||
|
|
||||||
use App\Constants;
|
|
||||||
use App\Entity\Episode;
|
|
||||||
use App\Repository\PodcastRepository;
|
use App\Repository\PodcastRepository;
|
||||||
use App\Service\DownloadService;
|
use App\Service\DownloadService;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
@ -24,7 +22,7 @@ class DownloadCommand extends Command
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected DownloadService $downloadService,
|
protected DownloadService $downloadService,
|
||||||
protected EntityManagerInterface $entityManager,
|
protected EntityManagerInterface $em,
|
||||||
protected PodcastRepository $podcastRepository,
|
protected PodcastRepository $podcastRepository,
|
||||||
protected ParameterBagInterface $parameterBag,
|
protected ParameterBagInterface $parameterBag,
|
||||||
) {
|
) {
|
||||||
|
@ -55,30 +53,22 @@ class DownloadCommand extends Command
|
||||||
return Command::FAILURE;
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$filepath = $this->downloadService->download($url);
|
$episode = $this->downloadService->download($url);
|
||||||
if (false === $filepath) {
|
if (false === $episode) {
|
||||||
$io->error('Download failed.');
|
$io->error('Download failed.');
|
||||||
|
|
||||||
return Command::FAILURE;
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$filename = basename($filepath);
|
|
||||||
$publicFilepath =
|
|
||||||
$this->parameterBag->get('kernel.project_dir')
|
|
||||||
.'/'
|
|
||||||
.Constants::BASE_PUBLIC_DIR
|
|
||||||
.Constants::FILES_BASE_PATH
|
|
||||||
.$filename;
|
|
||||||
rename($filepath, $publicFilepath);
|
|
||||||
|
|
||||||
$episode = new Episode();
|
|
||||||
$episode->setTitle($input->getOption('title') ?: $filename);
|
|
||||||
$episode->setDescription($input->getOption('description'));
|
|
||||||
$episode->setAudioFilename($filename);
|
|
||||||
$episode->setPodcast($podcast);
|
$episode->setPodcast($podcast);
|
||||||
$episode->setPublicationDate(date_create());
|
$episode->setDescription($input->getOption('description'));
|
||||||
$this->entityManager->persist($episode);
|
// Override only if the option has been passed, because the download service sets a default title.
|
||||||
$this->entityManager->flush();
|
if (($title = $input->getOption('title')) !== null) {
|
||||||
|
$episode->setTitle($title);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->persist($episode);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
$io->success("New Episode '{$episode->getTitle()}' added.");
|
$io->success("New Episode '{$episode->getTitle()}' added.");
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
use App\Constants;
|
use App\Constants;
|
||||||
use App\Entity\Podcast;
|
|
||||||
use App\Repository\EpisodeRepository;
|
use App\Repository\EpisodeRepository;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
@ -99,7 +98,7 @@ class Episode
|
||||||
|
|
||||||
public function getAudioUrl(): ?string
|
public function getAudioUrl(): ?string
|
||||||
{
|
{
|
||||||
if ($this->audioFilename === null) {
|
if (null === $this->audioFilename) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,21 +2,46 @@
|
||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Constants;
|
||||||
|
use App\Entity\Episode;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
|
||||||
|
/** Generate episodes from external sources. */
|
||||||
class DownloadService
|
class DownloadService
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected LoggerInterface $logger,
|
protected LoggerInterface $logger,
|
||||||
protected YoutubeService $youtubeService,
|
protected YoutubeService $youtubeService,
|
||||||
) {}
|
#[Autowire('%kernel.project_dir%')] protected string $projectDir,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download the audio from this URL and return its filepath.
|
* Download the audio from this URL and save the corresponding episode.
|
||||||
*/
|
*/
|
||||||
public function download(string $url): string|false
|
public function download(string $url): Episode|false
|
||||||
{
|
{
|
||||||
// For now assume every URL is a Youtube URL.
|
// For now assume every URL is a Youtube URL.
|
||||||
return $this->youtubeService->download($url);
|
$filepath = $this->youtubeService->download($url);
|
||||||
|
if (false === $filepath) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = basename($filepath);
|
||||||
|
$publicFilepath =
|
||||||
|
$this->projectDir
|
||||||
|
.'/'
|
||||||
|
.Constants::BASE_PUBLIC_DIR
|
||||||
|
.Constants::FILES_BASE_PATH
|
||||||
|
.$filename;
|
||||||
|
rename($filepath, $publicFilepath);
|
||||||
|
|
||||||
|
$episode = new Episode();
|
||||||
|
$episode->setTitle($filename);
|
||||||
|
$episode->setAudioFilename($filename);
|
||||||
|
$episode->setPublicationDate(date_create());
|
||||||
|
|
||||||
|
return $episode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ class YoutubeService
|
||||||
'-x',
|
'-x',
|
||||||
'-O', 'after_move:filepath', '--restrict-filenames',
|
'-O', 'after_move:filepath', '--restrict-filenames',
|
||||||
'-P', sys_get_temp_dir(),
|
'-P', sys_get_temp_dir(),
|
||||||
escapeshellarg($url),
|
$url,
|
||||||
]);
|
]);
|
||||||
try {
|
try {
|
||||||
$process->mustRun();
|
$process->mustRun();
|
||||||
|
|
Loading…
Reference in a new issue