Podcast RSS fixed

This commit is contained in:
NH Gooi
2024-05-06 13:29:40 +02:00
parent 2c5b6b3ac6
commit 75e9fc79f3
3 changed files with 66 additions and 32 deletions

View File

@@ -3,21 +3,20 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
error_reporting(E_ALL);
ini_set('display_errors', true);
class PodcastController extends Controller
{
public static $BASE_SQL = <<<QUERY
SELECT `podcast`.`id`, `podcast`.`soundfilename`,
`podcast_meta`.`creationdt` AS `created`, `podcast_meta`.`title`, `podcast_meta`.`content`, `podcast_meta`.`program`, `podcast_meta`.`imagefile`, `podcast_meta`.`imagecaption`,
SELECT `podcast`.`id`, `podcast`.`soundfilename`, `podcast`.`duration`,
`podcast_meta`.`creationdt` AS `created`, `podcast_meta`.`title`, `podcast_meta`.`content`, `podcast_meta`.`program`,
`podcast_meta`.`imagefile`, `podcast_meta`.`imagecaption`,
`programs`.`longname` AS `program_name`, `programs`.`description` AS `program_description`
FROM `podcast`
INNER JOIN `podcast_meta` ON `podcast`.`id` = `podcast_meta`.`podcast`
LEFT JOIN `programs` ON `podcast_meta`.`program` = `programs`.`id`
WHERE `podcast_meta`.`active` = 1 AND `podcast_meta`.`title` <> '' AND `podcast_meta`.`content` <> ''
WHERE `podcast_meta`.`active` = 1 AND `podcast_meta`.`public` = 1
QUERY;
/**
@@ -37,6 +36,39 @@ QUERY;
return $this->getPodcastList($request, null, []);
}
/**
* RSS-feed van alle podcasts
*/
public function rss(Request $request) {
$page = (int)$request->get('page', 1);
if($page <= 0) {
return abort(400);
}
$podcasts = $this->retrievePodcasts($page, $count = 20, $filter = null, $params = []);
$view = view('rss.podcasts')->with('podcasts', $podcasts)->with('url', $request->url())->with('page', $page);
return response($view)->header('Content-Type', 'application/xml');
}
/**
* RSS-feed van alle podcasts van een programma (bv. podcastserie)
*/
public function program_rss(Request $request, $program) {
$page = (int)$request->get('page', 1);
if($page <= 0) {
return abort(400);
}
$programQuery = app('db')->select('SELECT `programs`.`id` AS `id`, `programs`.`longname` AS `name`, `programs`.`description` AS `description`, `programs`.`tagline` AS `tagline`, `programs`.`description` AS `description`, `programs`.`email` FROM `programs` WHERE `programs`.`id` = :id', ['id' => $program]);
$programDetails = new \Model\Program($programQuery[0]);
$filter = '`podcast_meta`.`program` = :program';
$params = ['program' => (int)$program];
$podcasts = $this->retrievePodcasts($page, $count = 1000, $filter, $params);
$view = view('rss.podcasts')->with('program', $programDetails)->with('podcasts', $podcasts)->with('url', $request->url())->with('page', $page);
return response($view)->header('Content-Type', 'application/xml');
}
/**
* Lijst van alle podcasts voor een specifiek programma
*/
@@ -70,22 +102,17 @@ QUERY;
public function findpodcast(Request $request, $query) {
$page = (int)$request->get('pagina', 1);
$count = (int)$request->get('aantal', 15);
if($page <= 0) {
return abort(400);
}
$start = ($page - 1) * $count;
$query = strip_tags(urldecode($query));
$searchRange = [
(new \DateTimeImmutable())->setDate(date('Y') + 1 - $page, 1, 1),
(new \DateTimeImmutable())->setDate(date('Y') + 2 - $page, 1, 1)];
$podcasts = app('db')->select(self::$BASE_SQL
. ' AND `podcast_meta`.`creationdt` >= :startRange AND `podcast_meta`.`creationdt` <= :endRange'
. ' AND MATCH(`podcast_meta`.`title`, `podcast_meta`.`content`) AGAINST(:query IN BOOLEAN MODE)'
. ' AND MATCH(`podcast_meta`.`title`, `podcast_meta`.`content`) AGAINST(:query IN NATURAL LANGUAGE MODE)'
. ' ORDER BY `creationdt` DESC'
. ' LIMIT 0, 20', [
'startRange' => $searchRange[0]->format('Y-m-d'),
'endRange' => $searchRange[1]->format('Y-m-d'),
. ' LIMIT ' . $start . ', ' . $count, [
'query' => $query
]);
@@ -108,6 +135,12 @@ QUERY;
return abort(400);
}
$result = $this->retrievePodcasts($page, $count, $filter, $params);
return response()->json(['page' => $page, 'count' => $count, 'podcasts' => $result]);
}
private function retrievePodcasts($page, $count, $filter, $params)
{
$start = ($page - 1) * $count;
$podcasts = app('db')->select(self::$BASE_SQL
. ($filter ? ' AND (' . $filter . ')' : '')
@@ -120,7 +153,7 @@ QUERY;
$result[] = new \Model\Podcast($podcast);
}
return response()->json(['page' => $page, 'count' => $count, 'podcasts' => $result]);
return $result;
}
/**
@@ -140,7 +173,7 @@ QUERY;
}
return response()->download($podcast->getSoundfile(),
'6FM Gemist - ' . str_replace('/', '-', $podcast->title) . '.mp3');
'NH Gooi Gemist - ' . str_replace('/', '-', $podcast->title) . '.mp3');
}
/***
@@ -174,7 +207,7 @@ QUERY;
public function latestNews(Request $request) {
$filename = '/var/audio/regionieuws.mp3';
return response()->download($filename,
'6FM Regionieuws ' . date('d-m-Y') . '.mp3');
'NH Gooi Regionieuws ' . date('d-m-Y') . '.mp3');
$size = filesize($filename);
$content = fread($file, $size);
@@ -196,12 +229,8 @@ QUERY;
$files = [];
$size = 0;
while($duration--) {
$file = '/var/audio/uitzending/' . $currentHour->format('Ymd_Hi');
if(file_exists($file . '.mp3')) {
$file .= '.mp3';
} else if(file_exists($file . '.MP3')) {
$file .= '.MP3';
} else {
$file = '/var/audio/uitzending/' . $currentHour->format('Ymd_Hi') . '.mp3';
if(!file_exists($file)) {
return abort(404);
}

View File

@@ -1,5 +1,6 @@
<?php header('Access-Control-Allow-Origin: *'); ?>
<?xml version="1.0" encoding="UTF-8"?>
<?php header('Content-Type: text/xml'); ?>
<{{ '' /* avoid PHP parsing this as short open tag */ }}?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"

View File

@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?php header('Access-Control-Allow-Origin: *'); ?>
<?php header('Content-Type: text/xml'); ?>
<{{ '' /* avoid PHP parsing this as short open tag */ }}?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:spotify="http://www.spotify.com/ns/rss"
@@ -46,6 +48,8 @@
<itunes:explicit>no</itunes:explicit>
@if($podcast->image)
<itunes:image href="https://nhgooi.nl{{$podcast->image->url}}"/>
@elseif($podcast->program && $podcast->program->image)
<itunes:image href="https://nhgooi.nl{{$podcast->program->image->url}}"/>
@endif
</item>
@endforeach