| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 ic noai showmode showmatch: |
| 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 2 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, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301, USA |
| 18 |
|
| 19 |
# Copyright (C) 2023, Bruce Schuck <[email protected]> |
| 20 |
|
| 21 |
# Changes: |
| 22 |
# 2024-04-13 - Changed to use |
| 23 |
# https://query1.finance.yahoo.com/v8/finance/chart/ |
| 24 |
|
| 25 |
package Finance::Quote::CurrencyRates::YahooJSON; |
| 26 |
|
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
|
| 30 |
use constant DEBUG => $ENV{DEBUG}; |
| 31 |
use if DEBUG, 'Smart::Comments'; |
| 32 |
|
| 33 |
use JSON; |
| 34 |
|
| 35 |
our $VERSION = '1.71'; # VERSION |
| 36 |
|
| 37 |
my $YIND_URL_HEAD = 'https://query1.finance.yahoo.com/v8/finance/chart/'; |
| 38 |
my $YIND_URL_TAIL = '?metrics=high&interval=1d&range=1d'; |
| 39 |
|
| 40 |
sub new |
| 41 |
{ |
| 42 |
my $self = shift; |
| 43 |
my $class = ref($self) || $self; |
| 44 |
|
| 45 |
my $this = {}; |
| 46 |
bless $this, $class; |
| 47 |
|
| 48 |
my $args = shift; |
| 49 |
|
| 50 |
### YahooJSON->new args : $args |
| 51 |
|
| 52 |
return $this; |
| 53 |
} |
| 54 |
|
| 55 |
sub multipliers |
| 56 |
{ |
| 57 |
my ($this, $ua, $from, $to) = @_; |
| 58 |
|
| 59 |
my $json_data; |
| 60 |
my $rate; |
| 61 |
my $reply = $ua->get($YIND_URL_HEAD |
| 62 |
. ${from} |
| 63 |
. ${to} |
| 64 |
. '%3DX' |
| 65 |
. $YIND_URL_TAIL); |
| 66 |
|
| 67 |
### HTTP Status: $reply->code |
| 68 |
return unless ($reply->code == 200); |
| 69 |
|
| 70 |
my $body = $reply->content; |
| 71 |
|
| 72 |
$json_data = JSON::decode_json $body; |
| 73 |
|
| 74 |
### JSON: $json_data |
| 75 |
|
| 76 |
if ( !$json_data || !$json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'} ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$rate = |
| 81 |
$json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'}; |
| 82 |
|
| 83 |
### Rate from JSON: $rate |
| 84 |
|
| 85 |
return unless $rate + 0; |
| 86 |
|
| 87 |
# For small rates, request the inverse |
| 88 |
if ($rate < 0.001) { |
| 89 |
### Rate is too small, requesting inverse : $rate |
| 90 |
my ($a, $b) = $this->multipliers($ua, $to, $from); |
| 91 |
return ($b, $a); |
| 92 |
} |
| 93 |
|
| 94 |
return (1.0, $rate); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
1; |
| 99 |
|
| 100 |
=head1 NAME |
| 101 |
|
| 102 |
Finance::Quote::CurrencyRates::YahooJSON - Obtain currency rates from |
| 103 |
https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price |
| 104 |
|
| 105 |
=head1 SYNOPSIS |
| 106 |
|
| 107 |
use Finance::Quote; |
| 108 |
|
| 109 |
$q = Finance::Quote->new(currency_rates => {order => ['YahooJSON']}); |
| 110 |
|
| 111 |
$value = $q->currency('18.99 EUR', 'USD'); |
| 112 |
|
| 113 |
=head1 DESCRIPTION |
| 114 |
|
| 115 |
This module fetches currency rates from |
| 116 |
https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price |
| 117 |
provides data to Finance::Quote to convert the first argument to the equivalent |
| 118 |
value in the currency indicated by the second argument. |
| 119 |
|
| 120 |
This module is not the default currency conversion module for a Finance::Quote |
| 121 |
object. |
| 122 |
|
| 123 |
=head1 Terms & Conditions |
| 124 |
|
| 125 |
Use of https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price is |
| 126 |
governed by any terms & conditions of that site. |
| 127 |
|
| 128 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 129 |
which explicitly carries a "No Warranty" clause. |
| 130 |
|
| 131 |
=cut |