150 lines
4.8 KiB
PHP
150 lines
4.8 KiB
PHP
<?php
|
|
|
|
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;
|
|
use Celery\Celery;
|
|
|
|
class PodcastEpisodeController extends Controller
|
|
{
|
|
private static $_CELERY_MESSAGE_TIMEOUT = 900000; // 15 minutes
|
|
|
|
public function index(Request $request) {
|
|
$user = Auth::user();
|
|
try {
|
|
if (!$user->id) {
|
|
throw new \Exception('You must be logged in');
|
|
}
|
|
return PodcastEpisode::where('podcast_id','=',$request->podcast_id)->get()->toJson();
|
|
|
|
} catch (\Exception $exception) {
|
|
return response($exception->getMessage(), 500);
|
|
}
|
|
}
|
|
|
|
public function store(Request $request) {
|
|
$user = Auth::user();
|
|
try {
|
|
if (!$user->id) {
|
|
throw new \Exception('You must be logged in');
|
|
}
|
|
|
|
$podcastEpisode = new PodcastEpisode();
|
|
$podcastEpisode->fill([
|
|
'podcast_id' => $request->podcast_id,
|
|
'publication_date' =>$request->episode['pubDate'],
|
|
'download_url' => $request->episode['link'],
|
|
'episode_guid' => $request->episode['guid'],
|
|
'episode_title' => $request->episode['title'],
|
|
'episode_description' => htmlentities($request->episode['description']),
|
|
])->save();
|
|
|
|
$brokerTask = $this->downloadPodcastEpisode($request, $podcastEpisode);
|
|
|
|
$ref = new ThirdPartyTrackReference();
|
|
$ref->fill([
|
|
'service' => 'podcast',
|
|
'foreign_id' => $podcastEpisode->id,
|
|
'file_id' => null
|
|
])->save();
|
|
|
|
$task = new CeleryTask();
|
|
$timezone = new DateTimeZone(getenv('APP_TIMEZONE'));
|
|
$task->fill([
|
|
'task_id' => $brokerTask->task_id,
|
|
'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)) {
|
|
try {
|
|
$file = (new FileController())->store($request);
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
}
|
|
$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'),
|
|
config('rabbitmq.password'),
|
|
config('rabbitmq.vhost'),
|
|
'podcast',
|
|
'podcast',
|
|
config('rabbitmq.port'),
|
|
false,
|
|
self::$_CELERY_MESSAGE_TIMEOUT
|
|
);
|
|
|
|
$data = [
|
|
'episode_id' => $podcastEpisode->id,
|
|
'episode_url' => $request->episode_url,
|
|
'episode_title' => $podcastEpisode->episode_title,
|
|
'podcast_name' => $podcast->title,
|
|
'override_album' => 'false' //ToDo connect $album_override from imported_podcast,
|
|
];
|
|
|
|
$taskId = $conn->PostTask('podcast-download', $data, true, 'podcast');
|
|
return $taskId;
|
|
|
|
} catch (\Exception $exception) {
|
|
Log::error($exception->getMessage());
|
|
die($exception->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|