Debian Codegraph @

lib/Finance/Quote/GoldMoney.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3 #
4 # Copyright (C) 1998, Dj Padzensky <[email protected]>
5 # Copyright (C) 1998, 1999 Linas Vepstas <[email protected]>
6 # Copyright (C) 2000, Yannick LE NY <[email protected]>
7 # Copyright (C) 2000, Paul Fenwick <[email protected]>
8 # Copyright (C) 2000, Brent Neal <[email protected]>
9 # Copyright (C) 2000, Volker Stuerzl <[email protected]>
10 # Copyright (C) 2006, Klaus Dahlke <[email protected]>
11 # Copyright (C) 2008, Stephan Ebelt <[email protected]>
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301, USA
27 #
28 # $Id: $
29
30 package Finance::Quote::GoldMoney;
31 require 5.005;
32
33 use HTTP::Request::Common;
34 use JSON;
35
36 use strict;
37 use warnings;
38
39 our $VERSION = '1.71'; # VERSION
40
41 our $DISPLAY = 'GoldMoney - Gold, Silver, Platinum ';
42 our @LABELS = qw/exchange name date isodate price method/;
43 our $METHODHASH = {subroutine => \&goldmoney,
44 display => $DISPLAY,
45 labels => \@LABELS};
46
47 sub methodinfo {
48 return (
49 goldmoney => $METHODHASH,
50 );
51 }
52
53 sub labels {
54 my %m = methodinfo();
55 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
56 }
57
58 sub methods {
59 my %m = methodinfo();
60 return map {$_ => $m{$_}{subroutine} } keys %m;
61 }
62
63 # goldmoney($quoter, @symbols)
64 #
65 # - get 'gold' and 'silver' spot rates from goldmoney.com
66 # - error out properly (that is: ignore) all other symbols
67 #
68 sub goldmoney {
69 my $quoter = shift;
70 my @symbols = @_;
71 return unless @symbols;
72
73 my $ua = $quoter->user_agent;
74
75 # Set the ua to be blank. GoldMOney are using CloudFlare who block
76 # the default useragent.
77 $ua->agent('');
78 my ( %symbolhash, @q, %info );
79 my ( $html_string, $te, $table_gold, $table_silver,
80 $table_platinum, $gold_gg, $gold_oz, $silver_oz,
81 $platinum_oz, $platinum_pg, $currency
82 );
83
84 my $_want_gold = 0;
85 my $_want_silver = 0;
86 my $_want_platinum = 0;
87
88 # - feed all requested symbols into %info (to be returned later)
89 # - set error state to false by default
90 # - see if a gold or silver rate is requested
91 foreach my $s (@symbols) {
92 $info{ $s, 'success' } = 0;
93 $info{ $s, 'exchange' } = 'goldmoney.com';
94 $info{ $s, 'method' } = 'goldmoney';
95 $info{ $s, 'symbol' } = $s;
96
97 if ( $s eq 'gold' ) {
98 $_want_gold = 1;
99 }
100 elsif ( $s eq 'silver' ) {
101 $_want_silver = 1;
102 }
103 elsif ( $s eq 'platinum' ) {
104 $_want_platinum = 1;
105 }
106 else {
107 $info{ $s, 'errormsg' } =
108 "No data returned (note: this module only works for 'gold' and 'silver')";
109 }
110 }
111
112 # get the JSON of the prices. Currently getting sell price,
113 if ( $_want_gold or $_want_silver or $_want_platinum ) {
114
115 my $currency = $quoter->{"currency"} || 'EUR';
116 my $GOLDMONEY_URL =
117 "http://www.goldmoney.com/metal/prices/currentSpotPrices?currency="
118 . lc($currency)
119 . "&units=grams&price=bid";
120 my $response = $ua->request( GET $GOLDMONEY_URL);
121
122 if ( $response->is_success ) {
123 $html_string = $response->content;
124
125 my $json = from_json($html_string);
126
127 $table_gold = $json->{spotPrices}[0];
128 $table_silver = $json->{spotPrices}[1];
129 $table_platinum = $json->{spotPrices}[2];
130 }
131 else {
132 # retrieval error - flag an error and return right away
133 foreach my $s (@symbols) {
134 %info = _goldmoney_error( @symbols,
135 'HTTP error: ' . $response->status_line );
136 return wantarray() ? %info : \%info;
137 }
138 return wantarray() ? %info : \%info;
139 }
140
141 # get gold rate
142 #
143 if ($_want_gold) {
144
145 # assemble final dataset
146 # - take "now" as date/time as the site is always current and does
147 # not provide this explicitly - so there is a time-slip
148 $quoter->store_date( \%info, 'gold',
149 { isodate => _goldmoney_time('isodate') } );
150
151 $info{ 'gold', 'time' } = _goldmoney_time('time');
152 $info{ 'gold', 'name' } = 'Gold Spot';
153 $info{ 'gold', 'last' } = $table_gold->{spotPrice};
154 $info{ 'gold', 'price' } = $table_gold->{spotPrice};
155 $info{ 'gold', 'currency' } = $currency;
156 $info{ 'gold', 'success' } = 1;
157 }
158
159 # get silver rate
160 #
161 if ($_want_silver) {
162
163 $quoter->store_date( \%info, 'silver',
164 { isodate => _goldmoney_time('isodate') } );
165 $info{ 'silver', 'time' } = _goldmoney_time('time');
166 $info{ 'silver', 'name' } = 'Silver Spot';
167 $info{ 'silver', 'last' } = $table_silver->{spotPrice};
168 $info{ 'silver', 'price' } = $table_silver->{spotPrice};
169 $info{ 'silver', 'currency' } = $currency;
170 $info{ 'silver', 'success' } = 1;
171
172 }
173
174 # get platinum rate
175 #
176 if ($_want_platinum) {
177
178 # assemble final dataset
179 # - take "now" as date/time as the site is always current and does
180 # not provide this explicitly - so there is a time-slip
181 $quoter->store_date( \%info, 'platinum',
182 { isodate => _goldmoney_time('isodate') } );
183 $info{ 'platinum', 'time' } = _goldmoney_time('time');
184 $info{ 'platinum', 'name' } = 'Platinum Spot';
185 $info{ 'platinum', 'last' } = $table_platinum->{spotPrice};
186 $info{ 'platinum', 'price' } = $table_platinum->{spotPrice};
187 $info{ 'platinum', 'currency' } = $currency;
188 $info{ 'platinum', 'success' } = 1;
189
190 }
191 }
192
193 return wantarray() ? %info : \%info;
194 }
195
196 # - populate %info with errormsg and status code set for all requested symbols
197 # - return a hash ready to pass back to fetch()
198 sub _goldmoney_error {
199 my @symbols = shift;
200 my $msg = shift;
201 my %info;
202
203 foreach my $s (@symbols) {
204 $info{ $s, "success" } = 0;
205 $info{ $s, "errormsg" } = $msg;
206 }
207
208 return (%info);
209 }
210
211 # - return current 'isodate' and 'time' string
212 sub _goldmoney_time {
213 my $want = shift;
214 my @now = localtime();
215 my $str;
216
217 if ( $want eq 'isodate' ) {
218 $str = sprintf( '%4d-%02d-%02d', $now[5] + 1900, $now[4] + 1, $now[3] );
219 }
220 elsif ( $want eq 'time' ) {
221 $str = sprintf( '%02d:%02d:%02d', $now[2], $now[1], $now[0] );
222 }
223
224 return ($str);
225 }
226
227 1;
228
229 =head1 NAME
230
231 Finance::Quote::GoldMoney - obtain spot rates from GoldMoney.
232
233 =head1 SYNOPSIS
234
235 use Finance::Quote;
236
237 $q = Finance::Quote->new;
238
239 %rates = $q->fetch('goldmoeny','gold', 'silver', 'platinum');
240
241 =head1 DESCRIPTION
242
243 This module obtains current spot rates for 'gold', 'silver' and
244 'platinum' from Goldmoney (http://www.goldmoney.com). All other
245 symbols are ignored.
246
247 Information returned by this module is governed by Net Transactions
248 Ltd.'s terms and conditions. This module is *not* affiliated with the
249 company in any way. Use at your own risk.
250
251 =head1 LABELS RETURNED
252
253 The following labels are returned by Finance::Quote::GoldMoney:
254
255 - exchange
256 - name
257 - date, time
258 - price (per gram),
259 - currency
260
261 =head1 SEE ALSO
262
263 GoldMoney (Net Transactions Ltd.), http://www.goldmoney.com/
264
265 =cut