PHP Interface for RunKeeper and Sportstracklive | Flavio's blog
 

PHP Interface for RunKeeper and Sportstracklive

A few days ago I finally dusted off the bicycle and, taking advantage of good weather, I wanted to make a long trip along the sea coastline.

Given that the odometer was out of service, I tried some app for Android that exploited the GPS to record the route, the speed, the altitude, and so on. Runkeeper immediately jumped to the eye: attractive graphics, opportunity of social sharing, Free App (even if improved).
Similar to Runkeeper, also Sports Track Live keeps track of your performance. Compared to the first, however, has a site less attractive, and a way better Android App (even if it's payware).

I installed the app, made my 35km, and at home I loaded my route on the website of RunKeeper.
However, nor RunKeeper Sportstracklive nor offer any chance to integrate the statistics of its run on your web site (while you can share it via Facebook or Twitter). Also, they don't offer an API to interact with their data.

In a few minutes to spare, I wrote a small application in PHP, which allows to show into my homepage the statistics related to’LAST activity, running or cycling, whatever it is.

It's enough to enter your username, set the validity time of the cache (to avoid overloading the site), and you can immediately see your statistics!

An example, taken from my homepage:

Interfaccia PHP per Runkeeper in funzione

RunKeeper PHP interface for running

Let's take a quick look at the code:

<?
/* Sportstracklive.com data parser. It automatically shows data from the LAST activity for a given username. Set your username, cache file name, and expiration time of the cachefile.
(C) flapane.com - Feb-2013*/

define('SPORTSTRACK_USER','type_your_username');        //Runkeeper Username
$expiretime=6;          // cache expire time in hours
$cachename="sportstracklive.cache"; //name of the cachefile

//create the cachefile if it doesn't exist
if (!file_exists($cacheName)) {
     $create = fopen($cacheName, 'W');
      chmod ("$cacheName", 0644); //set chmod 644
     fclose($create);
}

// Is the file older than $expiretime, or the file is new/empty?
$FileAge = time() - filectime($cacheName);    // Calculate file age in seconds
if ($FileAge > ($expiretime * 3600) || 0 == filesize($cacheName))
{
    include_once('simple_html_dom.php');
    $opts = array(
  'http'=>array(
    'user_agent'=>'Mozilla/5.0 (Windows; You; Windows NT 5.1; in; rv:1.9.2.3) Gecko/20100401 MRA 5.6 (build 03278) Firefox/3.6.3 (.NET CLR 3.5.30729)',
    'Accept: text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8'
  )
);
    $context = stream_context_create($opts);    //create an useragent for simple_html_dom
    $html = file_get_html('http://www.sportstracklive.com/search?what=user:'.SPORTSTRACK_USER.'&search=Search', 0, $context);
    $link = $html->find('.likeh4 lightGrey.', 0)->find('a', 0)->href;    // Get the last activity link
    $link2 = file_get_html('http://www.sportstracklive.com'.$link, 0, $context);
    $activity_table = $html->find('td', 1);    // Get the activity table stats
    $activity_table2 = $link2->find('table', 7);    //get other stats
    $activity_table3 = $link2->find('table', 8);    //get other stats (calories)

    $activity_date = strip_tags($activity_table->find('.lightGrey', 1)); //last activity data
    $category= strip_tags($link2->find(".likeh2 ', 0)); //activty type
    $distance= strip_tags($activity_table->find(".likeh2 ', 6)); //total distance
    $moving_time= strip_tags($activity_table2->find(".likeh4 ', 2)); //total moving time
    $avg_speed= strip_tags($activity_table2->find(".likeh4 ', 3)); //average speed
    $avg_time_km= strip_tags($activity_table2->find(".likeh4 ', 4)); //average moving pace
    $calories= strip_tags($activity_table3->find(".likeh4 ', 0)); //calories

    // Now refresh the cachefile with newer content

    $stats = '<a href="http://www.sportstracklive.com'.$link.'" target="_blank">Last Activity&agrave;:<b> '.$activity_date.'</b></A><br />Type of activity&agrave;:<b> '.$category.'</b><br />Total distance:<b> '.$distance.'</b><br />Duration:<b> '.$moving_time.'</b><br />Speed&agrave; average:<b> '.$avg_speed.'</b><br />Average time on Km:<b> '.$avg_time_km.'</b><br />Calories Burned:<b> '.$calories.'</b><br />';

    $handle = fopen($cacheName, 'wb');
    fwrite($handle, $stats);            
}

//cachefile is fresh enough... outputting data
$data=file_get_contents($cacheName);
echo $data;
?>

As mentioned above, Just set your own username. For convenience, I decided to update the cache every 12 hours (720 minutes), more than enough time, given that hardly you will work out twice a day!

You can download the zip file containing everything you need to show your stats, at these locations:

Runkeeper PHP parser

Sportstracklive PHP parser

Feedback, Comments and ratings, are welcomed!

 

1 Comment »

Please accept third-party cookies to be able to comment on the post! The CHANGE COOKIE CHOICES button is located in the footer of the site. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.