Skip to content

Instantly share code, notes, and snippets.

@VDK
Created January 23, 2018 12:15
Show Gist options
  • Save VDK/ef49b4c7bd65ff3e653ffa55dec32839 to your computer and use it in GitHub Desktop.
Save VDK/ef49b4c7bd65ff3e653ffa55dec32839 to your computer and use it in GitHub Desktop.
Filter YouTube rss feed to only let videos through that have a CreativeCommons license.
<?php
$key = 'googleAPIkey';
if (isset($_GET['user'])){
$user = strip_tags($_GET['user']);
$var = 'user';
}
elseif(isset($_GET['channel_id'])){
$user = strip_tags($_GET['channel_id']);
$var = 'channel_id';
}
else{
echo "variable user or channel_id not set"; die;
}
header('Content-type: application/xml');
$dom=new DOMDocument();
$dom->load('https://www.youtube.com/feeds/videos.xml?'.$var.'='.$user);
$nodesToDelete = array();
$root=$dom->documentElement;
$markers=$root->getElementsByTagName('entry');
// Loop trough childNodes
foreach ($markers as $marker) {
$ytid=$marker->getElementsByTagNameNS('*','videoId')->item(0)->textContent;
$json =json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/videos?key='.$key.'&part=status&id='.$ytid), true);
if ($json['items'][0]['status']['license'] != "creativeCommon"){
$nodesToDelete[]=$marker;
}
}
// You delete the nodes
foreach ($nodesToDelete as $node) $node->parentNode->removeChild($node);
echo $dom->saveXML();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment