Skip to content

Instantly share code, notes, and snippets.

@hermanschutte
Created October 18, 2011 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermanschutte/1295579 to your computer and use it in GitHub Desktop.
Save hermanschutte/1295579 to your computer and use it in GitHub Desktop.
Get news articles from RSS feeds and send them to Instapaper

How to use this script

Just execute the script by specifying your Instapaper username, password and any number of rss feeds

php getnews.php [username] [password] [rss feed]...

<?php
function http_response($url, $status = null, $wait = 1)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
if($status == $httpCode)
{
return TRUE;
}
return FALSE;
sleep($wait);
}
if (isset($argv[1]) && isset($argv[2]) && isset($argv[3])) {
$username = $argv[1];
$password = $argv[2];
$doc = new DOMDocument();
for ($i = 3; $i < count($argv); $i++) {
// Read RSS Feed
$doc->load($argv[$i]);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
// Loop through all articles and add each to Instapaper
foreach ($arrFeeds as $article) {
$url = 'https://instapaper.com/api/add?username='.$username.'&password='.$password.'&url='.urlencode($article['link']).'&title='.urlencode($article['title']).'&selection='.urlencode($article['desc']);
echo "Adding: ".$article['link']."...";
if (http_response($url, '201')) {
echo "added!\n";
} else {
echo "error! [code: ".$httpCode."]\n";
}
}
}
echo "All done!\n";
} else {
echo "Please specify an Instapaper username, password and at least 1 RSS feed\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment