Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Last active December 15, 2015 22:19
Show Gist options
  • Save swvitaliy/5331812 to your computer and use it in GitHub Desktop.
Save swvitaliy/5331812 to your computer and use it in GitHub Desktop.
Агрегатор rss'очек :-)
<?php
// Агрегатор rss'очек :-)
$script_url = '[ Вставьте сюда url к этому скрипту ]';
$result_title = '[ Вставьте сюда title выходной rss ]';
$links = array(
'http://downloads.bbc.co.uk/podcasts/worldservice/bureve/rss.xml',
'http://downloads.bbc.co.uk/podcasts/worldservice/5floor/rss.xml',
'http://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml',
'http://downloads.bbc.co.uk/podcasts/worldservice/newshour/rss.xml',
'http://downloads.bbc.co.uk/podcasts/radio4/fooc/rss.xml',
'http://downloads.bbc.co.uk/podcasts/worldservice/bizdaily/rss.xml'
);
$summary = '';
$items = array();
foreach($links as $link) {
$text = file_get_contents($link);
$xml = simplexml_load_string($text);
$description = $xml->children('channel')->xpath('//description');
$summary .= (string) $description[0] . "\n";
foreach($xml->children('channel')->xpath('//item') as $item) {
$obj = null;
normalize_simple_xml($item, $obj);
$items[strtotime($obj['pubDate'])] = $obj;
}
}
$summary = trim($summary);
krsort($items);
$str = implode('', array_map(function($v) {
$url = $v['enclosure']['url'];
$length = $v['enclosure']['length'];
$type = $v['enclosure']['type'];
return "<item>"
. "<title>{$v['title']}</title>"
. "<description>{$v['description']}</description>"
. "<pubDate>{$v['pubDate']}</pubDate>"
. "<enclosure url=\"{$url}\" length=\"{$length}\" type=\"{$type}\"/>"
. "<guid isPermaLink=\"false\">{$url}</guid>"
. "<link>{$url}</link>"
. "<media:content url=\"{$url}\" fileSize=\"{$length}\" type=\"{$type}\" medium=\"audio\" expression=\"full\" duration=\"\"/>"
. "</item>";
}, $items));
$xml = <<<XML
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:ppg="http://bbc.co.uk/2009/01/ppgRss" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>{$result_title}</title>
<link>{$script_url}</link>
<description>
{$summary}
</description>
<language>en-gb</language>
<atom:link href="{$script_url}" rel="self" type="application/rss+xml"/>
{$str}
</channel>
</rss>
XML;
print($xml);
function normalize_simple_xml($obj, &$result) {
$data = $obj;
if (is_object($data)) {
$data = get_object_vars($data);
}
if (is_array($data)) {
foreach ($data as $key => $value) {
$res = null;
normalize_simple_xml($value, $res);
if (($key == '@attributes') && ($key)) {
$result = $res;
} else {
$result[$key] = $res;
}
}
} else {
$result = $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment