Quote/Stooq.pm

filter: Statementstmt 37.4% 46 / 123 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 4 totaltotal 29.6% 59 / 199 CC 22 SCAR 57.6
CC
22
Cov
27%
stooq
506.0
BEGIN
1.0
BEGIN
1.0
CRAP 318.4
« Quote/Union.pm ↑ Summary Quote/TwelveData.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3 #
4 # Copyright (C) 2023, Bruce Schuck <bschuck@asgard-systems.com>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA
20 #
21  
22 package Finance::Quote::Stooq;
23  
24 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use HTTP::CookieJar::LWP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 our $VERSION = '1.72_01'; # TRIAL VERSION
38  
39 5 our $DISPLAY = 'Stooq - Warsaw Stock Exchange';
40 5 our @LABELS = qw/symbol name open high low last bid ask date currency method/;
41 5 our $METHODHASH = {subroutine => \&stooq,
42 display => $DISPLAY,
43 labels => \@LABELS};
44  
45 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
46 return (
47 10 stooq => $METHODHASH,
48 europe => $METHODHASH,
49 poland => $METHODHASH,
50 );
51 }
52  
53 5 ▶sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; }
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
54  
55 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
56 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
57 }
58  
59 5 my $STOOQ_URL = 'https://stooq.com/q/?s=';
60  
61 5 my %currencies_by_link = (
62 '?i=21' => "EUR", # Europe (€)
63 '?i=23' => "GBP", # United Kingdom (£)
64 '?i=25' => "HKD", # Hong Kong (HK$)
65 '?i=30' => "HUF", # Hungary (Ft)
66 '?i=39' => "JPY", # Japan (¥)
67 '?i=60' => "PLN", # Poland (zł)
68 '?i=77' => "USD", # United States ($)
69 );
70  
71 5 my %currencies_by_symbol = (
72 '&pound;' => "GBP", # United Kingdom (£)
73 'p.' => "GBX", # United Kingdom (penny)
74 '&euro;' => "EUR", # Europe (€)
75 "z\x{142}" => "PLN", # Poland (zł)
76 '$' => "USD", # United States ($)
77 '&cent;' => "USX", # United States (¢)
78 'HK$' => "HKD", # Hong Kong (HK$)
79 '&yen;' => "JPY", # Japan (¥)
80 'Ft' => "HUF", # Hungary (Ft)
81 );
82  
83 ### [<now>] Currencies by Symbol: %currencies_by_symbol
84  
85 0 ▶sub stooq {
Subroutine stooq: not called CC=22 SCAR=62.3
Pod undocumented
86  
87 0 my $quoter = shift;
88 0 my @stocks = @_;
89 0 my (%info, $tree, $table, $pricetable, $url, $reply);
90  
91 0 my $cj = HTTP::Cookies->new();
92 # Cookies content gleaned from browser session
93 0 $cj->set_cookie(0, 'cookie_user', '%3F+++1dllg000G+1100d1300c3%7Clrq', '/', '.stooq.com');
94 0 $cj->set_cookie(0, 'cookie_uu', '260114000', '/', '.stooq.com');
95 0 $cj->set_cookie(0, 'FCCDCF', '%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B32%2C%22%5B%5C%22ab16ab99-60a0-4da1-a296-29c8a27efbf9%5C%22%2C%5B1767931790%2C786000000%5D%5D%22%5D%2C%5B13%2C%22%5B%5C%22DBABL~BVQqAAAAAg%5C%22%2C%5B%5B7%2C%5B1768425873%2C984063000%5D%5D%5D%5D%22%5D%5D%5D', '/', '.stooq.com');
96 0 $cj->set_cookie(0, 'FCNEC', '%5B%5B%22AKsRol_Zywhkz7ukryuiQZ7evZsxa0eSbPMEpqvZUwq6Kmf32IqN4xQFgJjIG_fDDxo4f5sLKIXXGsrdQayx3tROTwoyC8BZX4rNDu3fBfzCikX_gkuhCra5dJotNToqdQrxouYXlgE_k7KTmpnQn7AEOImeVQrz2g%3D%3D%22%5D%5D', '/', '.stooq.com');
97 0 $cj->set_cookie(0, 'PHPSESSID', '3vqtfg7shbf23iog9frtbqdn15', '/', 'stooq.com');
98 0 $cj->set_cookie(0, 'privacy', '1768598448', '/', 'stooq.com');
99 0 $cj->set_cookie(0, 'uid', 'usb132zim841xr4doi8j12uk4a', '/', 'stooq.com');
100  
101 0 my $ua = $quoter->user_agent();
102  
103 0 $ua->cookie_jar($cj);
104  
105 0 $ua->default_header('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7');
106 0 $ua->default_header('Accept-Encoding' => 'gzip, deflate, br, zstd');
107 0 $ua->default_header('Accept-Language' => 'en-US,en;q=0.9');
108 0 $ua->default_header('Host' => 'stooq.com');
109 0 $ua->default_header('Pragma' => 'no-cache');
110 0 $ua->default_header('Upgrade-Insecure-Requests' => '1');
111 0 $ua->default_header('User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36');
112  
113 0 foreach my $stock (@stocks) {
114  
115 0 $url = $STOOQ_URL . $stock;
116 0 $reply = $ua->request( GET $url );
117  
118 0 my $code = $reply->code;
119 0 my $desc = HTTP::Status::status_message($code);
120 0 my $headers = $reply->headers_as_string;
121 0 my $body = $reply->decoded_content;
122  
123 ### Headers: $headers
124 ### Body: $body
125  
126 0 my ($name, $bid, $ask, $last, $open, $high, $low, $date, $currency);
127 0 my ($te, $table);
128  
129 0 $info{ $stock, "symbol" } = $stock;
130  
131 0 ▶ if ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
132  
133 # Use HTML::TableExtract to parse HTML in $body
134  
135 # The table with the security name is the only table
136 # with bgcolor=e9e9e9 style=z-index:1
137 0 $te = HTML::TableExtract->new(
138 attribs => { bgcolor => 'e9e9e9', style => 'z-index:1' } );
139 0 ▶ if (($te->parse($body)) && ($table = $te->first_table_found)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te->parse($body) and $table = $te->first_table_found) 0 0 0
Condition0%0/3
$te->parse($body) and $table = $te->first_table_found
!ll&&!rl&&r
000
MC/DC0%0/2
$te->parse($body) and $table = $te->first_table_found
$te->parse($body) $table = $te->first_table_found
Truth table0%0/3
A and B
A= $te->parse($body)B= $table = $te->first_table_found
ABresult
0X0
100
111
140 ### NameTable Rows: $table->rows()
141 0 ($name) = $table->cell(0,1) =~ m|^.*?(\w.*)$|;
142 0 $te->eof;
143 }
144  
145 # The table with the price data is the only table with
146 # attribute id='t1'
147 0 $te = HTML::TableExtract->new( keep_html => 1,
148 attribs => { id => 't1' } );
149 0 ▶ if (($te->parse($body)) && ($table = $te->first_table_found)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te->parse($body) and $table = $te->first_table_found) { } 0 0 0
Condition0%0/3
$te->parse($body) and $table = $te->first_table_found
!ll&&!rl&&r
000
MC/DC0%0/2
$te->parse($body) and $table = $te->first_table_found
$te->parse($body) $table = $te->first_table_found
Truth table0%0/3
A and B
A= $te->parse($body)B= $table = $te->first_table_found
ABresult
0X0
100
111
150 0 (my $last) = $table->cell(0,0) =~ m|^.+>([\d\.]+)<|;
151  
152 # usually currency is embedded in an A tag
153 # curency default: td > b[> span_with_price] + "&nbsp;" + _a_linking_to_currency
154 # curency USD/HUF: td > b > _a_linking_to_currency + "&nbsp;" + span_with_price
155 # except for commodities there's no A tag:
156 # commodities: td > b[> span_with_price] + "&nbsp;_currency_without_link_"
157 ### [<now>] Cell 0,0: $table->cell(0,0)
158 0 (my $currlink) = $table->cell(0,0) =~ m|<a href=t/(\?i=\d+)>|;
159 0 ▶ if ( ($currlink) && ($currencies_by_link{$currlink}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currlink and $currencies_by_link{$currlink}) { } 0 0 0
Condition0%0/3
$currlink and $currencies_by_link{$currlink}
!ll&&!rl&&r
000
MC/DC0%0/2
$currlink and $currencies_by_link{$currlink}
$currlink $currencies_by_link{$currlink}
Truth table0%0/3
A and B
A= $currlinkB= $currencies_by_link{$currlink}
ABresult
0X0
100
111
160 0 $currency = $currencies_by_link{$currlink};
161 } else {
162 0 (my $currsymbol) = $table->cell(0,0)
163 =~ m#[\d\.]+</span></b>&nbsp;(.+)/(ozt|lb|t|gal|bbl|bu|mmBtu)#;
164 ### [<now>] CurrSymbol: $currsymbol
165 0 ▶ if ( ($currsymbol) && ($currencies_by_symbol{$currsymbol}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currsymbol and $currencies_by_symbol{$currsymbol}) 0 0 0
Condition0%0/3
$currsymbol and $currencies_by_symbol{$currsymbol}
!ll&&!rl&&r
000
MC/DC0%0/2
$currsymbol and $currencies_by_symbol{$currsymbol}
$currsymbol $currencies_by_symbol{$currsymbol}
Truth table0%0/3
A and B
A= $currsymbolB= $currencies_by_symbol{$currsymbol}
ABresult
0X0
100
111
166 0 $currency = $currencies_by_symbol{$currsymbol};
167 }
168 }
169  
170 ### [<now>] Currency: $currency
171
172 0 (my $date) = $table->cell(0,1) =~ m|Date.+>(\d{4}-\d{2}-\d{2})<|;
173 0 (my $high, my $low) = $table->cell(1,1)
174 =~ m|.+>([\d\.]+)<.+>([\d\.]+)<|;
175 0 (my $open) = $table->cell(3,0) =~ m|Open.+>([\d\.]+)<|;
176 0 (my $bid) = $table->cell(4,0) =~ m|Bid.+>([\d\.]+)<|;
177 0 (my $ask) = $table->cell(4,1) =~ m|Ask.+>([\d\.]+)<|;
178 # If last and date are defined, save values in hash
179 0 ▶ if ( ($last) && ($date) && ($currency) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and $date and $currency) { } 0 0 0
Condition0%0/6
$last and $date and $currency
!ll&&!rl&&r
000
$last and $date
!ll&&!rl&&r
000
MC/DC0%0/3
$last and $date and $currency
$last $date $currency
Truth table0%0/4
A and B and C
A= $lastB= $dateC= $currency
ABCresult
0XX0
10X0
1100
1111
180 0 $info{ $stock, 'success' } = 1;
181 0 $info{ $stock, 'method' } = 'stooq';
182 0 $info{ $stock, 'name' } = $name;
183 0 $info{ $stock, 'last' } = $last;
184 0 $info{ $stock, 'currency' } = $currency;
185 0 $info{ $stock, 'open' } = $open;
186 0 $info{ $stock, 'high' } = $high;
187 0 $info{ $stock, 'low' } = $low;
188 0 ▶ $info{ $stock, 'bid' } = $bid if ($bid);
Branch: 1 branch
BranchTrueFalseTotal
if $bid 0 0 0
189 0 ▶ $info{ $stock, 'ask' } = $ask if ($ask);
Branch: 1 branch
BranchTrueFalseTotal
if $ask 0 0 0
190 0 $quoter->store_date(\%info, $stock, { isodate => $date });
191 # Adjust/scale price data if currency is GBX (GBp) or USX (USc)
192 0 ▶ if ( ( $currency eq 'GBX' ) || ( $currency eq 'USX' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency eq "GBX" or $currency eq "USX") 0 0 0
Condition0%0/3
$currency eq "GBX" or $currency eq "USX"
l!l&&r!l&&!r
000
MC/DC0%0/2
$currency eq "GBX" or $currency eq "USX"
$currency eq "GBX" $currency eq "USX"
Truth table0%0/3
A or B
A= $currency eq "GBX"B= $currency eq "USX"
ABresult
1X1
011
000
193 0 foreach my $field ( $quoter->default_currency_fields ) {
194 0 ▶ next unless ( $info{ $stock, $field } );
Branch: 1 branch
BranchTrueFalseTotal
unless $info{join $;, $stock, $field} 0 0 0
195 $info{ $stock, $field } =
196 0 $quoter->scale_field( $info{ $stock, $field }, 0.01 );
197 }
198 0 ▶ if ( $info{ $stock, 'currency' } eq 'GBX' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{join $;, $stock, 'currency'} eq 'GBX') { } 0 0 0
199 0 $info{ $stock, 'currency' } = 'GBP';
200 } else {
201 0 $info{ $stock, 'currency' } = 'USD';
202 }
203 }
204 } else {
205 0 $info{ $stock, "success" } = 0;
206 0 $info{ $stock, "errormsg" } =
207 "Error retrieving quote for $stock. Could not parse HTML returned from $url.";
208 }
209 } else {
210 0 $te->eof;
211 0 $info{ $stock, "success" } = 0;
212 0 $info{ $stock, "errormsg" } =
213 "Error retrieving quote for $stock. Could not parse HTML returned from $url.";
214 }
215  
216 } else { # HTTP Request failed (code != 200)
217 0 $info{ $stock, "success" } = 0;
218 0 $info{ $stock, "errormsg" } =
219 "Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)";
220 }
221  
222 }
223  
224 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
225 0 return \%info;
226  
227 }
228  
229 5 1;
230  
231 __END__
« Quote/Union.pm ↑ Summary Quote/TwelveData.pm »