Debian Codegraph @

lib/Finance/Quote/TesouroDireto.pm

raw
1 #!/usr/bin/perl -w
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA
17 #
18 #
19 # $Id: $
20
21 package Finance::Quote::TesouroDireto;
22 require 5.10.1;
23
24 use strict;
25 use warnings;
26
27 use constant DEBUG => $ENV{DEBUG};
28 use if DEBUG, 'Smart::Comments';
29
30 use LWP::UserAgent;
31 use HTTP::Request::Common;
32 use Time::Piece;
33
34 our $VERSION = '1.71'; # VERSION
35
36 our $DISPLAY = 'Brazilian Govt Bonds, tesouro_direto';
37 our $FEATURES = {};
38 our @LABELS = qw/exchange date isodate symbol name price last method currency/;
39 our $METHODHASH = {subroutine => \&tesouro,
40 display => $DISPLAY,
41 labels => \@LABELS,
42 features => $FEATURES};
43
44 sub methodinfo {
45 return (
46 tesouro_direto => $METHODHASH,
47 );
48 }
49
50 sub methods {
51 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
52 }
53
54 sub labels {
55 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
56 }
57
58 # Sanitizes input from tesouro's csv (x.xxx,xx) to US values (xxxx.xx)
59 sub convert_price {
60 $_ = shift;
61
62 # Replaces ',' with '.' and removes the other "extra" characters
63 tr/,. /./d ;
64
65 return $_;
66 }
67
68 # Returns the bond name (concatenation of bond type and year).
69 # Some "special" bonds are paid in installments; the name of these
70 # bonds uses the year of the first installment, however the due date
71 # from the csv refers to the last one.
72 sub get_bond_name {
73 my ($name, $date) = @_;
74
75 my $year = substr($date, 6, 4);
76
77 # Apply the conditional logic based on the value of $name.
78 if ($name eq 'Tesouro Renda+ Aposentadoria Extra') {
79 return "$name " . ($year - 19);
80 } elsif ($name eq 'Tesouro Educa+') {
81 return "$name " . ($year - 4);
82 } else {
83 # Otherwise, return a string of the name and the original year.
84 return "$name $year";
85 }
86 }
87
88 sub tesouro
89 {
90 my $quoter = shift;
91 my @funds = @_;
92 return unless @funds;
93
94 my $ua = $quoter->user_agent;
95
96 my (%fundsymbol, %fundhash, @q, %info);
97
98 # create hash of all funds requested
99 foreach my $fund (@funds)
100 {
101 $fundhash{$fund} = 0;
102 }
103
104 my $url = "https://www.tesourotransparente.gov.br/ckan/dataset/df56aa42-484a-4a59-8184-7676580c81e3/resource/796d2059-14e9-44e3-80c9-2d9e30b405c1/download/precotaxatesourodireto.csv";
105 my $response = $ua->request(GET $url);
106
107 if ($response->is_success) {
108
109 # process csv data
110 foreach (split('\015?\012',$response->content)) {
111
112 @q = $quoter->parse_csv_semicolon($_) or next;
113 next unless (defined $q[0]);
114
115 my $bond_name = get_bond_name($q[0], $q[1]);
116
117 if (exists $fundhash{$bond_name})
118 {
119 my $t = Time::Piece->strptime($q[2], "%d/%m/%Y");
120 next unless ($t->epoch > $fundhash{$bond_name});
121
122 $fundhash{$bond_name} = $t->epoch;
123
124 $info{$bond_name, "exchange"} = "Tesouro Direto";
125 $info{$bond_name, "name"} = $bond_name;
126 $info{$bond_name, "symbol"} = $bond_name;
127 $info{$bond_name, "price"} = convert_price($q[6]);
128 $info{$bond_name, "last"} = convert_price($q[6]);
129 $quoter->store_date(\%info, $bond_name, {eurodate => $q[2]});
130 $info{$bond_name, "method"} = "tesouro_direto";
131 $info{$bond_name, "currency"} = "BRL";
132 $info{$bond_name, "success"} = 1;
133 }
134 }
135
136 # check to make sure a value was returned for every fund requested
137 foreach my $fund (keys %fundhash)
138 {
139 if ($fundhash{$fund} == 0)
140 {
141 $info{$fund, "success"} = 0;
142 $info{$fund, "errormsg"} = "No data returned";
143 }
144 }
145 }
146 else
147 {
148 foreach my $fund (@funds)
149 {
150 $info{$fund, "success"} = 0;
151 $info{$fund, "errormsg"} = "HTTP error";
152 }
153 }
154
155 ### result: %info
156
157 return wantarray() ? %info : \%info;
158 }
159
160
161 1;
162
163 =head1 NAME
164
165 Finance::Quote::TesouroDireto - Obtain quotes for Brazilian government bounds
166
167 =head1 SYNOPSIS
168
169 use Finance::Quote;
170 $q = Finance::Quote->new;
171
172 %stockinfo = $q->fetch("tesouro_direto", "Tesouro IPCA+ 2045");
173
174 =head1 DESCRIPTION
175
176 This module obtains quotes for Brazilian government bounds, obtained from
177 the dataset released by Secretaria do Tesouro Nacional on Tesouro
178 Transparente under the Open Database License (ODbL):
179
180 https://www.tesourotransparente.gov.br/ckan/dataset/taxas-dos-titulos-ofertados-pelo-tesouro-direto
181
182 =head1 LABELS RETURNED
183
184 The following labels may be returned by Finance::Quote::TesouroDireto:
185 exchange, name, symbol, date, price, last, method, currency.
186
187 =head1 SEE ALSO
188
189 =cut