108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Podcast\Podcast;
|
|
use App\Models\Podcast\PodcastEpisode;
|
|
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');
|
|
}
|
|
return $this->downloadPodcastEpisode($request);
|
|
} catch (\Exception $exception) {
|
|
return response($exception->getMessage(), 500);
|
|
}
|
|
}
|
|
|
|
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([
|
|
'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();
|
|
|
|
$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,
|
|
];
|
|
|
|
$result = $conn->PostTask('podcast-download', $data, true, 'podcast');
|
|
|
|
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());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|