| 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 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 5 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 5 | ▶ | use Web::Scraper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 5 | ▶ | use String::Util qw(trim); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||
| 32 | |||||||||||
| 33 | 5 | our $DISPLAY = 'ZA - Sharenet South Africa'; | |||||||||
| 34 | 5 | our @LABELS = qw/method source name symbol currency last price date isodate exchange/; | |||||||||
| 35 | 5 | our $METHODHASH = {subroutine => \&sharenet, | |||||||||
| 36 | display => $DISPLAY, | ||||||||||
| 37 | labels => \@LABELS}; | ||||||||||
| 38 | |||||||||||
| 39 | 10 | ▶ | sub methodinfo { | ||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 40 | return ( | ||||||||||
| 41 | 10 | za => $METHODHASH, | |||||||||
| 42 | ); | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 5 | ▶ | sub labels { | ||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 46 | 5 | my %m = methodinfo(); | |||||||||
| 47 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 5 | ▶ | sub methods { | ||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 5 | my %m = methodinfo(); | |||||||||
| 52 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub sharenet { | ||||||||
|
Subroutine
sharenet: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 56 | 0 | my $quoter = shift; | |||||||||
| 57 | 0 | my @symbols = @_; | |||||||||
| 58 | 0 | my $ua = $quoter->user_agent(); | |||||||||
| 59 | 0 | my %info; | |||||||||
| 60 | |||||||||||
| 61 | 0 | foreach my $symbol (@_) { | |||||||||
| 62 | 0 | eval { | |||||||||
| 63 | 0 | my $url = "https://www.sharenet.co.za/jse/$symbol"; | |||||||||
| 64 | 0 | my $reply = $ua->get($url); | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | my $widget = scraper { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | 0 | ▶ | process 'h1.share-chart-title', 'name' => ['TEXT', sub{trim($_)}], | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 68 | 0 | ▶ | process 'h1.share-chart-title + h2', 'last' => ['TEXT', sub{$_ =~ /([0-9,.]+)/ ? $1 : '<unknown>';}], | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | process 'h1.share-chart-title + h2 + div b', 'day' => ['TEXT', sub{$_ =~ /(\w{3}\s+\d+\s+\w{3}),/ ? $1 : '<unknown>';}], | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 70 | 0 | }; | |||||||||
| 71 | |||||||||||
| 72 | 0 | my $result = $widget->scrape($reply); | |||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | die "Failed to find $symbol" unless exists $result->{name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | # Sharenet reports in minor denomination. Change this to major denomination | ||||||||||
| 77 | 0 | my $price = $result->{last}; | |||||||||
| 78 | 0 | $price =~ s/,//; | |||||||||
| 79 | 0 | $price = $price / 100; | |||||||||
| 80 | |||||||||||
| 81 | ### RESULT : $result | ||||||||||
| 82 | |||||||||||
| 83 | 0 | $info{$symbol, 'success'} = 1; | |||||||||
| 84 | 0 | $info{$symbol, 'currency'} = 'ZAR'; | |||||||||
| 85 | 0 | $info{$symbol, 'name'} = $result->{name}; | |||||||||
| 86 | 0 | $info{$symbol, 'price'} = $price; | |||||||||
| 87 | 0 | $info{$symbol, 'last'} = $price; | |||||||||
| 88 | |||||||||||
| 89 | # Some applications would like to see the symbol in the returned data | ||||||||||
| 90 | 0 | $info{$symbol, 'symbol'} = $symbol; | |||||||||
| 91 | 0 | $info{$symbol, 'source'} = 'sharenet.co.za'; | |||||||||
| 92 | 0 | $info{$symbol, 'exchange'} = 'JSE'; | |||||||||
| 93 | 0 | $info{$symbol, 'method'} = 'za'; | |||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | if ($result->{day} =~ /(\d+)\s+(\w{3})/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $quoter->store_date(\%info, $symbol, {day => $1, month => $2}); | |||||||||
| 97 | } | ||||||||||
| 98 | }; | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | my $error = "Search failed: $@"; | |||||||||
| 102 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 103 | 0 | $info{$symbol, 'errormsg'} = trim($error); | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | ### info : %info | ||||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 5 | 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 | |||||||||||