/
YahooJapan.pm
274 lines (215 loc) · 9.11 KB
/
YahooJapan.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package Finance::Quote::YahooJapan;
use utf8;
use 5.018;
use warnings;
use HTML::TreeBuilder 5 -weak;
use URI::Escape;
our $VERSION = 'v1.2.1';
# Maximum number of symbols that a search query can contain.
my $n_symbols_per_query = 4;
# Maximum number of page links to follow per query.
my $n_pages_per_query = 3;
# Delay in seconds between HTTP requests.
my $delay_per_request = 0.25;
sub methods {
return (yahoo_japan => \&yahoo_japan);
}
sub labels {
return (yahoo_japan => ['method', 'success', 'symbol', 'name', 'date',
'isodate', 'time', 'currency', 'price', 'errormsg']);
}
sub yahoo_japan {
my ($quoter, @symbols) = @_;
return if (!@symbols); # do nothing if no symbols.
my $ua = $quoter->get_user_agent;
my $url_base = 'https://finance.yahoo.co.jp/search/';
my %info = ();
my @retry_later = ();
# initial trial loop: ignore page links.
while (my @syms = splice @symbols, 0, $n_symbols_per_query) {
my $url = $url_base . '?query=' . join '+', map { uri_escape($_) } @syms;
# trick to avoid single-item pages
$url .= '+%5EDJI' if (@syms < 3 && @syms < $n_symbols_per_query);
my $reply = $ua->get($url);
if ($reply->is_success) {
my $tree = HTML::TreeBuilder->new;
$tree->ignore_unknown(0);
$tree->parse_content($reply->content);
my %quotes = _scrape($tree);
my $has_next_page = _has_next_page($tree, 1);
for my $sym (@syms) {
next if ($info{$sym, 'success'});
if (exists $quotes{$sym}) {
%info = (%info, _convert_quote($quoter, $sym, $quotes{$sym}));
} elsif ($has_next_page) {
push @retry_later, $sym;
} else {
$info{$sym, 'success'} = 0;
$info{$sym, 'symbol'} = $sym;
$info{$sym, 'method'} = 'yahoo_japan';
$info{$sym, 'errormsg'} = 'Requested quote not found.';
}
}
}
if (@symbols) { select undef, undef, undef, $delay_per_request; }
}
# retry loop: follow page links.
while (my @syms = splice @retry_later, 0, $n_symbols_per_query) {
my %quotes = ();
my $url = $url_base . '?query=' . join '+', map { uri_escape($_) } @syms;
# trick to avoid single-item pages
$url .= '+%5EDJI' if (@syms < 3 && @syms < $n_symbols_per_query);
for (my $page = 1; $page <= $n_pages_per_query; $page++) {
select undef, undef, undef, $delay_per_request;
my $reply = $ua->get($url . '&page=' . $page);
if ($reply->is_success) {
my $tree = HTML::TreeBuilder->new;
$tree->ignore_unknown(0);
$tree->parse_content($reply->content);
%quotes = (%quotes, _scrape($tree));
my $has_next_page = _has_next_page($tree, $page);
last if (!$has_next_page);
}
}
for my $sym (@syms) {
next if ($info{$sym, 'success'});
if (exists $quotes{$sym}) {
%info = (%info, _convert_quote($quoter, $sym, $quotes{$sym}));
} else {
$info{$sym, 'success'} = 0;
$info{$sym, 'symbol'} = $sym;
$info{$sym, 'method'} = 'yahoo_japan';
$info{$sym, 'errormsg'} = 'Requested quote not found.';
}
}
}
return %info if wantarray;
return \%info;
}
sub n_symbols_per_query {
my $class = shift;
return $n_symbols_per_query if (!@_);
$n_symbols_per_query = shift;
return $class;
}
sub n_pages_per_query {
my $class = shift;
return $n_pages_per_query if (!@_);
$n_pages_per_query = shift;
return $class;
}
sub delay_per_request {
my $class = shift;
return $delay_per_request if (!@_);
$delay_per_request = shift;
return $class;
}
# Tests if a list page has the next page of it.
sub _has_next_page {
my ($tree, $current_page) = @_;
my $elm_paging = $tree->look_down('id', 'pagerbtm');
if (defined $elm_paging) {
for my $page_link ($elm_paging->find('button')) {
my $num = $page_link->as_text;
return 1 if ($num =~ /^[0-9]+$/ && $num == $current_page + 1);
}
}
return 0;
}
# Converts an internal quote data to Finance::Quote-style one.
sub _convert_quote {
my ($quoter, $sym, $quote) = @_;
my %info = ();
$info{$sym, 'symbol'} = $sym;
$info{$sym, 'currency'} = 'JPY';
$info{$sym, 'method'} = 'yahoo_japan';
$info{$sym, 'name'} = $quote->{'name'};
$info{$sym, 'date'} = $quote->{'date'};
$info{$sym, 'isodate'} = $quote->{'date'};
$info{$sym, 'time'} = $quote->{'time'};
$info{$sym, 'price'} = $quote->{'price'};
# validate quote.
my @errors = ();
push @errors, 'Invalid name.' if ($info{$sym, 'name'} =~ /^\s*$/);
push @errors, 'Invalid price.' if ($info{$sym, 'price'} eq '');
if ($info{$sym, 'date'} eq '') {
push @errors, 'Invalid datetime.';
} else {
$quoter->store_date(\%info, $sym, { isodate => $info{$sym, 'date'} });
}
$info{$sym, 'errormsg'} = join ' / ', @errors;
$info{$sym, 'success'} = $info{$sym, 'errormsg'} ? 0 : 1;
return %info;
}
sub _scrape {
my $tree = shift;
my %quotes = ();
my $container = $tree->look_down('id', 'sr');
if (defined $container) {
# process each <article> that represents a single item
for my $e ($container->find('article')) {
my $sym = $e->look_down('class', '_2QwBsxBs')->as_text;
my ($date, $time) = _parse_datetime($e->find('time')->as_text);
my $quote = {
name => $e->find('h2')->as_text,
price => $e->look_down('class', '_3rXWJKZF')->as_text,
date => $date,
time => $time
};
$quote->{'price'} =~ tr/.0-9//cd; # strip commas, etc.
# for a stock code, register a duplicate quote with market letter
if ($sym =~ /^[0-9A-Z]{2}[0-9][0-9A-Z][0-9]?$/) {
my $pat = qr/(?:quote\/|code=)($sym\.[A-Z])/;
$e->look_down('_tag', 'a', 'href', $pat)->attr('href') =~ $pat;
$quotes{lc $1} = $quote if (defined $1);
}
# XXX destructive when a stock quote from other market already exists
$quotes{$sym} = $quote;
}
}
return %quotes;
}
# Determines the date and time of a quote.
sub _parse_datetime($;) {
my $datetime = shift;
my @now = localtime;
my ($year, $mon, $mday, $time) = ($now[5] + 1900, 0, 0, '15:00:00');
if ($datetime =~ /([0-9]{1,2}):([0-9]{1,2})/) {
# HH:MM
$time = sprintf '%02d:%02d:00', $1, $2;
($mon, $mday) = ($now[4] + 1, $now[3]);
}
if ($datetime =~ /([0-9]{1,2})\/([0-9]{1,2})/) {
# MM/DD
($mon, $mday) = ($1, $2);
$year-- if ($now[4] + 1 < $mon); # MM may point last December in January.
}
my $date = sprintf '%04d-%02d-%02d', $year, $mon, $mday;
return ($mon && $mday) ? ($date, $time) : ('', '');
}
1;
__END__
=head1 NAME
Finance::Quote::YahooJapan - A Perl module that enables GnuCash to get quotes of Japanese stocks and mutual funds from Yahoo! Finance JAPAN.
=head1 SYNOPSIS
use Finance::Quote;
my $q = Finance::Quote->new('-defaults', 'YahooJapan');
my %quotes = $q->fetch('yahoo_japan', '6758', '6861', '7203');
=head1 DESCRIPTION
Finance::Quote::YahooJapan is a submodule of Finance::Quote, and adds support for Japanese stock and mutual fund quotes. This module extracts these quotes from the result pages of Yahoo! Finance JAPAN's stock price search service. Thus this module enables GnuCash to obtain Japanese quotes through its online price update feature.
=head1 SETUP
=head2 1. Install Finance::Quote
Install and setup Finance::Quote module as explained in the GnuCash Help Manual: L<https://www.gnucash.org/docs/v5/C/gnucash-manual/acct-create.html#accts-online-quotes>
=head2 2. Install Finance::Quote::YahooJapan
a. Type C<cpanm https://github.com/LiosK/Finance--Quote--YahooJapan.git> in the terminal. Or, if you don't prefer to use C<cpanm>, locate the directory where F<Finance::Quote::*> are installed, and then put F<lib/Finance/Quote/YahooJapan.pm> in the directory.
b. Set the C<FQ_LOAD_QUOTELET> environment variable to C<-defaults YahooJapan> in order to load Finance::Quote::YahooJapan.
=head2 3. Setup GnuCash Online Quote Feature
Launch GnuCash and setup your securities as explained in the Manual: L<https://www.gnucash.org/docs/v5/C/gnucash-manual/acct-create.html#accts-online-quotes>
=head1 LICENSE
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Quotations fetched through this module are bound by Yahoo!'s terms and conditions. See L<https://finance.yahoo.co.jp/> for more details.
=head1 AUTHOR
LiosK E<lt>contact@mail.liosk.netE<gt>
=head1 SEE ALSO
Finance::Quote
=cut