| 1 |
# |
| 2 |
# Copyright (C) 2024, Przemyslaw Kryger |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# |
| 17 |
# $Id: $ |
| 18 |
# |
| 19 |
|
| 20 |
package Finance::Quote::MorningstarJP; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use constant DEBUG => $ENV{DEBUG}; |
| 25 |
use if DEBUG, 'Smart::Comments'; |
| 26 |
|
| 27 |
use XML::LibXML; |
| 28 |
use LWP::UserAgent; |
| 29 |
use String::Util qw(trim); |
| 30 |
|
| 31 |
our $VERSION = '1.71'; # VERSION |
| 32 |
|
| 33 |
our $DISPLAY = 'Morningstar JP'; |
| 34 |
our @LABELS = qw/nav isin symbol name currency date isodate/; |
| 35 |
our $METHODHASH = {subroutine => \&morningstarjp, |
| 36 |
display => $DISPLAY, |
| 37 |
labels => \@LABELS}; |
| 38 |
|
| 39 |
sub methodinfo { |
| 40 |
return ( |
| 41 |
morningstarjp => $METHODHASH, |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; } |
| 46 |
|
| 47 |
sub methods { |
| 48 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 49 |
} |
| 50 |
|
| 51 |
sub morningstarjp { |
| 52 |
my $quoter = shift; |
| 53 |
my @symbols = @_; |
| 54 |
my $ua = $quoter->user_agent(); |
| 55 |
my %info; |
| 56 |
|
| 57 |
foreach my $symbol (@_) { |
| 58 |
eval { |
| 59 |
my $url = "https://apl.wealthadvisor.jp/webasp/funddataxml/basic/basic_$symbol.xml"; |
| 60 |
my $reply = $ua->get($url); |
| 61 |
|
| 62 |
my $dom; |
| 63 |
eval {$dom = XML::LibXML->load_xml(string => $reply->decoded_content)}; |
| 64 |
if ($@) { |
| 65 |
$info{$symbol, 'success' } = 0; |
| 66 |
$info{$symbol, 'errormsg' } = $@; |
| 67 |
next; |
| 68 |
} |
| 69 |
### [<now>] DOM: $dom->toString() |
| 70 |
|
| 71 |
unless ($dom->findnodes('//Fund/@MS_FUND_CODE')->[0]->to_literal() eq $symbol) { |
| 72 |
$info{$symbol, 'success'} = 0; |
| 73 |
$info{$symbol, 'errormsg'} = 'Symbol not found'; |
| 74 |
next; |
| 75 |
} |
| 76 |
|
| 77 |
my $nav = $dom->findnodes('//Fund/Price/@KIJYUNKAGAKU')->[0]->to_literal(); |
| 78 |
$nav =~ s/,//; |
| 79 |
my $date = $dom->findnodes('//Fund/Price/@KIJYUN_YMD')->[0]->to_literal(); |
| 80 |
$date =~ s/[^0-9]/-/g; |
| 81 |
my $isin = $dom->findnodes('//Fund/@ISIN')->[0]->to_literal(); |
| 82 |
my $name = $dom->findnodes('//Fund/@FUND_NAME')->[0]->to_literal(); |
| 83 |
|
| 84 |
$info{$symbol, 'success'} = 1; |
| 85 |
$info{$symbol, 'method'} = 'MorningstarJP'; |
| 86 |
$info{$symbol, 'currency'} = 'JPY'; |
| 87 |
$info{$symbol, 'symbol'} = $symbol; |
| 88 |
$info{$symbol, 'nav'} = $nav; |
| 89 |
$info{$symbol, 'isin'} = $isin; |
| 90 |
$info{$symbol, 'name'} = $name; |
| 91 |
|
| 92 |
$quoter->store_date(\%info, $symbol, {isodate => $date}); |
| 93 |
}; |
| 94 |
|
| 95 |
if ($@) { |
| 96 |
my $error = "Search failed: $@"; |
| 97 |
$info{$symbol, 'success'} = 0; |
| 98 |
$info{$symbol, 'errormsg'} = trim($error); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
### info : %info |
| 103 |
|
| 104 |
return wantarray() ? %info : \%info; |
| 105 |
} |
| 106 |
|
| 107 |
1; |
| 108 |
|
| 109 |
__END__ |
| 110 |
|
| 111 |
=head1 NAME |
| 112 |
|
| 113 |
Finance::Quote::MorninstarJP - Obtain quotes from from Morningstar (Japan) |
| 114 |
|
| 115 |
=head1 SYNOPSIS |
| 116 |
|
| 117 |
use Finance::Quote; |
| 118 |
|
| 119 |
$q = Finance::Quote->new; |
| 120 |
|
| 121 |
%stockinfo = $q->fetch("morningstarjp", "2009100101"); |
| 122 |
|
| 123 |
=head1 DESCRIPTION |
| 124 |
|
| 125 |
This module obtains information from Morningstar (Japan), |
| 126 |
L<http://www.wealthadvisor.co.jp/>. |
| 127 |
|
| 128 |
Information returned by this module is governed by Morningstar |
| 129 |
(Japan)'s terms and conditions. |
| 130 |
|
| 131 |
=head1 FUND SYMBOLS |
| 132 |
|
| 133 |
Use the numeric symbol shown in the URL on the "SnapShot" page |
| 134 |
of the security of interest. |
| 135 |
|
| 136 |
e.g. For L<https://www.wealthadvisor.co.jp/snapshot/2009100101>, |
| 137 |
one would supply 2009100101 as the symbol argument on the fetch API call. |
| 138 |
|
| 139 |
=head1 LABELS RETURNED |
| 140 |
|
| 141 |
The following labels may be returned by Finance::Quote::MorningstarJP: |
| 142 |
nav, isin, symbol, name, currency. |
| 143 |
|
| 144 |
=head1 Terms & Conditions |
| 145 |
|
| 146 |
Use of apl.wealthadvisor.jp is governed by any terms & conditions of that site. |
| 147 |
|
| 148 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 149 |
which explicitly carries a "No Warranty" clause. |
| 150 |
|
| 151 |
=head1 SEE ALSO |
| 152 |
|
| 153 |
Morningstar (Japan), L<http://www.wealthadvisor.co.jp/> |
| 154 |
|
| 155 |
=cut |