Blog Programistyczny

Branżowy blog dla Webmastera.

PHP Webmastering

zamiana kanału YouTube na feed RSS

kiedyś dało się to zrobić przez link typu:


http://gdata.youtube.com/feeds/api/users/xxxx/uploads

Niestety od kwietnia 2015 jest to przez YT systematycznie wycofywane i kanały przestają działać w ten sposób.

Pomocnym może okazać się ten skrypt PHP. Znaleźliśmy to na tej stronie i poprawiliśmy w nim błędy. Został także dostosowany do polskich realiów (m.in. pod kątem kodowania polskich znaków).

Enjoy.


<?php

libxml_use_internal_errors(true);

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;
}

$url = $_GET['url'];

$host = preg_replace('#^(https?://[^/]+).*#', '$1', $url);
$author = preg_replace('#^(https?://[^/])/user/([^/]+).*#', '$1', $url);

$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);

$mainTitle = $xpath->query('//title')->item(0)->nodeValue;
$links = $xpath->query('//a[starts-with(@href, "/watch")]');

$entries = array();

if (!is_null($links)) {
foreach ($links as $link) {
$href = $link->getAttribute('href');
$title = $link->getAttribute('title');

$title= iconv("UTF-8","ISO-8859-1",$title);

if ($title === '')
$title = trim($link->nodeValue);

if (!isset($entries[$href]) || strlen($entries[$href]) < strlen($title))
$entries[$href] = $title;
}
}

header('Content-Type: application/atom+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;

echo '<?xml version="1.0" encoding="utf-8"?>';

?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text"><?= $mainTitle ?></title>
<link rel="self" href="<?= $self ?>" type="application/atom+xml" />
<link rel="alternate" href="<?= $url ?>" type="text/html" />
<updated><?= date('c', $updated) ?></updated>
<id><?= $url ?></id>
<?php

$skip = array('Alle wiedergeben','Wiedergeben');

foreach ($entries as $entryUrl => $title) {
$entryUrl = htmlspecialchars("$host$entryUrl");
$title = htmlspecialchars($title);
$updated -= 60;

if (!in_array(trim($title),$skip)) {

?>
<entry>
<id><?= $entryUrl ?></id>
<title type="text"><?= $title ?></title>
<link rel="alternate" href="<?= $entryUrl ?>" />
<author>
<name><?= $author ?></name>
</author>
<updated><?= date('c', $updated) ?></updated>
</entry>
<?php

}

}
?>
</feed>

 

 

Recommended
Uruchamiasz stronę na WordPress? Prześledź listę polecanych przez nasz dodatków.
Cresta Posts Box by CP