Created
January 23, 2018 12:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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