| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Weightbot::API; |
|
2
|
|
|
|
|
|
|
{ |
|
3
|
|
|
|
|
|
|
$Weightbot::API::VERSION = '1.1.0'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: get Weightbot iPhone app data from weightbot.com |
|
7
|
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1781
|
use warnings; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
79
|
|
|
9
|
2
|
|
|
2
|
|
14
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
75
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
193
|
|
|
12
|
2
|
|
|
2
|
|
9255
|
use WWW::Mechanize; |
|
|
2
|
|
|
|
|
794241
|
|
|
|
2
|
|
|
|
|
103
|
|
|
13
|
2
|
|
|
2
|
|
2598
|
use Class::Date qw(date); |
|
|
2
|
|
|
|
|
32362
|
|
|
|
2
|
|
|
|
|
15
|
|
|
14
|
2
|
|
|
2
|
|
10054
|
use File::Slurp; |
|
|
2
|
|
|
|
|
69251
|
|
|
|
2
|
|
|
|
|
2436
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
|
18
|
1
|
|
|
1
|
1
|
913
|
my ($class, $self) = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
1
|
50
|
|
|
|
6
|
croak 'No email specified, stopped' unless $self->{email}; |
|
21
|
1
|
50
|
|
|
|
4
|
croak 'No password specified, stopped' unless $self->{password}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
1
|
|
50
|
|
|
11
|
$self->{site} ||= 'https://weightbot.com'; |
|
24
|
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
3
|
bless($self, $class); |
|
26
|
1
|
|
|
|
|
4
|
return $self; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub raw_data { |
|
31
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
32
|
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
0
|
$self->_get_data_if_needed; |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
return $self->{raw_data}; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub data { |
|
40
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
3
|
$self->_get_data_if_needed; |
|
43
|
|
|
|
|
|
|
|
|
44
|
1
|
50
|
|
|
|
5
|
unless ($self->{data}) { |
|
45
|
1
|
|
|
|
|
1
|
my $result; |
|
46
|
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
2
|
my $n = 1; |
|
48
|
1
|
|
|
|
|
1
|
my $prev_date; |
|
49
|
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
3
|
local $Class::Date::DATE_FORMAT="%Y-%m-%d"; |
|
51
|
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
4
|
foreach my $line (split '\n', $self->{raw_data}) { |
|
53
|
5
|
100
|
|
|
|
17
|
next if $line =~ /^date, kilograms, pounds$/; |
|
54
|
4
|
|
|
|
|
26
|
my ($d, $k, $p) = split /\s*,\s*/, $line; |
|
55
|
|
|
|
|
|
|
|
|
56
|
4
|
|
|
|
|
16
|
$d = date($d); |
|
57
|
|
|
|
|
|
|
|
|
58
|
4
|
100
|
|
|
|
1460
|
if ($prev_date) { |
|
59
|
3
|
50
|
|
|
|
556
|
if ($d < $prev_date) { |
|
60
|
0
|
|
|
|
|
0
|
croak "Date '$d' is earlier than '$prev_date', stopped"; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
3
|
|
|
|
|
411
|
my $expected_date = $prev_date + '1D'; |
|
64
|
3
|
|
|
|
|
869
|
while ("$d" ne "$expected_date") { |
|
65
|
1
|
|
|
|
|
148
|
push @$result, { |
|
66
|
|
|
|
|
|
|
date => "$expected_date", |
|
67
|
|
|
|
|
|
|
kg => '', |
|
68
|
|
|
|
|
|
|
lb => '', |
|
69
|
|
|
|
|
|
|
n => $n, |
|
70
|
|
|
|
|
|
|
}; |
|
71
|
1
|
|
|
|
|
78
|
$expected_date += '1D'; |
|
72
|
1
|
|
|
|
|
171
|
$n++; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
4
|
|
|
|
|
448
|
push @$result, { |
|
78
|
|
|
|
|
|
|
date => "$d", |
|
79
|
|
|
|
|
|
|
kg => $k, |
|
80
|
|
|
|
|
|
|
lb => $p, |
|
81
|
|
|
|
|
|
|
n => $n, |
|
82
|
|
|
|
|
|
|
}; |
|
83
|
4
|
|
|
|
|
484
|
$prev_date = $d; |
|
84
|
4
|
|
|
|
|
15
|
$n++; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
1
|
|
|
|
|
7
|
$self->{data} = $result; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
10
|
return $self->{data}; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _get_data_if_needed { |
|
94
|
1
|
|
|
1
|
|
2
|
my ($self) = @_; |
|
95
|
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
3
|
my $cache_filename; |
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
50
|
|
|
|
7
|
if ($self->{use_cache_file}) { |
|
99
|
0
|
0
|
|
|
|
0
|
$cache_filename |
|
100
|
|
|
|
|
|
|
= defined($self->{cache_file}) |
|
101
|
|
|
|
|
|
|
? $self->{cache_file} |
|
102
|
|
|
|
|
|
|
: '/tmp/weightbot.data'; |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
0
|
if (-e $cache_filename) { |
|
105
|
0
|
|
|
|
|
0
|
$self->{raw_data} = read_file($cache_filename); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
1
|
50
|
|
|
|
5
|
unless ($self->{raw_data}) { |
|
110
|
0
|
|
|
|
|
0
|
my $mech = WWW::Mechanize->new( |
|
111
|
|
|
|
|
|
|
agent => "Weightbot::API/$Weightbot::API::VERSION", |
|
112
|
|
|
|
|
|
|
); |
|
113
|
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
$mech->get( $self->{site} . '/account/login'); |
|
115
|
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
0
|
$mech->submit_form( |
|
117
|
|
|
|
|
|
|
form_number => 1, |
|
118
|
|
|
|
|
|
|
fields => { |
|
119
|
|
|
|
|
|
|
email => $self->{email}, |
|
120
|
|
|
|
|
|
|
password => $self->{password}, |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
); |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
0
|
$mech->submit_form( |
|
125
|
|
|
|
|
|
|
form_number => 1, |
|
126
|
|
|
|
|
|
|
); |
|
127
|
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
0
|
if ($mech->content !~ /^date, kilograms, pounds\n/) { |
|
129
|
0
|
|
|
|
|
0
|
croak "Recieved incorrect data, stopped" |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
0
|
$self->{raw_data} = $mech->content; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
1
|
50
|
|
|
|
3
|
if ($self->{use_cache_file}) { |
|
136
|
0
|
|
|
|
|
|
write_file($cache_filename, $self->{raw_data}); |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
__END__ |