Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Rough implementation of some Beeminder API calls needed for TagTime | ||
# See http://beeminder.com/api-docs | ||
|
||
# Get your personal Beeminder auth token (after signing in) from | ||
# https://www.beeminder.com/api/v1/auth_token.json | ||
# And set a global variable like $beemauth = "abc123"; | ||
# (That's already done in TagTime settings but if you're using this elsewhere | ||
# you'll need to set $beemauth.) | ||
|
||
use LWP::UserAgent; # tip: run 'sudo cpan' and at the cpan prompt do 'upgrade' | ||
use JSON; # then 'install LWP::UserAgent' and 'install JSON' etc | ||
use HTTP::Request::Common; # pjf recomends cpanmin.us | ||
use Data::Dumper; $Data::Dumper::Terse = 1; | ||
$beembase = 'https://www.beeminder.com/api/v1/'; | ||
|
||
# Delete datapoint with given id for beeminder.com/u/g | ||
sub beemdelete { my($u, $g, $id) = @_; | ||
my $ua = LWP::UserAgent->new; | ||
my $uri = $beembase . | ||
"users/$u/goals/$g/datapoints/$id.json?auth_token=$beemauth"; | ||
my $resp = $ua->delete($uri); | ||
beemerr('DELETE', $uri, {}, $resp); | ||
} | ||
|
||
# Fetch all the datapoints for beeminder.com/u/g | ||
sub beemfetch { my($u, $g) = @_; | ||
my $ua = LWP::UserAgent->new; | ||
#$ua->timeout(30); # give up if no response for this many seconds; default 180 | ||
my $uri = $beembase . | ||
"users/$u/goals/$g/datapoints.json?auth_token=$beemauth"; | ||
my $resp = $ua->get($uri); | ||
beemerr('GET', $uri, {}, $resp); | ||
return decode_json($resp->content); | ||
} | ||
|
||
# Create a new datapoint {timestamp t, value v, comment c} for bmndr.com/u/g | ||
# and return the id of the new datapoint. | ||
sub beemcreate { my($u, $g, $t, $v, $c) = @_; | ||
my $ua = LWP::UserAgent->new; | ||
my $uri = $beembase."users/$u/goals/$g/datapoints.json?auth_token=$beemauth"; | ||
my $data = [ timestamp => $t, | ||
value => $v, | ||
comment => $c ]; | ||
my $resp = $ua->post($uri, Content => $data); | ||
beemerr('POST', $uri, {@$data}, $resp); | ||
my $x = decode_json($resp->content); | ||
return $x->{"id"}; | ||
} | ||
|
||
# Update a datapoint with the given id. Similar to beemcreate/beemdelete. | ||
sub beemupdate { my($u, $g, $id, $t, $v, $c) = @_; | ||
my $ua = LWP::UserAgent->new; | ||
my $uri = $beembase . | ||
"users/$u/goals/$g/datapoints/$id.json?auth_token=$beemauth"; | ||
my $data = [ timestamp => $t, | ||
value => $v, | ||
comment => $c ]; | ||
# the following doesn't work for a reason i don't yet understand: | ||
#my $resp = $ua->put(...); | ||
# for some reason it works to instead make a POST request and change the | ||
# method to PUT... | ||
my $req = POST($uri, Content => $data); | ||
$req->method('PUT'); | ||
my $resp = $ua->request($req); | ||
beemerr('PUT', $uri, {@$data}, $resp); | ||
} | ||
|
||
# Takes request type (GET, POST, etc), uri string, hashref of data arguments, | ||
# and response object; barfs verbosely if problems. | ||
# Obviously this isn't the best way to do this. | ||
sub beemerr { my($rt, $uri, $data, $resp) = @_; | ||
if(!$resp->is_success) { | ||
print "Error making the following $rt request to Beeminder:\n$uri\n"; | ||
print Dumper $data; | ||
print $resp->status_line, "\n", $resp->content, "\n"; | ||
exit 1; | ||
} | ||
} | ||
|
||
1; # when requiring a library in perl it has to return 1. | ||
|
||
|
||
# How Paul Fenwick does it in Perl: | ||
#my ($user, $auth_token, $datapoint, $comment); | ||
#my $mech = WWW::Mechanize( autocheck => 1 ) | ||
#$mech->post( | ||
#"http://beeminder.com/api/v1/users/$busr/goals/$slug/datapoints.json? | ||
#auth_token=$auth_token", | ||
#{ | ||
# timestamp => time(), | ||
# value => $datapoint, | ||
# comment => $comment | ||
#} | ||
#); |