| 1 | #--------------------------------------------------------------------- | ||||||||||||
| 2 | package Finance::PaycheckRecords::Fetcher; | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # Copyright 2013 Christopher J. Madsen | ||||||||||||
| 5 | # | ||||||||||||
| 6 | # Author: Christopher J. Madsen <perl@cjmweb.net> | ||||||||||||
| 7 | # Created: 4 Feb 2013 | ||||||||||||
| 8 | # | ||||||||||||
| 9 | # This program is free software; you can redistribute it and/or modify | ||||||||||||
| 10 | # it under the same terms as Perl itself. | ||||||||||||
| 11 | # | ||||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the | ||||||||||||
| 15 | # GNU General Public License or the Artistic License for more details. | ||||||||||||
| 16 | # | ||||||||||||
| 17 | # ABSTRACT: Fetch paystubs from PaycheckRecords.com | ||||||||||||
| 18 | #--------------------------------------------------------------------- | ||||||||||||
| 19 | |||||||||||||
| 20 | 1 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | 1 | our $VERSION = '1.000'; | |||||||||||
| 25 | # This file is part of Finance-PaycheckRecords-Fetcher 1.000 (July 12, 2014) | ||||||||||||
| 26 | |||||||||||||
| 27 | 1 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 1 | ▶ | use File::Slurp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 1 | ▶ | use LWP::UserAgent 6 (); # SSL certificate validation | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 1 | ▶ | use URI (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 1 | ▶ | use URI::QueryParam (); # part of URI; has no version number | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 1 | ▶ | use WWW::Mechanize 1.50 (); # autocheck on | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | #===================================================================== | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | sub new | ||||||||||||
| 39 | 0 | ▶ | { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 40 | 0 | my ($class, $user, $password) = @_; | |||||||||||
| 41 | |||||||||||||
| 42 | 0 | bless { | |||||||||||
| 43 | username => $user, | ||||||||||||
| 44 | password => $password, | ||||||||||||
| 45 | mech => WWW::Mechanize->new, | ||||||||||||
| 46 | }, $class; | ||||||||||||
| 47 | } # end new | ||||||||||||
| 48 | |||||||||||||
| 49 | #--------------------------------------------------------------------- | ||||||||||||
| 50 | # Get a URL, automatically supplying login credentials if needed: | ||||||||||||
| 51 | |||||||||||||
| 52 | sub _get | ||||||||||||
| 53 | 0 | ▶ | { | ||||||||||
|
Subroutine
_get: not called CC=3 SCAR=24.8
| |||||||||||||
| 54 | 0 | my ($self, $url) = @_; | |||||||||||
| 55 | |||||||||||||
| 56 | 0 | my $mech = $self->{mech}; | |||||||||||
| 57 | |||||||||||||
| 58 | 0 | $mech->get($url); | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | if ($mech->form_name('Login_Form')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | $mech->set_fields( | ||||||||||||
| 62 | userStrId => $self->{username}, | ||||||||||||
| 63 | password => $self->{password}, | ||||||||||||
| 64 | 0 | ); | |||||||||||
| 65 | 0 | $mech->click('Login', 5, 4); | |||||||||||
| 66 | # If we still see the login form, we must have failed to login properly | ||||||||||||
| 67 | 0 | ▶ | Carp::croak("PaycheckRecords: login failed") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | if $mech->form_name('Login_Form'); | ||||||||||||
| 69 | } | ||||||||||||
| 70 | } # end _get | ||||||||||||
| 71 | |||||||||||||
| 72 | #--------------------------------------------------------------------- | ||||||||||||
| 73 | 0 | ▶ | sub listURL { 'https://www.paycheckrecords.com/in/paychecks.jsp' } | ||||||||||
|
Subroutine
listURL: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 74 | |||||||||||||
| 75 | |||||||||||||
| 76 | sub available_paystubs | ||||||||||||
| 77 | 0 | ▶ | { | ||||||||||
|
Subroutine
available_paystubs: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 78 | 0 | my ($self) = @_; | |||||||||||
| 79 | |||||||||||||
| 80 | 0 | $self->_get( $self->listURL ); | |||||||||||
| 81 | |||||||||||||
| 82 | my @links = $self->{mech}->find_all_links( | ||||||||||||
| 83 | 0 | url_regex => qr!/in/paystub_printerFriendly\.jsp! | |||||||||||
| 84 | ); | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | my %stub; | |||||||||||
| 87 | |||||||||||||
| 88 | 0 | for my $link (@links) { | |||||||||||
| 89 | 0 | my $url = $link->url_abs; | |||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | $stub{ $url->query_param('date') // die "Expected date= in $url" } | ||||||||||
|
Condition0%0/2
$url->query_param("date") // die("Expected date= in $url")
MC/DC0%0/1
$url->query_param("date") // die("Expected date= in $url")
$url->query_param("date")
Truth table0%0/2
A
A= $url->query_param("date")
| |||||||||||||
| 92 | = $url; | ||||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | \%stub; | |||||||||||
| 96 | } # end available_paystubs | ||||||||||||
| 97 | #--------------------------------------------------------------------- | ||||||||||||
| 98 | |||||||||||||
| 99 | |||||||||||||
| 100 | sub mirror | ||||||||||||
| 101 | 0 | ▶ | { | ||||||||||
|
Subroutine
mirror: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 102 | 0 | my ($self) = @_; | |||||||||||
| 103 | |||||||||||||
| 104 | 0 | my $mech = $self->{mech}; | |||||||||||
| 105 | |||||||||||||
| 106 | 0 | my $stubs = $self->available_paystubs; | |||||||||||
| 107 | |||||||||||||
| 108 | 0 | my @fetched; | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | foreach my $date (sort keys %$stubs) { | |||||||||||
| 111 | 0 | my $fn = "Paycheck-$date.html"; | |||||||||||
| 112 | 0 | ▶ | next if -e $fn; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | $self->_get($stubs->{$date}); | |||||||||||
| 114 | 0 | File::Slurp::write_file( $fn, {binmode => ':utf8'}, $mech->content ); | |||||||||||
| 115 | 0 | push @fetched, $fn; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | @fetched; | |||||||||||
| 119 | } # end mirror | ||||||||||||
| 120 | |||||||||||||
| 121 | #===================================================================== | ||||||||||||
| 122 | # Package Return Value: | ||||||||||||
| 123 | |||||||||||||
| 124 | 1 | 1; | |||||||||||
| 125 | |||||||||||||
| 126 | __END__ | ||||||||||||