Polar HRM + GPX to TCX and Strava upload

From tTiMe
(Redirected from Gpx2tcx)
Jump to: navigation, search

perl/Tkx script combining GPS track and HRM Polar files into TCX format based on Paul Colby's awk script.

  • Reads Polar HRM file (heart rate, altitude, speed, cadence)
  • Reads GPX or TCX as GPS input
  • GPS file can come from a different source than a Polar GPS and does not need to be (time) synchronized with HRM data
  • Picks 1st data / values from HRM input, if not valid or missing from GPS input
  • 1-click upload to Strava


Hrmgpx2tcx.png


Download

hrmgpx2tcx.pl

Installing Perl on Windows

Upload to Strava

sub strava_upload
{
   use LWP;
   use WWW::Mechanize;
   use LWP::UserAgent;
  
   my ( $tcx, $user, $passwd ) = @_;
  
   if (!-e $tcx || !-f $tcx){
       return "Cannot find GPX/TCX file.";
   }
   if ( !defined($user) || !length($user)){
       return "Username missing.";
   }
   if ( !defined($passwd) || !length($passwd)){
       return "Password missing.";
   }
   
   # Login
   my $upload_url = "http://app.strava.com/upload/select";
   my $mech = WWW::Mechanize->new(agent => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
   $mech->get($upload_url);
   my $content = $mech->content();
   $mech->form_id('login_form');
   $mech->field('email' => $user);
   $mech->field('password' => $passwd);
   $mech->click_button(number => 1);
   $content = $mech->content();
 
   if ($content =~ /The username or password did not match. Please try again/){
       return "The username or password did not match. Please try again.";
   }

   $content =~ /input name=[\"\']authenticity_token[\"\'] type=[\"\']hidden[\"\'] value\=[\"\'](.*)[\"\']/g;
   my $token = $1;
   if (!defined($token) || length($token) < 10){
       return "Could login, but could not find token.";
   }
 
   # Upload
   my $form = $mech->form_number(1);
   $mech->field('_method' => 'post');
   $mech->field('authenticity_token' => $token);
   $mech->field('files[]' => $tcx);
   my $input = $form->find_input('files[]');
   $input->filename('myFilename');
   $input->headers('Content-Type', 'application/octet-stream');
   my $res = $mech->submit();
 
   if (!$res->is_success()){
       return "Upload went wrong:".$res->status_line().".";
   }
 
   return "Successful uploading.";
}
Personal tools