feat(podcast): created Models and Controllers
This commit is contained in:
parent
e4a8a7605a
commit
15d256303f
6 changed files with 263 additions and 0 deletions
10
app/Http/Controllers/ImportedPodcastController.php
Normal file
10
app/Http/Controllers/ImportedPodcastController.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ImportedPodcastController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
73
app/Http/Controllers/PodcastController.php
Normal file
73
app/Http/Controllers/PodcastController.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Podcast\Podcast;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class PodcastController extends Controller
|
||||
{
|
||||
public function index(Request $request) {
|
||||
if (!isset($request->per_page) || is_null($request)) {
|
||||
$pagination = 5;
|
||||
} else {
|
||||
$pagination = $request->per_page;
|
||||
}
|
||||
|
||||
return Podcast::searchFilter($request)
|
||||
->with(['episodes', 'imported'])
|
||||
->paginate($pagination)
|
||||
->toJson();
|
||||
}
|
||||
|
||||
public function store(Request $request) {
|
||||
return $this->save($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used to update a smart block
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function update(Request $request) {
|
||||
return $this->save($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a smart block, cleaning also cc_blockcontent and cc_blockcriteria
|
||||
* @param $id
|
||||
* @return int
|
||||
*/
|
||||
public function delete($id) {
|
||||
return Podcast::with(['content', 'criteria'])::destroy($id);
|
||||
}
|
||||
|
||||
protected function save(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$xml = simplexml_load_file($request->url);
|
||||
$itunes = $xml->channel->children('itunes', TRUE);
|
||||
|
||||
$dbPodcast = Podcast::firstOrNew(['id' => $request->id]);
|
||||
$dbPodcast->fill([
|
||||
'url' => $request->url,
|
||||
'title' => $xml->channel->title,
|
||||
'creator' => $itunes->author,
|
||||
'description' => $itunes->subtitle,
|
||||
'language' => $xml->channel->language,
|
||||
'copyright' => $xml->channel->copyright,
|
||||
'link' => $xml->channel->link,
|
||||
'itunes_author'=> $itunes->author,
|
||||
'itunes_keywords' => '',
|
||||
'itunes_summary' => $itunes->summary,
|
||||
'itunes_subtitle' => $itunes->subtitle,
|
||||
'itunes_category' => '',
|
||||
'itunes_explicit' => $itunes->explicit,
|
||||
'owner' => $user->id,
|
||||
])->save();
|
||||
|
||||
return $dbPodcast->toJson();
|
||||
}
|
||||
}
|
74
app/Http/Controllers/PodcastEpisodeController.php
Normal file
74
app/Http/Controllers/PodcastEpisodeController.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
25
app/Models/Podcast/ImportedPodcast.php
Normal file
25
app/Models/Podcast/ImportedPodcast.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Podcast;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ImportedPodcast extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'imported_podcast';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'auto_ingest',
|
||||
'auto_ingest_timestamp',
|
||||
'album_override',
|
||||
'podcast_id'
|
||||
];
|
||||
|
||||
public function podcast() {
|
||||
return $this->belongsTo(Podcast::class);
|
||||
}
|
||||
}
|
53
app/Models/Podcast/Podcast.php
Normal file
53
app/Models/Podcast/Podcast.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Podcast;
|
||||
|
||||
use App\Filters\PlaylistFilter;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Podcast extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'podcast';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'url',
|
||||
'title',
|
||||
'creator',
|
||||
'description',
|
||||
'language',
|
||||
'copyright',
|
||||
'link',
|
||||
'itunes_author',
|
||||
'itunes_keywords',
|
||||
'itunes_summary',
|
||||
'itunes_subtitle',
|
||||
'itunes_category',
|
||||
'itunes_explicit',
|
||||
'owner',
|
||||
];
|
||||
|
||||
|
||||
public function owner() {
|
||||
return $this->belongsTo(User::class, 'owner');
|
||||
}
|
||||
|
||||
public function episodes() {
|
||||
return $this->hasMany(PodcastEpisode::class, 'podcast_id');
|
||||
}
|
||||
|
||||
public function imported()
|
||||
{
|
||||
return $this->hasOne(ImportedPodcast::class, 'podcast_id');
|
||||
}
|
||||
|
||||
public function scopeSearchFilter($query, $request) {
|
||||
$filters = new PlaylistFilter();
|
||||
return $filters->apply($query, $request);
|
||||
}
|
||||
}
|
28
app/Models/Podcast/PodcastEpisode.php
Normal file
28
app/Models/Podcast/PodcastEpisode.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Podcast;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PodcastEpisode extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'podcast_episodes';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'file_id',
|
||||
'podcast_id',
|
||||
'publication_date',
|
||||
'download_url',
|
||||
'episode_guid',
|
||||
'episode_title',
|
||||
'episode_description',
|
||||
];
|
||||
|
||||
public function podcast() {
|
||||
return $this->belongsTo(Podcast::class);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue