Skip to content

Instantly share code, notes, and snippets.

@goodevilgenius
Last active October 26, 2017 14:16
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 goodevilgenius/6426099 to your computer and use it in GitHub Desktop.
Save goodevilgenius/6426099 to your computer and use it in GitHub Desktop.
[Reddit RSS Relinker] Modifies Reddit RSS feeds so that the links go to the original URL, rather than the comments page. Also adds author. Can optionally filter based on regular expressions. #RSS #Reddit
<?php
$url = $_GET['url'];
if (!preg_match('@http://(www\.)?reddit\.com/r/([A-Za-z0-9]+)\.rss@', $url)) {
header("HTTP/1.0 400 Invalid Reddit RSS URL");
exit;
}
if (!empty($_GET['filter']) && !empty($_GET['filter_item']) && (preg_match($_GET['filter'],'') !== false)) {
$filter = $_GET['filter'];
$f_item = $_GET['filter_item'];
} else $filter = false;
header("Content-type: application/rss+xml");
$rss = new SimpleXMLElement($url, 0, true);
$toremove = array();
foreach ($rss->channel->item as $item) {
$d = (string)($item->description);
$m = '';
if (preg_match('@submitted by <a href="http://www.reddit.com/user/([A-Za-z0-9_]+)"> ?\1 ?</a>( <br/> )?<a href="([^"]+)">[^<]+</a>@',$d, $m)) {
$item->link = $m[3];
$item->author = $m[1];
}
if ($filter) {
if (!preg_match($filter, $item->{$f_item})) $toremove[] = dom_import_simplexml($item);
}
}
for ($i = count($toremove) - 1; $i >= 0; $i--) {
$n = $toremove[$i];
$n->parentNode->removeChild($n);
}
echo $rss->saveXML();
/**
Copyright (c) 2013, Daniel Ray Jones
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
Gist: https://gist.github.com/6426099
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment