line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Google::Portfolio; |
2
|
|
|
|
|
|
|
# ABSTRACT: Manipulate Google Finance portfolios a little |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
197279
|
use 5.008; |
|
1
|
|
|
|
|
9
|
|
5
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
469
|
use Moo; |
|
1
|
|
|
|
|
9385
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
1583
|
use namespace::clean; |
|
1
|
|
|
|
|
9510
|
|
|
1
|
|
|
|
|
7
|
|
10
|
1
|
|
|
1
|
|
813
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
33724
|
|
|
1
|
|
|
|
|
33
|
|
11
|
1
|
|
|
1
|
|
499
|
use HTML::Form; |
|
1
|
|
|
|
|
5505
|
|
|
1
|
|
|
|
|
30
|
|
12
|
1
|
|
|
1
|
|
616
|
use JSON::PP; |
|
1
|
|
|
|
|
11408
|
|
|
1
|
|
|
|
|
76
|
|
13
|
1
|
|
|
1
|
|
7
|
use Carp 'croak'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
14
|
1
|
|
|
1
|
|
6
|
use URI; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
821
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '1.05'; # VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has user => ( is => 'rwp' ); |
19
|
|
|
|
|
|
|
has passwd => ( is => 'rwp' ); |
20
|
|
|
|
|
|
|
has is_authed => ( is => 'rwp', default => 0 ); |
21
|
|
|
|
|
|
|
has hash => ( is => 'rwp' ); |
22
|
|
|
|
|
|
|
has json => ( is => 'ro', default => sub { JSON::PP->new->utf8->allow_barekey } ); |
23
|
|
|
|
|
|
|
has ua => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
default => sub { |
26
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
27
|
|
|
|
|
|
|
max_redirect => 24, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
push( @{ $ua->requests_redirectable }, 'POST' ); |
30
|
|
|
|
|
|
|
$ua->cookie_jar({}); |
31
|
|
|
|
|
|
|
return $ua; |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub BUILD { |
36
|
1
|
|
|
1
|
0
|
12
|
my ($self) = @_; |
37
|
1
|
50
|
33
|
|
|
17
|
$self->login( $self->user, $self->passwd ) if ( $self->user and $self->passwd ); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub login { |
41
|
0
|
|
|
0
|
1
|
|
my ( $self, $user, $passwd ) = @_; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
$self->_set_user($user) if ($user); |
44
|
0
|
0
|
|
|
|
|
$self->_set_passwd($passwd) if ($passwd); |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
0
|
|
|
|
croak('Must provide "user" and "passwd" values to login() or new()') |
47
|
|
|
|
|
|
|
unless ( $self->user and $self->passwd ); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $form = ( HTML::Form->parse( |
50
|
|
|
|
|
|
|
$self->ua->request( HTTP::Request->new( 'GET', 'https://mail.google.com/tasks/ig' ) ) |
51
|
|
|
|
|
|
|
) )[0]; |
52
|
0
|
|
|
|
|
|
$form->value( 'Email', $self->user ); |
53
|
0
|
|
|
|
|
|
$form = ( HTML::Form->parse( $self->ua->request( $form->click ) ) )[0]; |
54
|
0
|
|
|
|
|
|
$form->value( 'Passwd', $self->passwd ); |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $res = $self->ua->request( $form->click ); |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
croak('Authentication failed; check user and passwd values and that LWP::Protocol::https is installed') |
59
|
|
|
|
|
|
|
if ( $res->content =~ /Sign in/ ); |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$self->_set_is_authed(1); |
62
|
0
|
|
|
|
|
|
return $self; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub portfolio { |
66
|
0
|
|
|
0
|
1
|
|
my ( $self, $pid ) = @_; |
67
|
0
|
|
0
|
|
|
|
$pid ||= 1; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $res = $self->ua->request( |
70
|
|
|
|
|
|
|
HTTP::Request->new( 'GET', 'https://www.google.com/finance/portfolio?action=view&pid=' . $pid ) |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my ($fgp_data_string) = grep { $_ =~ /^\s*google\.finance\.data\s/ } split( "\n", $res->content ); |
|
0
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$fgp_data_string =~ s/^\s*google\.finance\.data\s*=\s*//; |
75
|
0
|
|
|
|
|
|
$fgp_data_string =~ s/;$//; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $fgp_data = $self->json->decode($fgp_data_string); |
78
|
0
|
|
|
|
|
|
$self->_set_hash( $fgp_data->{common}{hash} ); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return $fgp_data->{portfolio_view}{portfolio_table}{cps}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
my @months = qw( January February March April May June July August September October November December ); |
85
|
|
|
|
|
|
|
sub add { |
86
|
0
|
|
|
0
|
1
|
|
my ( $self, $details ) = @_; |
87
|
0
|
0
|
|
|
|
|
croak('Transaction details must be supplied to add() as a hashref') unless ( ref $details eq 'HASH' ); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
my $uri = URI->new; |
90
|
|
|
|
|
|
|
$uri->query_form( |
91
|
|
|
|
|
|
|
editmode => 'trans', |
92
|
|
|
|
|
|
|
menu_type => 'transaction', |
93
|
|
|
|
|
|
|
pid => $details->{pid} || 1, |
94
|
|
|
|
|
|
|
add_ttype_1 => uc( $details->{type} || '' ), |
95
|
|
|
|
|
|
|
add_symbols_1 => $details->{symbol}, |
96
|
|
|
|
|
|
|
add_shares_1 => $details->{shares}, |
97
|
|
|
|
|
|
|
add_price_1 => $details->{price}, |
98
|
|
|
|
|
|
|
add_commission_1 => $details->{commission}, |
99
|
|
|
|
|
|
|
add_notes_1 => $details->{notes}, |
100
|
|
|
|
|
|
|
add_is_cash_synced_1 => 'on', |
101
|
0
|
|
0
|
|
|
|
add_date_1 => $details->{date} || do { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
102
|
|
|
|
|
|
|
my ( $month, $day, $year ) = ( localtime() )[ 4, 3, 5 ]; |
103
|
|
|
|
|
|
|
$months[$month] . ' ' . $day . ', ' . ( $year + 1900 ); |
104
|
|
|
|
|
|
|
}, |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new( |
108
|
|
|
|
|
|
|
'POST', |
109
|
|
|
|
|
|
|
'https://www.google.com/finance/portfolio?action=add&hash=' . $self->hash, |
110
|
|
|
|
|
|
|
); |
111
|
0
|
|
|
|
|
|
$req->content( $uri->query ); |
112
|
0
|
|
|
|
|
|
$self->ua->request($req); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub watchlist { |
117
|
0
|
|
|
0
|
1
|
|
my ( $self, $details ) = @_; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my $uri = URI->new; |
120
|
|
|
|
|
|
|
$uri->query_form( |
121
|
|
|
|
|
|
|
editmode => 'trans', |
122
|
|
|
|
|
|
|
pid => $details->{pid} || 1, |
123
|
|
|
|
|
|
|
watchlist => ( |
124
|
|
|
|
|
|
|
( ref( $details->{list} ) eq 'ARRAY' ) |
125
|
0
|
|
|
|
|
|
? join( ' ', @{ $details->{list} } ) |
126
|
|
|
|
|
|
|
: $details->{list} |
127
|
0
|
0
|
0
|
|
|
|
), |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new( |
131
|
|
|
|
|
|
|
'POST', |
132
|
|
|
|
|
|
|
'https://www.google.com/finance/portfolio?action=edit_portfolio_del_btn&hash=' . $self->hash, |
133
|
|
|
|
|
|
|
); |
134
|
0
|
|
|
|
|
|
$req->content( $uri->query ); |
135
|
0
|
|
|
|
|
|
$self->ua->request($req); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |