| 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::CSE; | ||||||||||
| 19 | |||||||||||
| 20 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 5 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 5 | ▶ | use JSON qw( decode_json ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 5 | ▶ | use String::Util qw(trim); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||
| 31 | |||||||||||
| 32 | 5 | our $DISPLAY = 'CSE - Colombo Stock Exchange'; | |||||||||
| 33 | 5 | our @LABELS = qw/isin close last high low cap change p_change name symbol currency method symbol date isodate/; | |||||||||
| 34 | 5 | our $METHODHASH = {subroutine => \&cse, | |||||||||
| 35 | display => \$DISPLAY, | ||||||||||
| 36 | labels => \@LABELS}; | ||||||||||
| 37 | |||||||||||
| 38 | 10 | ▶ | sub methodinfo { | ||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | return ( | ||||||||||
| 40 | 10 | cse => $METHODHASH, | |||||||||
| 41 | ); | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 5 | ▶ | sub labels { | ||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 45 | 5 | my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 5 | ▶ | sub methods { | ||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | 5 | my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub cse { | ||||||||
|
Subroutine
cse: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 54 | 0 | my $quoter = shift; | |||||||||
| 55 | 0 | my @symbols = @_; | |||||||||
| 56 | 0 | my $ua = $quoter->user_agent(); | |||||||||
| 57 | 0 | my %info; | |||||||||
| 58 | |||||||||||
| 59 | 0 | foreach my $symbol (@_) { | |||||||||
| 60 | 0 | eval { | |||||||||
| 61 | 0 | my $url = 'https://www.cse.lk/api/companyInfoSummery'; | |||||||||
| 62 | 0 | my $form = { | |||||||||
| 63 | 'symbol' => $symbol, | ||||||||||
| 64 | 'MIME Type' => 'application/x-www-form-urlencoded; charset=UTF-8', | ||||||||||
| 65 | }; | ||||||||||
| 66 | 0 | my $reply = $ua->post($url, $form); | |||||||||
| 67 | 0 | my $search = JSON::decode_json $reply->content; | |||||||||
| 68 | |||||||||||
| 69 | ### Search : $url, $form, $reply->code | ||||||||||
| 70 | ### Search : $search | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | my $data = $search->{reqSymbolInfo} or die('query did not return expected data'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 0 | $info{$symbol, 'isin'} = $data->{isin}; | |||||||||
| 75 | 0 | $info{$symbol, 'close'} = $data->{closingPrice}; | |||||||||
| 76 | 0 | $info{$symbol, 'last'} = $data->{lastTradedPrice}; | |||||||||
| 77 | 0 | $info{$symbol, 'high'} = $data->{hiTrade}; | |||||||||
| 78 | 0 | $info{$symbol, 'low'} = $data->{lowTrade}; | |||||||||
| 79 | 0 | $info{$symbol, 'cap'} = $data->{marketCap}; | |||||||||
| 80 | 0 | $info{$symbol, 'change'} = $data->{change}; | |||||||||
| 81 | 0 | $info{$symbol, 'p_change'} = $data->{changePercentage}; | |||||||||
| 82 | 0 | $info{$symbol, 'name'} = $data->{name}; | |||||||||
| 83 | 0 | $info{$symbol, 'symbol'} = $data->{symbol}; | |||||||||
| 84 | 0 | $info{$symbol, 'currency'} = 'LKR'; | |||||||||
| 85 | 0 | $info{$symbol, 'method'} = 'cse'; | |||||||||
| 86 | 0 | $quoter->store_date(\%info, $symbol, {today => 1}); | |||||||||
| 87 | 0 | $info{$symbol, 'success'} = 1; | |||||||||
| 88 | }; | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | my $error = "CSE failed: $@"; | |||||||||
| 92 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 93 | 0 | $info{$symbol, 'errormsg'} = trim($error); | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 5 | 1; | |||||||||
| 101 | |||||||||||
| 102 | =head1 NAME | ||||||||||
| 103 | |||||||||||
| 104 | Finance::Quote::CSE - Obtain quotes from Colombo Stock Exchange in Sri Lanka | ||||||||||
| 105 | |||||||||||
| 106 | =head1 SYNOPSIS | ||||||||||
| 107 | |||||||||||
| 108 | use Finance::Quote; | ||||||||||
| 109 | |||||||||||
| 110 | $q = Finance::Quote->new; | ||||||||||
| 111 | |||||||||||
| 112 | %info = Finance::Quote->fetch('cse', 'YORK.N0000'); | ||||||||||
| 113 | |||||||||||
| 114 | =head1 DESCRIPTION | ||||||||||
| 115 | |||||||||||
| 116 | This module fetches information from the Colombo Stock Exchange (CSE) | ||||||||||
| 117 | in Sri Lanka http://www.cse.lk. | ||||||||||
| 118 | |||||||||||
| 119 | This module is loaded by default on a Finance::Quote object. It's also possible | ||||||||||
| 120 | to load it explicitly by placing 'CSE' in the argument list to | ||||||||||
| 121 | Finance::Quote->new(). | ||||||||||
| 122 | |||||||||||
| 123 | =head1 LABELS RETURNED | ||||||||||
| 124 | |||||||||||
| 125 | The following labels may be returned by Finance::Quote::CSE : | ||||||||||
| 126 | isin close last high low cap change p_change name symbol currency method symbol date isodate | ||||||||||
| 127 | |||||||||||
| 128 | =head1 TERMS & CONDITIONS | ||||||||||
| 129 | |||||||||||
| 130 | Use of www.cse.lk is governed by any terms & conditions of that site. | ||||||||||
| 131 | |||||||||||
| 132 | Finance::Quote is released under the GNU General Public License, version 2, | ||||||||||
| 133 | which explicitly carries a "No Warranty" clause. | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||