Debian Codegraph @

lib/Finance/Quote/CSE.pm

raw
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 use strict;
21 use warnings;
22
23 use constant DEBUG => $ENV{DEBUG};
24 use if DEBUG, 'Smart::Comments';
25
26 use LWP::UserAgent;
27 use JSON qw( decode_json );
28 use String::Util qw(trim);
29
30 our $VERSION = '1.71'; # VERSION
31
32 our $DISPLAY = 'CSE - Colombo Stock Exchange';
33 our @LABELS = qw/isin close last high low cap change p_change name symbol currency method symbol date isodate/;
34 our $METHODHASH = {subroutine => \&cse,
35 display => \$DISPLAY,
36 labels => \@LABELS};
37
38 sub methodinfo {
39 return (
40 cse => $METHODHASH,
41 );
42 }
43
44 sub labels {
45 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
46 }
47
48 sub methods {
49 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
50 }
51
52
53 sub cse {
54 my $quoter = shift;
55 my @symbols = @_;
56 my $ua = $quoter->user_agent();
57 my %info;
58
59 foreach my $symbol (@_) {
60 eval {
61 my $url = 'https://www.cse.lk/api/companyInfoSummery';
62 my $form = {
63 'symbol' => $symbol,
64 'MIME Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
65 };
66 my $reply = $ua->post($url, $form);
67 my $search = JSON::decode_json $reply->content;
68
69 ### Search : $url, $form, $reply->code
70 ### Search : $search
71
72 my $data = $search->{reqSymbolInfo} or die('query did not return expected data');
73
74 $info{$symbol, 'isin'} = $data->{isin};
75 $info{$symbol, 'close'} = $data->{closingPrice};
76 $info{$symbol, 'last'} = $data->{lastTradedPrice};
77 $info{$symbol, 'high'} = $data->{hiTrade};
78 $info{$symbol, 'low'} = $data->{lowTrade};
79 $info{$symbol, 'cap'} = $data->{marketCap};
80 $info{$symbol, 'change'} = $data->{change};
81 $info{$symbol, 'p_change'} = $data->{changePercentage};
82 $info{$symbol, 'name'} = $data->{name};
83 $info{$symbol, 'symbol'} = $data->{symbol};
84 $info{$symbol, 'currency'} = 'LKR';
85 $info{$symbol, 'method'} = 'cse';
86 $quoter->store_date(\%info, $symbol, {today => 1});
87 $info{$symbol, 'success'} = 1;
88 };
89
90 if ($@) {
91 my $error = "CSE failed: $@";
92 $info{$symbol, 'success'} = 0;
93 $info{$symbol, 'errormsg'} = trim($error);
94 }
95 }
96
97 return wantarray() ? %info : \%info;
98 }
99
100 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