Debian Codegraph @

lib/Finance/Quote/ZA.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA
18
19 package Finance::Quote::ZA;
20
21 use strict;
22 use warnings;
23
24 use constant DEBUG => $ENV{DEBUG};
25 use if DEBUG, 'Smart::Comments';
26
27 use LWP::UserAgent;
28 use Web::Scraper;
29 use String::Util qw(trim);
30
31 our $VERSION = '1.71'; # VERSION
32
33 our $DISPLAY = 'ZA - Sharenet South Africa';
34 our @LABELS = qw/method source name symbol currency last price date isodate exchange/;
35 our $METHODHASH = {subroutine => \&sharenet,
36 display => $DISPLAY,
37 labels => \@LABELS};
38
39 sub methodinfo {
40 return (
41 za => $METHODHASH,
42 );
43 }
44
45 sub labels {
46 my %m = methodinfo();
47 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
48 }
49
50 sub methods {
51 my %m = methodinfo();
52 return map {$_ => $m{$_}{subroutine} } keys %m;
53 }
54
55 sub sharenet {
56 my $quoter = shift;
57 my @symbols = @_;
58 my $ua = $quoter->user_agent();
59 my %info;
60
61 foreach my $symbol (@_) {
62 eval {
63 my $url = "https://www.sharenet.co.za/jse/$symbol";
64 my $reply = $ua->get($url);
65
66 my $widget = scraper {
67 process 'h1.share-chart-title', 'name' => ['TEXT', sub{trim($_)}],
68 process 'h1.share-chart-title + h2', 'last' => ['TEXT', sub{$_ =~ /([0-9,.]+)/ ? $1 : '<unknown>';}],
69 process 'h1.share-chart-title + h2 + div b', 'day' => ['TEXT', sub{$_ =~ /(\w{3}\s+\d+\s+\w{3}),/ ? $1 : '<unknown>';}],
70 };
71
72 my $result = $widget->scrape($reply);
73
74 die "Failed to find $symbol" unless exists $result->{name};
75
76 # Sharenet reports in minor denomination. Change this to major denomination
77 my $price = $result->{last};
78 $price =~ s/,//;
79 $price = $price / 100;
80
81 ### RESULT : $result
82
83 $info{$symbol, 'success'} = 1;
84 $info{$symbol, 'currency'} = 'ZAR';
85 $info{$symbol, 'name'} = $result->{name};
86 $info{$symbol, 'price'} = $price;
87 $info{$symbol, 'last'} = $price;
88
89 # Some applications would like to see the symbol in the returned data
90 $info{$symbol, 'symbol'} = $symbol;
91 $info{$symbol, 'source'} = 'sharenet.co.za';
92 $info{$symbol, 'exchange'} = 'JSE';
93 $info{$symbol, 'method'} = 'za';
94
95 if ($result->{day} =~ /(\d+)\s+(\w{3})/) {
96 $quoter->store_date(\%info, $symbol, {day => $1, month => $2});
97 }
98 };
99
100 if ($@) {
101 my $error = "Search failed: $@";
102 $info{$symbol, 'success'} = 0;
103 $info{$symbol, 'errormsg'} = trim($error);
104 }
105 }
106
107 ### info : %info
108
109 return wantarray() ? %info : \%info;
110 }
111
112 1;
113
114 =head1 NAME
115
116 Finance::Quote::ZA - Obtain South African stock and prices from
117 https://www.sharenet.co.za
118
119 =head1 SYNOPSIS
120
121 use Finance::Quote;
122
123 $q = Finance::Quote->new;
124 %info = Finance::Quote->fetch('za', 'AGL');
125
126 =head1 DESCRIPTION
127
128 This module obtains information about South African Stocks from
129 www.sharenet.co.za.
130
131 This module is loaded by default on a Finance::Quote object. It's also possible
132 to load it explicitly by placing 'za' in the argument list to
133 Finance::Quote->new().
134
135 =head1 LABELS RETURNED
136
137 The following labels will be returned: success currency name price date isodate symbol last source exchange.
138
139 =head1 Terms & Conditions
140
141 Use of sharenet.co.za is governed by any terms & conditions of that site.
142
143 Finance::Quote is released under the GNU General Public License, version 2,
144 which explicitly carries a "No Warranty" clause.
145
146 =cut
147