File Coverage

blib/lib/Finance/Quote/ZA.pm
Criterion Covered Total %
statement 23 56 41.0
branch 0 12 0.0
condition n/a
subroutine 9 11 81.8
pod 0 3 0.0
total 32 82 39.0


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