June 30, 2008 12:20 PM
This is another example of how to use a custom data source. In this case, the graph information will be coming from my Folding@Home statistics. Before writing any code, I logged into Trendrr.com and went to Start Tracking»Add Custom Data Set. My data source input key is given once the form is filled out appropriately. Now for the custom development. First, I will show the entire PHP file I use in order to retrieve Folding@Home data and then explain each piece.
<?php
include('foldingStats.php');
// Only submit data if accessed by cron
if ($_SERVER['argv'][1] == 'cron') {
// Get score, rank, and WUs for a specific user
if ($folding_stats = getFoldingStats('jbulava')) {
$api_url = 'http://www.trendrr.com/api/import/timeseries?';
$dataset_key = '<API Key>';
$score = $folding_stats['score'];
// Send value to your custom data source
$ch = curl_init($api_url.'dataset_key='.$dataset_key.'&value='.$score);
curl_exec($ch);
curl_close($ch);
}
}
else {
echo 'Folding@Home data sent only when accessed by cron.';
}
?>
The included foldingStats.php file does the data-mining magic; we'll get back to that. As in my Facebook API example, a cron job has been set up on my server to call this file on a periodical basis. Here's the definition I used.
0 * * * * php -f /<some directory>/datasources/folding/index.php cron
If the command line argument 'cron' is present, which is used to avoid non-uniform data points via accessing the file through a browser, the 'getFoldingStats' method is called from the included file. All we need to know right now is that it will return the 'score,' 'rank,' and 'work_units' of the given Folding@Home user as an associative array with these words as the key values. If the results are returned successfully, three important variables are set; the API URL, the data set key that was obtained when adding a new data source, and my Folding@Home score using the returned associative array. Now cURL can be used to piece together the full RESTful URL and submit a data point to Trendrr.
The actual process to obtain the statistics is less about using our API, but can be a valuable example as to how you may obtain data. cURL is used to make an HTTP request and the source code is stored as a string. As long as there is no statistic update in progress, this string is then parsed to obtain the numbers for rank, score, and number of work units.
<?php
/**
* Filename: foldingStats.php
* Created: Jun 24, 2008
* @author: jbulava
* @version: 1.0
*
* Library to access and retrieve Folding@Home stats.
*/
/**
* Retrieves score, rank, and work units for the given username.
*/
function getFoldingStats($username) {
if ($username == null)
return false;
// Grab page source with stats
$statsPage = curl_init('http://fah-web.stanford.edu/cgi-bin/main.py?qtype=userpage&username='.$username);
curl_setopt($statsPage, CURLOPT_RETURNTRANSFER, 1);
$pageSource = curl_exec($statsPage);
curl_close($statsPage);
$pageSource = strip_tags($pageSource);
// Check for unavailable server due to updates since this happens a lot
if (stristr($pageSource, 'Stats update in progress'))
return false;
// Isolate stats
$username = strtolower($username);
$statsStartPos = strpos($pageSource, 'Donator') + 8;
$statsEndPos = strpos($pageSource, 'Detailed listing', $statsStartPos);
$stats = substr($pageSource, $statsStartPos, ($statsEndPos - $statsStartPos) );
$stats = str_replace("\n", "", $stats);
$stats = str_replace("\t", "", $stats);
// Get total score
$strStart = strpos($stats, 'Score') + 6;
$strEnd = strpos($stats, '(certificate)', $strStart);
$score = trim(substr($stats, $strStart, ($strEnd - $strStart) ));
// Get overall rank
$strStart = strpos($stats, 'Donator Rank') + 13;
$strEnd = strpos($stats, 'of', $strStart);
$rank = trim(substr($stats, $strStart, ($strEnd - $strStart) ));
// Get total work units
$strStart = strpos($stats, 'WU') + 3;
$strEnd = strpos($stats, '(certificate)', $strStart);
$units = trim(substr($stats, $strStart, ($strEnd - $strStart) ));
return array(
'score' => $score,
'rank' => $rank,
'work_units' => $units
);
}
?>