From 4d685b3be98d79794162c8c2c4fd349376d2fa45 Mon Sep 17 00:00:00 2001 From: nvpie exozyme Date: Sat, 1 Jul 2023 08:22:14 +0000 Subject: [PATCH] youtube rss support added --- parse_rss.php | 110 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 22 deletions(-) diff --git a/parse_rss.php b/parse_rss.php index e8b8c91..63e47a3 100644 --- a/parse_rss.php +++ b/parse_rss.php @@ -1,34 +1,100 @@ channel->title; - $feeds = array(); + // Check if the provided URL is a YouTube RSS feed + if (strpos($rssUrl, 'youtube.com/feeds/videos.xml') !== false) { + // Load the YouTube RSS feed + $feedContent = file_get_contents($rssUrl); - foreach ($feed->channel->item as $item) { - $title = (string) $item->title; - $date = (string) $item->pubDate; - $content = (string) $item->description; + // Extract the channel title + preg_match('/(.*?)<\/title>/i', $feedContent, $titleMatches); + $channelTitle = $titleMatches[1]; - $feeds[] = array( - 'title' => $title, - 'date' => $date, - 'content' => $content + // Extract the videos + preg_match_all('/<entry>(.*?)<\/entry>/is', $feedContent, $videoMatches); + $videoData = $videoMatches[1]; + + if (!empty($videoData)) { + $feeds = array(); + + foreach ($videoData as $video) { + // Extract the video title + preg_match('/<title[^>]*>(.*?)<\/title>/i', $video, $titleMatches); + $title = $titleMatches[1]; + + // Extract the published date + preg_match('/<published>(.*?)<\/published>/i', $video, $dateMatches); + $date = $dateMatches[1]; + + // Extract the video ID + preg_match('/<yt:videoId>(.*?)<\/yt:videoId>/i', $video, $videoIdMatches); + $videoId = $videoIdMatches[1]; + + // Build the video URL + $videoUrl = 'https://www.youtube.com/watch?v=' . $videoId; + + // Extract the video descriptions + preg_match_all('/<media:description>(.*?)<\/media:description>/is', $video, $descriptionMatches); + $descriptions = $descriptionMatches[1]; + + // Generate the video content + $content = '<div><a href="' . $videoUrl . '">Watch on YouTube</a></div>'; + foreach ($descriptions as $description) { + $content .= '<div>' . $description . '</div>'; + } + + $feeds[] = array( + 'title' => $title, + 'date' => $date, + 'content' => $content + ); + } + + $feedTitle = 'YouTube Channel: ' . $channelTitle; + + $response = array( + 'success' => true, + 'feedTitle' => $feedTitle, + 'feeds' => $feeds + ); + } else { + $response = array( + 'success' => false, + 'message' => 'No videos found for the provided YouTube channel.' ); } - - $response = array( - 'success' => true, - 'feedTitle' => $feedTitle, - 'feeds' => $feeds - ); } else { - $response = array( - 'success' => false, - 'message' => 'Failed to parse RSS feed.' - ); + // Handle standard RSS and Atom feeds as before + $feed = simplexml_load_file($rssUrl); + + if ($feed) { + $feedTitle = (string) $feed->channel->title; + $feeds = array(); + + foreach ($feed->channel->item as $item) { + $title = (string) $item->title; + $date = (string) $item->pubDate; + $content = (string) $item->description; + + $feeds[] = array( + 'title' => $title, + 'date' => $date, + 'content' => $content + ); + } + + $response = array( + 'success' => true, + 'feedTitle' => $feedTitle, + 'feeds' => $feeds + ); + } else { + $response = array( + 'success' => false, + 'message' => 'Failed to parse RSS feed.' + ); + } } } else { $response = array(