Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Version 0.3
 - Fixed a problem with stock names containing spaces.
 - Improved error handling.
 - Added a test case.

Signed-off-by: LiosK <contact@mail.liosk.net>
  • Loading branch information
LiosK committed Oct 24, 2010
1 parent d314e0a commit 298b639
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/Finance/Quote/YahooJapan.pm
Expand Up @@ -13,7 +13,7 @@ use warnings;
use utf8;
use HTTP::Request::Common;

our $VERSION = '0.2';
our $VERSION = '0.3';

our $YAHOO_JAPAN_URL = 'http://quote.yahoo.co.jp/q';

Expand Down Expand Up @@ -63,18 +63,18 @@ sub _scrape($;@) {
my @table = grep /^<td/, split /\x0D?\x0A/, $content;

foreach my $row (@table) {
$row =~ s/&nbsp;|<[^>]+?>/ /g; # Stripping tags and NBSPs.
my (undef, $sym, $name, $date, $time, $price) = split /\s+/, $row;
my @cells = split /(?:&nbsp;|<[^>]+?>)+/, $row;
my (undef, $sym, $name, $date, $time, $price) = @cells;

# Formats data.
$price =~ tr/0-9//cd;
$date = _parse_date($date);
$time = _parse_time($time);
$price =~ tr/0-9//cd if (defined $price);
$date = _parse_date($date) if (defined $date);
$time = _parse_time($time) if (defined $time);

# Validates data.
my $success = 1;
$success = 0 if ($price eq '');
$success = 0 if ($date eq $_ERROR_DATE);
$success = 0 if (!defined $price || $price eq '');
$success = 0 if (!defined $date || $date eq $_ERROR_DATE);

$info{$sym, 'success'} = $success;
$info{$sym, 'currency'} = 'JPY';
Expand All @@ -83,6 +83,7 @@ sub _scrape($;@) {
$info{$sym, 'date'} = $date;
$info{$sym, 'time'} = $time;
$info{$sym, 'price'} = $price;
$info{$sym, 'errormsg'} = $success ? '' : $row;
}

return %info;
Expand Down
38 changes: 38 additions & 0 deletions test/fetch_quotes.pl
@@ -0,0 +1,38 @@
#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use Finance::Quote;

# list of target securities
my @symbols = qw/30_stocks
2914 3382 4063 4502 4503 5401 6301 6502 6752 6758
7201 7203 7267 7751 7974 8031 8058 8306 8316 8411
8604 8766 8802 9020 9432 9433 9437 9501 9503 9984
60_funds
02316097 42311066 32312965 01313068 02315097 45311065
01311969 0131100B 17313073 0931106C 50311083 50314087
50315087 01317095 01312095 01318088 0131B091 01314091
01312099 01316064 35311074 0431206B 49311059 0331306B
47311076 09312075 03313073 4431307B 0431105B 79311059
81311054 35312066 01318887 1231199B 01319887 3231199B
0631299C 7531103B 75311057 06311093 58311069 4731207C
45318007 17311093 08311052 08311046 2231E072 0431307B
22311983 29311069 49318019 2931401B 79313009 22317019
0331298C 7831101C 0231802C 6431406A 0331302B 01317079
50_ETFs
1305 1306 1308 1310 1311 1313 1314 1316 1317 1318
1319 1322 1325 1326 1327 1329 1330 1343 1344 1345
1347 1348 1349 1540 1541 1542 1543 1544 1610 1612
1613 1615 1617 1618 1619 1620 1621 1622 1623 1624
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
/;

# fetch and print quotes
my $q = Finance::Quote->new('-defaults', 'YahooJapan')->yahoo_japan(@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 298b639

Please sign in to comment.