Arquivo Eletrônico


2015, Apr-14

Importing Activity Data from Fitbit to Runkeeper

This is a PHP/shell scritp to Import activities from FitBit to RunKeeper. It includes the map, activity type, calories, time and distance.

Here is what it does:

  • Connect to FitBit via developer API and obtain a list of activities for a particular day. The API does not allow for a range of dates.
  • Use curl to download the activies. The download is formatted as Garmin's TCX format.
  • Translate the *.tcx file to a JSON stream in RunKeepers format.
  • Connect to RunKeeper via API and upload the JSON stream as a completed activity.

The scrip uses some libraries to interface with the APIs (why re-invent the wheel?). For FitBit I use fitbitphp and runkeeperAPI for RunKeeper. I also wrote a custom script for tranlating from TCX to JSON.

Developer accounts are needed for FitBit and RunKeeper. Sign-up pages can be found here and here. After that, a new Application needs to be registered on each service. This will create a Client Key and Secret for FitBit and a Client ID and Secret for RunKeeper.

FitBit API

Here is the basic usage for the FitBit library:

<?php
  $getdate = '2015-01-31';
  require 'fitbitphp.php';

  $fitbit = new FitBitPHP(FITBIT_KEY, FITBIT_SECRET);
  $fitbit->initSession('http://example.com/callback.php');

  $xml =$fitbit->getActivities('',$getdate);
  print_r($xml);
?>

These are the portions that are interesting to us:

$xml->activities->{for_each_activity}->activityParentName
$xml->activities->{for_each_activity}->logId

The activityParentName contains the type of activity performed: Walk, Run, Hike, etc. and the logId is used to build the http request to actually download the log file.

Downloading the Log Files from FitBit

The FitBit API does not allow for downloading the TCX file. For that, we will need to do an HTTP GET request with curl. But the problem is that we need to be logged in to download the data. I'm using Lynx to obtain the authentication cookie from FitBit (I tried curl and it worked until they made changes to the login form). The actual HTTP address for the Log File is:

https://www.fitbit.com/activities/exercise/{logId}?export=tcx

Runkeeper API

This library requires that [YAML][] is present. Here is the basic code used to upload the log to RunKeeper:

<?php
  define('YAMLPATH', '/path/to/yaml/install/directory/');
  define('RUNKEEPERAPIPATH', '/path/to/runkeeperapi/install/directory/');
  define('CONFIGPATH', '/path/to/config/file/directory/');
  require(YAMLPATH.'lib/sfYamlParser.php');
  require(RUNKEEPERAPIPATH.'lib/runkeeperAPI.class.php');

  /* API initialization */
  $rkAPI = new runkeeperAPI(CONFIGPATH.'rk-api.sample.yml');

  /* Do a "Create" request on "FitnessActivity" interface with fields => return created FitnessActivity content if request success, false if not */
  $fields = json_decode(LOG_JSON_DATA);
  $rkCreateActivity = $rkAPI->doRunkeeperRequest('NewFitnessActivity','Create',$fields);
  if ($rkCreateActivity) {
    print_r($rkCreateActivity);
  }
?>

All together

My script takes all of the above, adds some code to save and retrieve the OAuth tokens and loops throught the list of available activities. Nothing fancy, but it works.

The only configuration needed is at the beginning of the script and the parameters for executing Lynx.




Attachments:

Fitbit to runkeeper


1/14