quote.fm-RSS mit den empfehlungen aller gefolgten

felix schwenzel, , in wirres.net    

seitdem quote.fm RSS-feeds einzelner nutzer anbietet (hier die ode von martin weigert auf die quote.fm-RSS-feeds) habe ich ein paar quote.fm-nutzer in meinem RSS-dings (früher google reader, jetzt auf meiner eigenen fever installation) aboniert. und lieben gerlernt. das liegt zum einen natürlich an den jeweiligen nutzern. abonniert hatte ich bisher:

aber ich habe mich schon immer gefragt, warum bietet mir quote.fm nicht alle leute denen ich auf quote.fm folge als RSS-feed an? warum sollte ich alle 140 leute denen ich folge einzeln abonnieren? kürzlich fiel mir dann beim duschen ein, dass das ja nicht so schwer sein könnte sowas zu scripten.

die quote.fm-API abfragen nach denen denen ich folge (quote.fm/api/user/listFollowings?username=ix), aus dieser liste feed-URLs konstruieren und diese feed-liste SimplePie zum frass vorwerfen und neu rausschreiben. hier ist das ergebnis:

* * *

eben via martin weigert gelesen, dass quote.fm das geld ausgegangen ist und quote.fm an eine agentur vertickt haben:

That means: Philipp, Marcel, Flo and I are saying our good-byes and sometime in the (quite near) future elbdudler will take over and hopefully build this thing here into something that you guys will appreciate.

hoffentlich wird das nicht so schlimm wie es sich anhört, aber noch funktioniert dieses quote.fm-dings ja noch.

* * *

das script hat als abhängigkeit lediglich SimplePie (download) und einen cache-ordner auf der gleichen ebene in der das script liegt:

das script ist nicht elegant gescriptet, funktioniert aber. fehler schliesse ich wie immer ausdrücklich nicht aus.


<?php
$input = '
<form method="get" name="schick it ab" action="http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'">
<fieldset>
<label for="1">quote.fm benutzername</label>
<input id ="1" type="text" name="user" />
<input type="submit" name="senden" />
</fieldset>
</form>';
$user_id = $_GET["user"];
if ($user_id == "") {
$user_id = "ix";
echo $input;
exit;
}
$count = 200;
$apiurl= 'https://quote.fm/api/user/listFollowings?username='.$user_id;
$timeout = 15;

$feeds = array();
$i=0;
while ( $i <= 20 ) // mehr als 20 seiten? eher nicht.
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl.'&pageSize=100&page='.$i);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
$output = curl_exec($ch);
curl_close($ch);
$rawData = trim($output);

$response = json_decode($rawData, true);
//
// echo $response;
if ( ($response['code'] == "404") AND ($i == 0) ) {

echo "<p>fehler. <i>".$user_id."</i> kein gültiger quote.fm benutzername.</p>";
echo $input;
?>

<?php
exit; }

if ( ($response['totalCount'] - (($i+1) * 100)) <= 0 ) { $i=20; }
foreach($response['entities'] as $entry){
$feeds[] = 'http://quote.fm/'.$entry['username'].'/feed';
//echo $entry['username'];
}
$i++;
}

//print_r($feeds);

// Include the SimplePie library
// For 1.3+:
require_once('SimplePie/autoloader.php');

// Create a new SimplePie object
$feed = new SimplePie();

// Instead of only passing in one feed url, we'll pass in an array of three
$feed->set_feed_url($feeds);

// We'll use favicon caching here (Optional)
//$feed->set_favicon_handler('handler_image.php');

$feed->set_cache_duration ( 3600 );
$feed->set_timeout ( 10 );
$feed->set_stupidly_fast( true );
//$feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
$feed->force_feed(true);

// Initialize the feed object
$feed->init();

// This will work if all of the feeds accept the same settings.
$feed->handle_content_type();

// Begin our XHTML markup
header("Content-Type: application/rss+xml");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">

<channel>
<title>Recent recommendations of all people <?php echo $user_id; ?> is following</title>
<link>https://quote.fm/<?php echo $user_id; ?></link>
<description>Recent recommendations of all people <?php echo $user_id; ?> is following</description>
<lastBuildDate><?php echo date(DATE_ATOM , time()); ?></lastBuildDate>
<language>en</language>
<atom:link href="<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?user='.$user_id; ?>" rel="self" type="application/rss+xml" />
<generator>http://quote.fm</generator>

<?php if ($feed->error): ?>
<?php //echo $feed->error; ?>
<?php print_r( $feed->error); ?>
<?php endif; ?>

<?php $j = 0; ?>
<?php foreach ($feed->get_items() as $item): ?>

<item>
<title><![CDATA[<?php echo $item->get_title(); ?>]]></title>
<link><?php echo $item->get_permalink(); ?></link>
<pubDate><?php echo $item->get_date('D, j M Y H:i:s O'); ?></pubDate>
<dc:creator><?php $author = $item->get_author(0); if ($author != "") {echo $author->get_name(); } ?></dc:creator>
<category><![CDATA[<?php $category = $item->get_category(0); if ($category != "") { echo $category->get_label();} ?>]]></category>
<guid isPermaLink="true"><?php echo $item->get_permalink(); ?></guid>
<description><![CDATA[<p><?php $feed = $item->get_feed(); echo $feed->get_title(); ?></p>
<?php echo $item->get_content(); ?>]]></description>

<content:encoded><![CDATA[<p><?php echo $feed->get_title(); ?></p>
<?php echo $item->get_content(); ?>]]></content:encoded>
</item>
<?php $j++; if ($j >= $count) { break; }?>
<?php endforeach; ?>
</channel>
</rss>