Skip to content
Browse files

add files

  • Loading branch information...
0 parents commit 4d6eab472bc7096545c89035d770c6273389efc6 @ohac committed Jun 27, 2011
Showing with 79 additions and 0 deletions.
  1. +64 −0 lib/Finance/Quote/MtGox.pm
  2. +15 −0 test/fetch_quotes.pl
64 lib/Finance/Quote/MtGox.pm
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -w
+
+package Finance::Quote::MtGox;
+
+use strict;
+use warnings;
+use utf8;
+use HTTP::Request::Common;
+use JSON qw/encode_json decode_json/;
+
+our $VERSION = '0.1';
+
+our $MT_GOX_URL = 'http://mtgox.com/code/data/ticker.php';
+
+our $_ERROR_DATE = '0000-00-00';
+
+sub methods {
+ return (mt_gox => \&mt_gox);
+}
+
+sub labels {
+ return (mt_gox => ['method', 'success', 'name', 'date', 'time', 'currency', 'price']);
+}
+
+sub mt_gox {
+ my ($quoter, @symbols) = @_;
+ return unless @symbols;
+
+ my %info = ();
+ my $ua = $quoter->user_agent;
+
+ my $url = $MT_GOX_URL;
+ my $reply = $ua->request(GET $url);
+
+ if ($reply->is_success) {
+ foreach my $sym (@symbols) {
+ %info = (%info, _scrape($reply->content, $sym));
+ }
+ }
+ return %info if wantarray;
+ return \%info;
+}
+
+sub _scrape($;$) {
+ my ($content, $sym) = @_;
+ my %info = ();
+ my @now = localtime;
+ my $date = sprintf '%04d-%02d-%02d', $now[5] + 1900, $now[4] + 1, $now[3];
+ my $time = sprintf '%02d:%02d:%02d', $now[2], $now[1], $now[0];
+ my $data = decode_json($content);
+ my $price = $data->{'ticker'}{'last'};
+ my $success = 1;
+ $info{$sym, 'success'} = $success;
+ $info{$sym, 'currency'} = 'USD';
+ $info{$sym, 'method'} = 'mt_gox';
+ $info{$sym, 'name'} = 'Mt.Gox';
+ $info{$sym, 'date'} = $date;
+ $info{$sym, 'time'} = $time;
+ $info{$sym, 'price'} = $price;
+ $info{$sym, 'errormsg'} = $success ? '' : $content;
+ return %info;
+}
+
+1;
15 test/fetch_quotes.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+
+use utf8;
+use strict;
+use warnings;
+use Finance::Quote;
+
+my @symbols = qw/foo/;
+
+my $q = Finance::Quote->new('-defaults', 'MtGox')->mt_gox(@symbols);
+
+my @fields = qw/success currency method name date time price errormsg/;
+for my $sym (@symbols) {
+ print join("\t", $sym, map { $q->{$sym, $_} // 'N/A' } @fields), "\n";
+}

0 comments on commit 4d6eab4

Please sign in to comment.
Something went wrong with that request. Please try again.