feat (be podcast): added third_party_track_references and celery_tasks table support

This commit is contained in:
Marco Cavalli 2025-07-03 17:47:58 +02:00
parent 2dae6e07e7
commit bcf9b5c5a7
5 changed files with 135 additions and 45 deletions

View file

@ -2,8 +2,12 @@
namespace App\Http\Controllers;
use App\Models\CeleryTask;
use App\Models\Podcast\Podcast;
use App\Models\Podcast\PodcastEpisode;
use App\Models\ThirdPartyTrackReference;
use DateTime;
use DateTimeZone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
@ -32,33 +36,6 @@ class PodcastEpisodeController extends Controller
if (!$user->id) {
throw new \Exception('You must be logged in');
}
return $this->downloadPodcastEpisode($request);
} catch (\Exception $exception) {
return response($exception->getMessage(), 500);
}
}
public function test(Request $request) {
if(isset($request->file)) {
$file = (new FileController())->store($request);
$file = json_decode($file);
$episode = PodcastEpisode::where('file_id','=',null)
->where('episode_title','=', $file->track_title)->firstOrFail();
$episode->fill(['file_id' => $file->id])->save();
}
return $request;
}
private function downloadPodcastEpisode(Request $request) {
$request->validate([
'podcast_id' => 'required',
'episode_url' => 'required',
'episode_title' => 'required',
]);
try {
$podcast = Podcast::findOrFail($request->podcast_id);
$podcastEpisode = new PodcastEpisode();
$podcastEpisode->fill([
@ -70,6 +47,70 @@ class PodcastEpisodeController extends Controller
'episode_description' => htmlentities($request->episode['description']),
])->save();
$brokerTaskId = $this->downloadPodcastEpisode($request, $podcastEpisode);
$ref = new ThirdPartyTrackReference();
$ref->fill([
'service' => 'podcast',
'foreign_id' => $podcastEpisode->id,
])->save();
$task = new CeleryTask();
$timezone = new DateTimeZone(getenv('APP_TIMEZONE'));
$task->fill([
'task_id' => $brokerTaskId,
'track_reference' => $ref->id,
'name' => 'podcast-download',
'dispatch_time' => new DateTime('now', $timezone),
'status' => 'PENDING'
])->save();
return $podcastEpisode->toJson();
} catch (\Exception $exception) {
return response($exception->getMessage(), 500);
}
}
public function test(Request $request) {
if(isset($request->file)) {
$file = (new FileController())->store($request);
$file = json_decode($file);
$episode = PodcastEpisode::where('file_id','=',null)
->where('episode_title','=', $file->track_title)->firstOrFail();
$task = $episode->third_party_track_reference->celery_task;
$task->fill([
'status' => 'SUCCESS'
])->save();
$ref = $episode->third_party_track_reference;
$timezone = new DateTimeZone(getenv('APP_TIMEZONE'));
$ref->fill([
'file_id' => $file->id,
'upload_time' => new DateTime('now', $timezone),
'status' => 'SUCCESS'
])->save();
$episode->fill(['file_id' => $file->id])->save();
}
return $request;
}
private function downloadPodcastEpisode(Request $request, $podcastEpisode) {
$request->validate([
'podcast_id' => 'required',
'episode_url' => 'required',
'episode_title' => 'required',
]);
try {
$podcast = Podcast::findOrFail($request->podcast_id);
$conn = new Celery(
config('rabbitmq.host'),
config('rabbitmq.user'),
@ -90,24 +131,9 @@ class PodcastEpisodeController extends Controller
'override_album' => 'false' //ToDo connect $album_override from imported_podcast,
];
$result = $conn->PostTask('podcast-download', $data, true, 'podcast');
$taskId = $conn->PostTask('podcast-download', $data, true, 'podcast');
return $taskId;
return $result->getId();
while (!$result->isReady()) {
sleep(1);
}
if (!$result->isSuccess()) {
//Todo: throw exception
throw new \Exception('podcast episode id:'.$podcastEpisode->id.' download failed');
}
//Todo: return ok
return $result;
// $podcastEpisode->fill([
// ''
// ]);
} catch (\Exception $exception) {
Log::error($exception->getMessage());
die($exception->getMessage());