Skip to content

Instantly share code, notes, and snippets.

@bdelespierre
Created January 15, 2014 18:11
Show Gist options
  • Select an option

  • Save bdelespierre/8441281 to your computer and use it in GitHub Desktop.

Select an option

Save bdelespierre/8441281 to your computer and use it in GitHub Desktop.
Surpress StumbleUpon url and replace them with actual ones from your StumbleUpon likes feed
<?php
function stumble2url ($url) {
$doc = new DOMDocument;
if (!@$doc->loadHTMLFile($url))
return null;
return ($iframe = $doc->getElementById('tb-stumble-frame'))
? $iframe->getAttribute('src')
: null;
}
libxml_use_internal_errors(true);
set_time_limit(300); // 5 min
$rss_feed_address = "http://www.stumbleupon.com/rss/stumbler/bdelespierre/likes";
$rss_feed_dom = new DOMDocument;
$rss_feed_dom->load($rss_feed_address);
$rss_xpath = new DOMXPath($rss_feed_dom);
foreach ($rss_xpath->query('//channel/item') as $item) {
$link = $rss_xpath->query('./link', $item)->item(0);
$guid = $rss_xpath->query('./guid', $item)->item(0);
$desc = $rss_xpath->query('./description', $item)->item(0);
if (!$link || !$stumble_url = $link->nodeValue)
continue;
if (!$real_url = stumble2url($stumble_url))
continue;
$link->nodeValue = $real_url;
$guid->nodeValue = $real_url;
$real_desc_data = str_replace("href=\"$stumble_url\"", "href=\"$real_url\"", $desc->nodeValue);
$real_desc_cdata = $rss_feed_dom->createCDATASection($real_desc_data);
$real_desc = $rss_feed_dom->createElement('description');
$desc_parent = $desc->parentNode;
$desc_parent->removeChild($desc);
$desc_parent->appendChild($real_desc);
$real_desc->appendChild($real_desc_cdata);
}
PHP_SAPI != 'cli' && header("Content-Type: application/rss+xml");
echo $rss_feed_dom->saveXML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment