sintonia_webapp/app/Http/Controllers/PodcastEpisodeController.php

74 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Podcast\Podcast;
use App\Models\Podcast\PodcastEpisode;
use Illuminate\Http\Request;
use Celery\Celery;
use Illuminate\Support\Facades\Log;
class PodcastEpisodeController extends Controller
{
private static $_CELERY_MESSAGE_TIMEOUT = 900000; // 15 minutes
public function downloadPodcastEpisode(Request $request) {
$request->validate([
'podcast_id' => 'required',
'download_url' => 'required',
'episode_title' => 'required',
]);
try {
$podcast = Podcast::find($request->podcast_id);
$podcastEpisode = new PodcastEpisode();
$podcastEpisode->fill([
'podcast_id' => $request->podcast_id,
'publication_date' =>$request->publication_date,
'download_url' => $request->download_url,
'episode_guid' => $request->episode_guid,
'episode_title' => $request->episode_title,
'episode_description' => $request->episode_description,
])->save();
$conn = new Celery(
config('rabbitmq.host'),
config('rabbitmq.user'),
config('rabbitmq.password'),
config('rabbitmq.vhost'),
'podcast',
'podcast',
config('rabbitmq.port'),
false,
self::$_CELERY_MESSAGE_TIMEOUT
);
$data = [
'episode_id' => $podcastEpisode->id,
'episode_url' => $podcastEpisode->url,
'episode_title' => $podcastEpisode->episode_title,
'podcast_name' => $podcast->title,
'override_album' => 'false' //ToDo connect $album_override from imported_podcast,
];
$result = $conn->PostTask('tasks.download', $data);
while (!$result->isReady()) {
sleep(1);
}
if (!$result->isSuccess()) {
//Todo: throw exception
throw new \Exception('podcast episode id:'.$podcastEpisode->id.' download failed');
}
//Todo: return ok
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
}
}