Skip to content

Instantly share code, notes, and snippets.

@melice
Last active August 17, 2016 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melice/25216bb49afaf6c2f35decfb79190510 to your computer and use it in GitHub Desktop.
Save melice/25216bb49afaf6c2f35decfb79190510 to your computer and use it in GitHub Desktop.
fetch youtube channel as RSS
<?php
// origin : https://gist.github.com/Skalman/801436d9693ff03bc4ce
if (!isset($_GET['url'])) {
?>
<!DOCTYPE html>
<meta charset="utf-8">
<title>Youtube RSS creator</title>
<form>
<p>Create an RSS feed for the videos on the following page:
<p><input name="url" placeholder="E.g. https://www.youtube.com/user/scishow/videos" style="width: 30em">
<p><input type="submit" value="Create">
</form>
<?php
exit;
}
function loadNprepare($url,$encod='') {
$content = file_get_contents($url);
if (!empty($content)) {
if (empty($encod))
$encod = mb_detect_encoding($content);
$headpos = mb_strpos($content,'<head>');
if (FALSE=== $headpos)
$headpos= mb_strpos($content,'<HEAD>');
if (FALSE!== $headpos) {
$headpos+=6;
$content = mb_substr($content,0,$headpos) . '<meta http-equiv="Content-Type" content="text/html; charset='.$encod.'">' .mb_substr($content,$headpos);
}
$content=mb_convert_encoding($content, 'HTML-ENTITIES', $encod);
}
$dom = new DomDocument;
$res = $dom->loadHTML($content);
if (!$res) return FALSE;
return $dom;
}
$url = $_GET['url'];
$host = preg_replace('#^(https?://[^/]+).*#', '$1', $url);
$author = preg_replace('#^(https?://[^/])/user/([^/]+).*#', '$1', $url);
//$html = file_get_contents("compress.zlib://".$url);
//$doc = new DOMDocument();
//$doc->loadHTML($html);
$doc = loadNprepare($url,'utf-8');
$xpath = new DOMXpath($doc);
$mainTitle = $xpath->query('//title')->item(0)->nodeValue;
$trlist = $xpath->query('//tr');
$entries = array();
$length = $trlist->length;
for ($i = 1;$i<$length; $i++)
{
$entries[$i] = array(
// 'href' => $xpath->query("//tr[$i]/td/a")->item(0)->getAttribute("href"),
'title' => $xpath->query("//tr[$i]/td/a")->item(0)->nodeValue,
'author' => htmlspecialchars($xpath->query("//tr[$i]/td/div/a")->item(0)->nodeValue),
//$image => $xpath->query("//tr[$i]/td/span/a/span/span/span/img")->item(0)->getAttribute('data-thumb'),
'vid' => htmlspecialchars($xpath->query("//tr[$i]")->item(0)->getAttribute('data-video-id')),
);
$entries[$i]['image'] = 'http://img.youtube.com/vi/'.$entries[$i]['vid'].'/0.jpg';
$entries[$i]['guid'] = $entries[$i]['vid'];
$entries[$i]['link'] = 'https://www.youtube.com/watch?v='.$entries[$i]['vid'];
if ($i>10) break;
}
//var_dump($entries);die;
header('Content-Type: application/xml;charset=utf-8');
$mainTitle = htmlspecialchars($mainTitle);
$protocol = $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$port = ":$_SERVER[SERVER_PORT]";
if (($protocol === 'http' && $port === ":80") || ($protocol === 'https' && $port === ":443"))
$port = '';
$self = htmlspecialchars("$protocol://$_SERVER[SERVER_NAME]$port$_SERVER[REQUEST_URI]");
$url = htmlspecialchars($url);
$author = htmlspecialchars($author);
$updated = time();
$updated -= $updated % 60;
?>
<?xml version="1.0" encoding="utf-8" ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title><?= $mainTitle ?></title>
<link><?= $url ?></link>
<pubDate><?= date('r', $updated) ?></pubDate>
<description> youtube </description>
<?php
foreach ($entries as $item) {
// $ = htmlspecialchars("$host$entryUrl");
// $title = htmlspecialchars($title);
$updated -= 60;
?>
<item>
<title><![CDATA[<?= $item['title'] ?>]]></title>
<link><?= $item['link'] ?></link>
<guid><?= $item['link'] ?></guid>
<!-- <author><?= $item['author'] ?></author> -->
<image><?= $item['image'] ?></image>
<pubDate><?= date('r', $updated) ?></pubDate>
<description><![CDATA[<img src="<?= $item['image'] ?>">]]></description>
<enclosure url="<?= $item['image'] ?>" length="10240" type="image/jpg"/>
</item>
<?php
}
?>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment