| 1 | package GPS::Garmin::Connect; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use HTML::Form; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | GPS::Garmin::Connect - Allows simple fetching of | ||||||||||
| 16 | activities from http://connect.garmin.com | ||||||||||
| 17 | |||||||||||
| 18 | =head1 VERSION | ||||||||||
| 19 | |||||||||||
| 20 | Version 0.01 | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 2 | our $VERSION = '0.01'; | |||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||
| 28 | |||||||||||
| 29 | This module is a simple helper to fetch and parse activities | ||||||||||
| 30 | from http://connect.garmin.com | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | use GPS::Garmin::Connect; | ||||||||||
| 35 | |||||||||||
| 36 | my $connect = GPS::Garmin::Connect->new(); | ||||||||||
| 37 | my $json = $connect->fetchdata( $username, $password ); | ||||||||||
| 38 | |||||||||||
| 39 | my $activities = $connect->parse( $json ); | ||||||||||
| 40 | |||||||||||
| 41 | foreach my $activity (@$activities) { | ||||||||||
| 42 | print "My activity $activity->{activity} - HR: $activity->{heartrate}\n"; | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | =head1 FUNCTIONS | ||||||||||
| 47 | |||||||||||
| 48 | =head2 new | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 0 | my $self = shift; | |||||||||
| 54 | 0 | return bless { | |||||||||
| 55 | _loginurl => 'http://connect.garmin.com/signin', | ||||||||||
| 56 | }, $self; | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | =head2 fetchdata | ||||||||||
| 60 | |||||||||||
| 61 | $connect->fetchdata( $username, $password ); | ||||||||||
| 62 | |||||||||||
| 63 | Logins into connect.garmin.com and fetches all activities and returns | ||||||||||
| 64 | a JSON string which can be parsed using L<parse>. | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub fetchdata { | ||||||||
|
Subroutine
fetchdata: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 69 | 0 | my ($pkg, $username, $password) = @_; | |||||||||
| 70 | 0 | my $loginurl = $pkg->{_loginurl}; | |||||||||
| 71 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||
| 72 | 0 | $ua->cookie_jar( { } ); | |||||||||
| 73 | 0 | push @{ $ua->requests_redirectable }, 'POST'; | |||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | # Fetch login form to get a session id .. | ||||||||||
| 78 | 0 | my $loginformreq = HTTP::Request->new(GET => $loginurl); | |||||||||
| 79 | 0 | my $loginformres = $ua->request($loginformreq); | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | throw Error::Simple('Error while requesting login form: ' . $loginformres->status_line) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | unless $loginformres->is_success; | ||||||||||
| 83 | |||||||||||
| 84 | 0 | my $loginform = HTML::Form->parse($loginformres->content, $loginurl); | |||||||||
| 85 | 0 | $loginform->value( 'login:loginUsernameField' => $username ); | |||||||||
| 86 | 0 | $loginform->value( 'login:password' => $password ); | |||||||||
| 87 | |||||||||||
| 88 | # Send login request .. | ||||||||||
| 89 | 0 | my $loginres = $ua->request($loginform->click); | |||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | if ($loginres->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | # we successfully logged in (probably) | ||||||||||
| 93 | } else { | ||||||||||
| 94 | 0 | throw Error::Simple("Error while trying to log in: ".$loginres->status_line); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | # We can now retrieve our activity ... | ||||||||||
| 98 | 0 | my $req = HTTP::Request->new(GET => 'http://connect.garmin.com/proxy/activity-search-service-1.0/json/activities?_dc=1220170621856&start=0&limit=50'); | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $res = $ua->request($req); | |||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | if ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | return $res->content; | |||||||||
| 104 | } else { | ||||||||||
| 105 | 0 | throw Error::Simple("error while requesting activities (".$res->status_line.")"); | |||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | =head2 parse | ||||||||||
| 110 | |||||||||||
| 111 | method responsible for parsing the json data and returning a simplified array ref of hash refs: | ||||||||||
| 112 | |||||||||||
| 113 | $VAR1 = [ | ||||||||||
| 114 | { | ||||||||||
| 115 | 'begindate' => '2009-02-17', | ||||||||||
| 116 | 'distance' => 3156, | ||||||||||
| 117 | 'name' => 'Untitled', | ||||||||||
| 118 | 'heartrate' => 162, | ||||||||||
| 119 | 'duration' => 1980, | ||||||||||
| 120 | 'activity' => 'Untitled', | ||||||||||
| 121 | 'activityid' => '2194739', | ||||||||||
| 122 | 'id' => '2194739', | ||||||||||
| 123 | 'type' => 'Uncategorized', | ||||||||||
| 124 | 'begin' => 'Tue, Feb 17 \'09 08:27 AM' | ||||||||||
| 125 | }, | ||||||||||
| 126 | { | ||||||||||
| 127 | 'begindate' => '2009-02-17', | ||||||||||
| 128 | 'distance' => 2200, | ||||||||||
| 129 | 'name' => 'Untitled', | ||||||||||
| 130 | 'heartrate' => 157, | ||||||||||
| 131 | 'duration' => 1500, | ||||||||||
| 132 | 'activity' => 'Untitled', | ||||||||||
| 133 | 'activityid' => '2194738', | ||||||||||
| 134 | 'id' => '2194738', | ||||||||||
| 135 | 'type' => 'Uncategorized', | ||||||||||
| 136 | 'begin' => 'Tue, Feb 17 \'09 08:02 AM' | ||||||||||
| 137 | }, | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 143 | 0 | my ($pkg, $content) = @_; | |||||||||
| 144 | 0 | my $json = JSON->new(); | |||||||||
| 145 | |||||||||||
| 146 | 0 | my $results = $json->decode($content); | |||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | 0 | my $activities = $results->{results}->{activities}; | |||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | 0 | my $simpleactivities = []; | |||||||||
| 153 | 0 | foreach my $activity (@$activities) { | |||||||||
| 154 | 0 | my $a = $activity->{activity}; | |||||||||
| 155 | # convert it to something more "userfriendly" | ||||||||||
| 156 | 0 | my $hr = undef; | |||||||||
| 157 | 0 | ▶ | if ($a->{weightedMeanHeartRate}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 0 | $hr = int($a->{weightedMeanHeartRate}->{value}); | |||||||||
| 159 | } | ||||||||||
| 160 | 0 | my $durstr = $a->{sumDuration}->{display}; | |||||||||
| 161 | 0 | my ($hrs, $min, $sec) = split(/:/,$durstr); | |||||||||
| 162 | 0 | my $duration = (($hrs * 60) + $min) * 60 + $sec; | |||||||||
| 163 | my $ac = { 'id' => $a->{activityId}, | ||||||||||
| 164 | 'activityid' => $a->{activityId}, | ||||||||||
| 165 | 'activity' => $a->{activityName}->{value}, | ||||||||||
| 166 | 'name' => $a->{activityName}->{value}, | ||||||||||
| 167 | 'type' => $a->{activityType}->{display}, | ||||||||||
| 168 | 'distance' => int($a->{sumDistance}->{value}*1000),#sprintf("%.2f",$a->{sumDistance}->{value}), | ||||||||||
| 169 | 'duration' => $duration, | ||||||||||
| 170 | 'begin' => $a->{beginTimestamp}->{display}, | ||||||||||
| 171 | 'begindate' => $a->{beginTimestamp}->{value}, | ||||||||||
| 172 | 0 | 'heartrate' => $hr, | |||||||||
| 173 | }; | ||||||||||
| 174 | |||||||||||
| 175 | 0 | push(@$simpleactivities, $ac); | |||||||||
| 176 | } | ||||||||||
| 177 | 0 | return $simpleactivities; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | |||||||||||
| 181 | |||||||||||
| 182 | =head1 AUTHOR | ||||||||||
| 183 | |||||||||||
| 184 | Herbert Poul, C<< <hpoul at cpan.org> >> | ||||||||||
| 185 | |||||||||||
| 186 | =head1 BUGS | ||||||||||
| 187 | |||||||||||
| 188 | Please report any bugs or feature requests to C<bug-gps-garmin-connect at rt.cpan.org>, or through | ||||||||||
| 189 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=GPS-Garmin-Connect>. I will be notified, and then you'll | ||||||||||
| 190 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 191 | |||||||||||
| 192 | |||||||||||
| 193 | |||||||||||
| 194 | |||||||||||
| 195 | =head1 SUPPORT | ||||||||||
| 196 | |||||||||||
| 197 | You can find documentation for this module with the perldoc command. | ||||||||||
| 198 | |||||||||||
| 199 | perldoc GPS::Garmin::Connect | ||||||||||
| 200 | |||||||||||
| 201 | |||||||||||
| 202 | You can also look for information at: | ||||||||||
| 203 | |||||||||||
| 204 | =over 4 | ||||||||||
| 205 | |||||||||||
| 206 | =item * RT: CPAN's request tracker | ||||||||||
| 207 | |||||||||||
| 208 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=GPS-Garmin-Connect> | ||||||||||
| 209 | |||||||||||
| 210 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 211 | |||||||||||
| 212 | L<http://annocpan.org/dist/GPS-Garmin-Connect> | ||||||||||
| 213 | |||||||||||
| 214 | =item * CPAN Ratings | ||||||||||
| 215 | |||||||||||
| 216 | L<http://cpanratings.perl.org/d/GPS-Garmin-Connect> | ||||||||||
| 217 | |||||||||||
| 218 | =item * Search CPAN | ||||||||||
| 219 | |||||||||||
| 220 | L<http://search.cpan.org/dist/GPS-Garmin-Connect> | ||||||||||
| 221 | |||||||||||
| 222 | =back | ||||||||||
| 223 | |||||||||||
| 224 | |||||||||||
| 225 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 226 | |||||||||||
| 227 | |||||||||||
| 228 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 229 | |||||||||||
| 230 | Copyright 2009 Herbert Poul, all rights reserved. | ||||||||||
| 231 | |||||||||||
| 232 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 233 | under the same terms as Perl itself. | ||||||||||
| 234 | |||||||||||
| 235 | |||||||||||
| 236 | =cut | ||||||||||
| 237 | |||||||||||
| 238 | 2 | 1; # End of GPS::Garmin::Connect | |||||||||