-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitbitpost
executable file
·185 lines (151 loc) · 4.94 KB
/
fitbitpost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/.";
use Data::Dumper;
use OAuthSimple;
use WWW::Curl::Easy;
use WWW::Curl::Form;
use CGI::Simple;
#use Text::CSV;
# Set debug to 1 for great log messages:
my $DEBUG = 0;
my $accept_language_key = "Accept-Language";
# If your weights are in kilograms, try en_UK.
my $accept_language_value = "en_US";
my $oauth = new OAuthSimple();
# Set the action type to POST, otherwise the base string is wrong.
$oauth->setAction('POST');
# Read API keys from ~/.api_keys
# or just fill these in with your
# app's values from https://dev.fitbit.com/apps
# Put your home directory path here:
my $home = '~/';
# Where's your CSV? Mine is next to this script:
my $file = "weightbot_data.csv";
my %keys;
$keys{oauth_consumer_key} = "3f7661c5e85d4e61821d5d4503753bd4";
$keys{oauth_shared_secret} = "cbb04dd0b755489f9378e8be12217e41";
$keys{oauth_token} = "oauth_token";
$keys{oauth_token_secret} = "oauth_token_secret";
# .api_keys prefix:
$keys{api_keys_prefix} = "fitbit_uploader";
#sub fetchkey()
{
# my @keysToRead = ( keys(%keys) );
# my $file = "$home/.api_keys";
# open(MYINPUTFILE, "<$file") or die "Couldn't open '$file': $!";
# print "Reading API keys from $file:\n" if $DEBUG;
# my(@lines) = <MYINPUTFILE>; # read file into list
# my($line);
# foreach $line (@lines) # loop thru list
# {
# my @linekeys = split('=', $line);
# foreach $key (@keysToRead) {
# my $searchVar = $keys{api_keys_prefix} . "_" . $key;
# if (@linekeys[0] eq $searchVar) {
# my $value = @linekeys[1];
# chomp($value);
# print "Setting $key to $value\n" if $DEBUG;
# $keys{$key} = $value;
# }
# }
# }
# close(MYINPUTFILE);
#}
#fetchkey();
my $weight_key = "weight";
my $date_key = "date";
my $api_version = "1";
my $response_format = "json";
my $api_base_url = "http://api.fitbit.com";
my $info_get_url = "/$api_version/user/-/profile.$response_format";
my $activity_post_url = "/$api_version/user/-/body/activities.$response_format";
my $full_post_url = $api_base_url . $activity_post_url;
print "Uploading to $full_post_url\n" if $DEBUG;
# Upload:
$oauth->reset();
my $signedpost = $oauth->sign(
{
path => $full_post_url,
signatures => {
oauth_consumer_key => $keys{oauth_consumer_key},
shared_secret => $keys{oauth_shared_secret},
oauth_token => $keys{oauth_token},
oauth_secret => $keys{oauth_token_secret},
},
parameters => {
'activityid' => '17151',
'startTime' => '12:00',
'durationMillis' => '86400000',
'date' => '2014-07-25',
'distance' => '30850',
'distanceUnit' => 'Steps',
},
},
);
# Debug logging of the signing object and request URLs:
# print "???????????????????????????????????????\n" if $DEBUG;
# print Dumper($signedpost) if $DEBUG;
# print "???????????????????????????????????????\n" if $DEBUG;
# print "Post URL: $full_post_url\n" if $DEBUG;
# print "???????????????????????????????????????\n" if $DEBUG;
# Post a record:
# Make form data:
my $curlf = new WWW::Curl::Form;
# $curlf->formadd($weight_key, $weight);
# $curlf->formadd($date_key, $date);
# Make a post request:
my $postcurl = WWW::Curl::Easy->new();
$postcurl->setopt( CURLOPT_URL, $full_post_url );
# set the auth and language headers:
my @httpheaders = ["Authorization: $signedpost->{header}", "$accept_language_key: $accept_language_value" ];
$postcurl->pushopt( CURLOPT_HTTPHEADER, @httpheaders );
# debugging proxy:
# $postcurl->setopt( CURLOPT_PROXY, "127.0.0.1");
# $postcurl->setopt( CURLOPT_PROXYPORT, "8888");
# Set the form data:
$postcurl->setopt(CURLOPT_HTTPPOST, $curlf);
# Set up the response body:
my $postcurl_responsebody;
$postcurl->setopt(CURLOPT_WRITEDATA,\$postcurl_responsebody);
# Post the data:
my $request_retcode = $postcurl->perform;
my ($request_success, $response_code) = checkreturncode($postcurl, $request_retcode);
# Parse the response:
my $q = new CGI::Simple($postcurl_responsebody);
if ($request_success && ( $response_code == 200 || $response_code == 201) ) {
print "Success!";
print " Response:\n" if $DEBUG;
print "$postcurl_responsebody\n" if $DEBUG;
}
else {
print("Failed to post data.\n");
print "Response was:\n" if $DEBUG;
print Dumper($postcurl_responsebody) if $DEBUG;
exit 1;
}
#} else {
# warn "Line could not be parsed: $line\n";
#}
}
sub checkreturncode {
# Starts the actual request
my $curl = $_[0];
my $retcode = $_[1];
# Looking at the results...
if ($retcode == 0) {
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# print("Received response: $response_body\n");
if ($response_code != 200 && $response_code != 201) {
print "Got a non-200 reply: $response_code\n";
}
return (1, $response_code);
# judge result and next action based on $response_code
} else {
# Error code, type of error, error message
print("An error happened: $retcode:\n".$curl->strerror($retcode)."\n".$curl->errbuf."\n");
return 0;
}
}