#!/usr/bin/perl
# PODNAME: dump_fitbit_data_to_csv
# ABSTRACT: dump the last week's worth of FitBit data to a CSV file.
use strictures 1;

use autodie;
use POSIX;
use WebService::FitBit;


my $file = shift
  or die "Provide file name to dump data into.";

open( my $OUT , '>' , $file );

my $fb = WebService::FitBit->new();

my $day        = 86400;    # 1 day
my $total_days = 7;

### FIXME Text:CSV, anyone?

# Weekly CSV header
print qq{DATE,BURNED,CONSUMED,SCORE,STEPS,DISTANCE,ACTIVE_VERY,ACTIVE_FAIR,ACTIVE_LIGHT,SLEEP_TIME,AWOKEN};
print "\n";

for ( my $i = 0 ; $i < $total_days ; $i++ ) {
  my $previous_day = strftime( "%F", localtime( time - $day ) );
  print "Getting data for $previous_day ...\n";

  print $previous_day . ",";
  print $fb->total_calories($previous_day)->{burned} . ",";
  print $fb->total_calories($previous_day)->{consumed} . ",";
  print $fb->total_active_score($previous_day) . ",";
  print $fb->total_steps($previous_day) . ",";
  print $fb->total_distance($previous_day) . ",";

  my $ah = $fb->total_active_hours($previous_day);
  print $ah->{very} . ",";
  print $ah->{fairly} . ",";
  print $ah->{lightly} . ",";

  my $st = $fb->total_sleep_time($previous_day);
  print $st->{hours_asleep} . ",";
  print $st->{wakes} . "\n";

  $day += 86400;
}

close( $OUT );

__END__
=pod

=head1 NAME

dump_fitbit_data_to_csv - dump the last week's worth of FitBit data to a CSV file.

=head1 VERSION

version 0.1

=head1 AUTHORS

=over 4

=item *

John SJ Anderson <genehack@genehack.org>

=item *

Eric Blue <ericblue76@gmail.com>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by John SJ Anderson.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

