github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

LiosK / Finance--Quote--YahooJapan

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 2
  • Source
  • Commits
  • Network (2)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A Perl module that enables GnuCash to get quotes of Japanese stocks and funds from Yahoo! JAPAN. — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Deleted an use statement because Encode.pm had not been used any longer. 
LiosK (author)
Wed Aug 05 05:47:22 -0700 2009
commit  a5eb068f7653069fb2b717db66d8fcd77ae7f443
tree    9f984282ba636f68369ae3b1152a2272e76390a1
parent  d28cf83202312074cfc88ee9735fe9b22db97107
Finance--Quote--YahooJapan / lib / Finance / Quote / YahooJapan.pm lib/Finance/Quote/YahooJapan.pm
100644 98 lines (77 sloc) 2.758 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl -w
 
# Author: LiosK <contact@mail.liosk.net>
# License: The GNU General Public License
#
# Information obtained by this module may be covered by Yahoo's terms
# and conditions. See http://quote.yahoo.co.jp/ for more details.
 
package Finance::Quote::YahooJapan;
 
use strict;
use warnings;
use utf8;
use HTTP::Request::Common;
 
our $VERSION = '0.1';
 
our $YAHOO_JAPAN_URL = 'http://quote.yahoo.co.jp/q';
 
sub methods {
    return (yahoo_japan => \&yahoo_japan);
}
 
sub labels {
    return (yahoo_japan => ['method', 'success', 'name', 'date', 'currency', 'price']);
}
 
sub yahoo_japan {
    my ($quoter, @symbols) = @_;
    return unless @symbols; # Nothing if no symbols.
 
    my %info = ();
    my $ua = $quoter->user_agent;
 
    # A request can contain less than 51 symbols.
    while (my @syms = splice @symbols, 0, 50) {
        # The URL searchs the symbol (s), name (n), last trade date (d1),
        # and last price (l1) of the stocks or funds specified by @syms.
        my $url = $YAHOO_JAPAN_URL . '?f=snd1l1&s=' . join '+', @syms;
        my $reply = $ua->request(GET $url);
        if ($reply->is_success) {
            # The way to extract quotes from a HTTP response is defined in
            # another subroutine because it is quite likely to be modified.
            %info = (%info, _scrape($reply->content, @syms));
        }
    }
 
    return %info if wantarray;
    return \%info;
}
 
# Scrapes quotes from a HTML text.
sub _scrape($;@) {
    my ($content, @symbols) = @_;
    my %info = ();
 
    # Extracts price list table.
    # XXX: Using an ugly, inflexible and unsophisticated algorithm.
    ($content) = $content =~ /<tr class=chartbg>(.+?)<\/table>/s;
    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, $price) = split /\s+/, $row;
 
        # Formats data.
        $price =~ s/,//g; # TODO
        $date = _determine_date($date);
 
        # Validates data.
        # TODO
 
        $info{$sym, 'success'} = 1;
        $info{$sym, 'currency'} = 'JPY';
        $info{$sym, 'method'} = 'yahoo_japan';
        $info{$sym, 'name'} = $name;
        $info{$sym, 'date'} = $date;
        $info{$sym, 'price'} = $price;
    }
 
    return %info;
}
 
# Determines the date of a quote.
sub _determine_date($;) {
    my ($date, @now) = (shift, localtime);
    if ($date =~ /(\d{1,2})\/(\d{1,2})/) {
        # MM/DD
        my ($yyyy, $mm, $dd) = ($now[5] + 1900, $1, $2);
        $yyyy-- if ($now[4] + 1 < $mm); # MM may point last December in January.
        return sprintf '%04d-%02d-%02d', $yyyy, $mm, $dd;
    } else {
        return '0000-00-00';
    }
}
 
1;
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server