From 54521ffe7c9ee9c42eb974552dd91a94b517dca2 Mon Sep 17 00:00:00 2001 From: Thorsten Ortlepp Date: Fri, 14 May 2021 14:35:43 +0200 Subject: Fix for MP3 files in feed --- README | 2 +- feed2podcast/feed2podcast.php | 44 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README b/README index b644639..7b4c19f 100644 --- a/README +++ b/README @@ -6,7 +6,7 @@ feed2podcast --------- A simple script to convert an RSS feed into a podcast. Designed for Deutschlandfunk Kalenderblatt which is only available online and as -RSS feed but not as podcast. +RSS feed but not as podcast. Requires PHP 8. rssfilter diff --git a/feed2podcast/feed2podcast.php b/feed2podcast/feed2podcast.php index 0c16481..d7f137c 100644 --- a/feed2podcast/feed2podcast.php +++ b/feed2podcast/feed2podcast.php @@ -64,16 +64,31 @@ foreach ($xml->channel->item as $item) { // Only add episode to feed if media file is available if ($content[0] != 'XXX') { $id = md5($item->guid); - $filename = $id.'.aac'; + $filename = ''; + $filetype = ''; - // Download episode if not yet done - if (!file_exists($downloads.'/'.$filename)) { - foreach (glob($temp.'/*') as $file) { - if (is_file($file)) { - unlink($file); + if (str_ends_with($content[1], '.mp3')) { + $filename = $id.'.mp3'; + $filetype = 'audio/mpeg'; + + if (!file_exists($downloads.'/'.$filename)) { + downloadSingleMediaFile($content[1], $id, $downloads); + } + + } else { + $filename = $id.'.aac'; + $filetype = 'audio/aac'; + + // Download episode if not yet done + if (!file_exists($downloads.'/'.$filename)) { + foreach (glob($temp.'/*') as $file) { + if (is_file($file)) { + unlink($file); + } } + downloadMediaFile($content[1], $id, $temp, $downloads); } - downloadMediaFile($content[1], $id, $temp, $downloads); + } array_push($containing, $downloads.'/'.$filename); @@ -85,7 +100,7 @@ foreach ($xml->channel->item as $item) { echo ' '.$item->description.''; echo ' '.$item->pubDate.''; echo ' '.$item->guid.''; - echo ' '; + echo ' '; echo ' '.$content[0].''; echo ' '.$content[2].''; echo ''; @@ -167,6 +182,19 @@ function downloadMediaFile($url, $id, $temp, $downloads) { } +// Download episode media file +function downloadSingleMediaFile($url, $id, $downloads) { + $outfile = fopen($downloads.'/'.$id.'.mp3', 'wb') or exit('File open failed'); + $curl = curl_init(); + curl_setopt($curl, CURLOPT_FILE, $outfile); + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_URL, $url); + curl_exec($curl); + curl_close($curl); + fclose($outfile); +} + + // Download URL and return content function download($url) { $curl = curl_init(); -- cgit v1.2.3