Files
fullrss/fullrss.php
2019-01-08 22:56:16 -08:00

80 lines
3.5 KiB
PHP

<?php
// Full RSS Feed
// replaces article description with article contents
// Copyright 2019 Scott Alfter
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Usage: drop this file onto a PHP-enabled webserver and set the
// API key (see below). In your feed reader, prepend feed URLs with
// something like this:
// https://invalid.example.tld/fullrss.php?url=
// get an API key from https://mercury.postlight.com/
$mercury_api_key="*** YOUR MERCURY API KEY GOES HERE ***";
// maximum age to fetch text (seconds; set to a low multiple of your
// reader's refresh interval; set to 0 to fetch text regardless of age)
$max_age=120*60; // default: 2 hours
// configuration ends here
$url=$_GET["url"];
$feed=new SimpleXmlElement(file_get_contents($url)); // read feed into object
// type 1
foreach ($feed->item as $entry) // fix up each entry
{
if (substr($entry->link, 0, 21)=="http://minx.cc/?post=") // fix AoSHQ links
$entry->link="http://minx.cc:1080/?post=".substr($entry->link, 21);
if ($max_age==0 || time()-strtotime($entry->children("dc", TRUE)->date)<$max_age || time()-strtotime($entry->pubDate)<$max_age)
{
$ch=curl_init(); // use Mercury to get article text
curl_setopt($ch, CURLOPT_HTTPHEADER, array("x-api-key: ".$mercury_api_key));
curl_setopt($ch, CURLOPT_URL, "https://mercury.postlight.com/parser?url=".$entry->link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$article=json_decode(curl_exec($ch));
curl_close($ch);
$entry->description=$article->content;
}
}
// type 2
foreach ($feed->channel->item as $entry) // fix up each entry
{
if ($max_age==0 || time()-strtotime($entry->pubDate)<$max_age || time()-strtotime($entry->pubDate)<$max_age)
{
$ch=curl_init(); // use Mercury to get article text
curl_setopt($ch, CURLOPT_HTTPHEADER, array("x-api-key: ".$mercury_api_key));
curl_setopt($ch, CURLOPT_URL, "https://mercury.postlight.com/parser?url=".$entry->link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$article=json_decode(curl_exec($ch));
curl_close($ch);
$entry->description=$article->content;
}
}
header("Content-Type: application/rss+xml");
echo $feed->asXML(); // write the modified feed back out
?>