Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created November 1, 2021 00:22
Show Gist options
  • Select an option

  • Save hlorand/ee6461df5a3cc245abbd56ac0c6ff45d to your computer and use it in GitHub Desktop.

Select an option

Save hlorand/ee6461df5a3cc245abbd56ac0c6ff45d to your computer and use it in GitHub Desktop.
Merge YouTube channel RSSs into single RSS channel
<?php
header('Content-type: application/xml');
echo "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
echo "<channel>\n";
echo "<title>YouTube Channels RSS Feed</title>\n";
echo "<description>YouTube Channels RSS Feed</description>\n";
echo "<link>http://{$_SERVER[HTTP_HOST]}</link>\n";
echo "<atom:link href='http://{$_SERVER[HTTP_HOST]}' rel='self' type='application/rss+xml' />\n";
// Feed URLs - https://commentpicker.com/youtube-channel-id.php
$feeds = array(
"UC7ivXUYaRoZb5WDLKksETUA", // Puzser Robert
"UC4xKdmAXFh4ACyhpiQ_3qBw", // Tech Lead
"UCKf0UqBiCQI4Ol0To9V0pKQ", // Buff Dudes
"UCJ24N4O0bP7LGLBDvye7oCA", // Matt D Avella
"UC6nSFpj9HTCZ5t-N3Rm3-HA", // Vsauce
"UCRJovKcgUL7QumDE1YsLqzg", // Magyarosi Csaba
"UCittVh8imKanO_5KohzDbpg", // Paul Joseph Watson
"UC98tcedR6gULv8_b70WJKyw" // Leo Moracchioli
);
// compare two video dates to order them
function date_compare($a, $b) {
return strcmp($b->published, $a->published);
}
// Load past videos list
$videoids = explode(PHP_EOL, @file_get_contents("youtube.log"));
unlink("youtube.log");
// Collect each feed's items in an array and sort it by date
$entries = array();
foreach($feeds as $feed) {
$xml = simplexml_load_file("https://www.youtube.com/feeds/videos.xml?channel_id=".$feed);
foreach ($xml->entry as $entry) {
$entry->title = $xml->title . " - " . $entry->title;
array_push($entries,$entry);
}
}
usort($entries, "date_compare");
// Generate RSS
foreach($entries as $entry){
$entry->title = addslashes($entry->title);
$videoid = @array_pop(explode(":",$entry->id));
$link = "https://youtube.com/watch?v=" . $videoid;
$thumbnail = "https://img.youtube.com/vi/$videoid/hqdefault.jpg";
// Do not show past videos in the feed
if( !in_array($videoid, $videoids)){
echo "<item>\n";
echo "<title><![CDATA[{$entry->title}]]></title>\n";
echo "<description><![CDATA[<img src='$thumbnail' width='100%'>]]></description>\n";
echo "<pubDate>".date('D, d M Y H:i:s',strtotime($entry->published))." GMT</pubDate>\n";
echo "<link>$link</link>\n";
echo "<guid>$link</guid>\n";
echo "</item>\n";
}
// Collect past videos
file_put_contents("youtube.log", $videoid."\n", FILE_APPEND);
}
echo "</channel>\n";
echo "</rss>\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment