blib/lib/WebService/Idonethis.pm

filter: Statementstmt 46.0% 53 / 115 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 68.1% 15 / 22 Podpod 80.0% 4 / 5 totaltotal 40.4% 72 / 178 CC 16 SCAR 29.9
CC
16
Cov
36%
__ANON__
56.0
BUILD
42.0
get_range
12.0
CRAP 19.9
↑ Summary
1 package WebService::Idonethis;
2 1 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::XDG;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTML::Entities qw(decode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my $json = JSON::Any->new;
19  
20 # ABSTRACT: WebScraping pseudo-API for iDoneThis
21  
22 1 our $VERSION = '0.23'; # VERSION: Generated by DZP::OurPkg:Version
23  
24  
25 1 has agent => ( is => 'rw' );
26 1 has calendar => ( is => 'rw' );
27 1 has user_url => ( is => 'rw' );
28 1 has user => ( is => 'rw' );
29 1 has xdg => ( is => 'rw' );
30  
31 0 ▶sub BUILD {
Subroutine BUILD: not called CC=6 SCAR=37.4
Pod undocumented
32 0 my ($self, $args) = @_;
33  
34 0 my $agent = $self->agent;
35  
36 0 ▶ if (not $self->xdg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->xdg) 0 0 0
37  
38 # XDG is used to figure out where to store cache and config
39 # files. If not provided at initialisation time, we'll
40 # make our own.
41  
42 0 $self->xdg(File::XDG->new(name => 'webservice-idonethis-perl'));
43 }
44  
45 # Theoretically these may get changed after login.
46 0 ▶ $self->calendar( $args->{calendar} ? $args->{calendar} : $args->{user} );
Branch: 1 branch
BranchTrueFalseTotal
$args->{'calendar'} ? : 0 0 0
47 0 $self->user ( $args->{user} );
48 0 $self->user_url( "https://idonethis.com/cal/$self->calendar/" );
49  
50 0 ▶ if (not $agent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agent) 0 0 0
51  
52 # Initialise user-agent if none provided, storing cookies in
53 # the xdg cache.
54  
55 0 my $xdg = $self->xdg;
56  
57 0 ▶ if (not -e $xdg->cache_home) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $xdg->cache_home) 0 0 0
58 0 mkdir($xdg->cache_home);
59 }
60  
61 0 my $user_cache = File::Spec->catfile( $xdg->cache_home, $self->user );
62  
63 0 ▶ if (not -e $user_cache) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $user_cache) 0 0 0
64 0 mkdir($user_cache);
65 }
66  
67 0 $agent = WWW::Mechanize->new(
68 agent => "perl/$], WebService::Idonethis/" . $self->VERSION,
69 cookie_jar => HTTP::Cookies->new( file => File::Spec->catfile( $user_cache , "cookies") ),
70 keep_alive => 1,
71 stack_depth => 0, # Don't keep history
72 );
73  
74 0 $self->agent( $agent );
75  
76 }
77  
78 # Ping idonethis to see if we even need to login.
79  
80 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 $self->get_today; # Throws on failure
82 }
83 0 ▶ catch {
Subroutine __ANON__: not called CC=7 SCAR=40.3
84 # Our ping failed, so login instead.
85  
86 0 $agent->get( "https://idonethis.com/accounts/login/" );
87  
88 $agent->submit_form(
89 form_id => 'register',
90 fields => {
91 username => $args->{user},
92 password => $args->{pass},
93 }
94 0 );
95  
96 0 my $url = $agent->uri;
97  
98 0 ▶ if ($url =~ m{/login/?\#?$}i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($url =~ m[/login/?\#?$]i) { } 0 0 0
elsif ($url =~ m[/home/?\#?$]i) { } 0 0 0
elsif (not $url =~ m[/cal/\w+/?\#?$]) { } 0 0 0
99 # Bounced back to login screen. Login failed
100 0 croak "Login to idonethis failed (wrong username/password?)";
101 }
102 elsif ($url =~ m{/home/?\#?$}i) {
103  
104 # Taken to home-screen. Login successful, but we need to
105 # chase our calendar link.
106  
107 0 my $cal = $self->calendar;
108  
109 {
110 # Disable autochecks for now.
111 0 local $agent->{autocheck} = 0;
112  
113 # First try a link with the calendar name at the end.
114 # Failing that, try the calendar in the name
115 # Failing that, try personal.
116  
117 0 ▶ $agent->follow_link( url_regex => qr{/$cal/?\#?} ) //
Condition0%0/6
($agent->follow_link("url_regex", qr"/$cal/?\#?") // $agent->follow_link("text_regex", qr/\b$cal\b/)) // $agent->follow_link("text", "personal")
l!l&&r!l&&!r
000
$agent->follow_link("url_regex", qr"/$cal/?\#?") // $agent->follow_link("text_regex", qr/\b$cal\b/)
l!l&&r!l&&!r
000
MC/DC0%0/3
($agent->follow_link("url_regex", qr"/$cal/?\#?") // $agent->follow_link("text_regex", qr/\b$cal\b/)) // $agent->follow_link("text", "personal")
$agent->follow_link("url_regex", qr"/$cal/?\#?") $agent->follow_link("text_regex", qr/\b$cal\b/) $agent->follow_link("text", "personal")
Truth table0%0/4
A // B // C
A= $agent->follow_link("url_regex", qr"/$cal/?\#?")B= $agent->follow_link("text_regex", qr/\b$cal\b/)C= $agent->follow_link("text", "personal")
ABCresult
1XX1
01X1
0011
0000
118 $agent->follow_link( text_regex => qr{\b$cal\b} ) //
119 $agent->follow_link( text => 'personal' ) ;
120 }
121 }
122 elsif ($url !~ m{/cal/\w+/?\#?$}) {
123 # Oh noes! Where are we?
124 0 croak "Login to idonethis failed (unexpected URL $url)";
125 }
126  
127 # At this point, we *should* be on our calendar page.
128 0 $url = $agent->uri;
129  
130 0 $url =~ s/\#?$//; # Remove trailing hash, if present
131  
132 0 ▶ if ($url =~ m{/cal/(?<cal>[\w-]+)/?$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[/cal/(?<cal>[\w-]+)/?$]) { } 0 0 0
133  
134 # Looks like a valid calendar! Remember our calendar
135 # URL and username.
136  
137 0 $self->user_url( $url );
138 0 $self->calendar( $+{cal} );
139 }
140 else {
141 0 croak "Failed to navigate to idonethis calendar (" . $self->calendar. ") (found self at $url)";
142 }
143  
144 # We used to save the cookie jar on destruction, but that
145 # caused a hiccup with Moo. Now we save immediately after
146 # login.
147  
148 0 $self->agent->cookie_jar->save();
149 0 };
150  
151 0 return;
152  
153 }
154  
155  
156 0 ▶sub get_day {
Subroutine get_day: not called CC=1 SCAR=6.9
Pod documented
157 0 my ($self, $date) = @_;
158  
159 0 return $self->get_range($date, $date);
160 }
161  
162  
163 0 ▶sub get_today {
Subroutine get_today: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self) = @_;
165 0 my $today = strftime("%Y-%m-%d",localtime);
166  
167 0 return $self->get_day( $today );
168 }
169  
170  
171 0 ▶sub get_range {
Subroutine get_range: not called CC=3 SCAR=24.8
Pod documented
172  
173 0 my ($self, $start, $end) = @_;
174  
175 0 my $url = $self->user_url . "dailydone?";
176  
177 0 $url .= "start=$start&end=$end";
178  
179 0 $self->agent->get($url);
180  
181 # Decode JSON
182  
183 0 my $data = $json->decode( $self->agent->content );
184  
185 # Decode HTML entities.
186  
187 0 foreach my $record (@$data) {
188 0 ▶ $record->{text} = decode_entities($record->{text}) if $record->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"text"} 0 0 0
189 }
190  
191 0 return $data;
192 }
193  
194  
195 0 ▶sub set_done {
Subroutine set_done: not called CC=3 SCAR=24.8
Pod documented
196  
197 0 my ($self, %args) = @_;
198  
199 # TODO: Use real date objects.
200 # TODO: Allow more arguments to be passed.
201  
202 0 my $now = time();
203 0 my $timestamp = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($now));
204  
205 0 ▶ my $date = $args{date} || strftime("%Y-%m-%d", localtime($now));
Condition0%0/3
$args{'date'} || strftime('%Y-%m-%d', localtime $now)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'date'} || strftime('%Y-%m-%d', localtime $now)
$args{'date'} strftime('%Y-%m-%d', localtime $now)
Truth table0%0/3
A || B
A= $args{'date'}B= strftime('%Y-%m-%d', localtime $now)
ABresult
1X1
011
000
206 0 ▶ my $text = $args{text} or croak "set_done requires a 'text' argument";
Branch: 1 branch
BranchTrueFalseTotal
my $text = $args{"text"} or croak("set_done requires a 'text' argument") 0 0 0
207  
208 0 my $done_json = $json->encode({
209 calendar => $self->user,
210 owner => $self->user,
211 created => $timestamp,
212 modified => $timestamp,
213 done_date => $date,
214 text => $text,
215 total_comments => undef,
216 total_likes => undef,
217 url => undef,
218 });
219  
220 # TODO: There's got to be a better way of doing JSON posts than this...
221  
222 0 my $req = HTTP::Request->new( 'POST', $self->user_url . "dailydone?" );
223 0 $req->header ( 'Content-Type' => 'application/json' );
224 0 $req->header ( 'Accept' => 'application/json, text/javascript, */*; q0.01' );
225 0 $req->content( $done_json );
226  
227 # NOTE: This is wrong, and you should never do it, but it looks like
228 # we have to send this header for idonethis to accept the request.
229  
230 $req->header (
231 'X-CSRFToken' =>
232 0 $self->agent->cookie_jar->{COOKIES}{'idonethis.com'}{'/'}{csrftoken}[1]
233 );
234  
235 0 my $response = $self->agent->request( $req );
236  
237 # TODO: Check we die automatically on failed submission.
238  
239 0 return;
240 }
241  
242 1 __PACKAGE__->meta->make_immutable;
243  
244  
245 1 1;
246  
247 __END__
↑ Summary