Skip to content

Instantly share code, notes, and snippets.

@azharc
Created April 2, 2014 16:37
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 azharc/9937808 to your computer and use it in GitHub Desktop.
Save azharc/9937808 to your computer and use it in GitHub Desktop.
Instapaper Feed for KirbyText
<?php
/*
AUTHOR:
Azhar Chougle (azmataz.com)
DESCRIPTION:
This KirbyText extension will fetch your Instapaper Likes, choose a random one, and output a link.
It won't output if the feed is invalid, and will try to read it from cache if it is unavailable.
The cURL request is also set to timeout after 1 second so that your page load doesn't stall because of a fetch gone awry.
USAGE:
(instapaper: http://www.instapaper.com/starred/rss/********)
OUTPUT:
Recently he enjoyed reading (link)
*/
class kirbytextExtended extends kirbytext {
function __construct($text, $markdown=true) {
parent::__construct($text, $markdown);
/*
// define custom tags
$this->addTags('mynewtag', 'anothertag');
// define custom attributes
$this->addAttributes('mynewattribute', 'anotherattribute');
*/
$this->addTags('instapaper');
}
/*
// define a function for each new tag you specify
function mynewtag($params) {
// do something with the passed params here.
}
*/
function instapaper($params) {
$url = $params["instapaper"];
// If the cache has expired, get the new feed.
if (!$feed = cache::get('instapaper', false, 43200)) {
// Set up fetch
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_NOBODY => false,
CURLOPT_TIMEOUT => 1,
CURLOPT_RETURNTRANSFER => true,
);
// Set options
curl_setopt_array($ch, $options);
// Do it
$xml = curl_exec($ch);
curl_close($ch);
// Parse response
$feed = x::parse($xml);
// Check the parse.
if (!$feed) {
// Parse failed, attempt to load from cache.
if (!$feed = cache::get('instapaper')) {
return false;
}
} else {
// Cache the feed
cache::set('instapaper', $feed);
}
}
$article = $feed["channel"]["item"][array_rand($feed["channel"]["item"])];
// Final safety check and output.
if (url::valid($article["link"])) {
return "Recently he enjoyed reading " . str::link($article["link"], str::short($article["title"], 45));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment