-
Notifications
You must be signed in to change notification settings - Fork 0
/
getxchgrates.pl
52 lines (38 loc) · 1.11 KB
/
getxchgrates.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl
# Perl script to download and parse exchange rates for Polish zloty PLN to Dygraph format
# sudo apt install libxml-simple-perl
# perl -MCPAN -e'install "LWP::Simple"'
# perl -MCPAN -e'install "XML::Simple"'
use IO::Socket;
use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
my $url = 'http://www.nbp.pl/kursy/xml/dir.txt';
my $file = 'dir.txt';
getstore( $url, $file );
open my $handle, '<', $file;
chomp( my @lines = <$handle> );
close $handle;
my @files;
foreach (@lines) {
if ( ord($_) == 97 ) {
chomp($_);
push @files, $_;
}
}
open( my $outh, '>', 'output.txt' );
print $outh "D,EUR,USD,CHF,GBP\n";
for my $i ( 0 .. $#files ) {
my $fn = $files[$i];
chop $fn;
$fn = $fn . '.xml';
my $url = 'http://www.nbp.pl/kursy/xml/' . $fn;
getstore( $url, $fn );
my $xml = XMLin($fn);
print $outh $xml->{data_publikacji} . ',';
print $outh $xml->{pozycja}[7]{kurs_sredni} =~ s/,/./r . ',';
print $outh $xml->{pozycja}[1]{kurs_sredni} =~ s/,/./r . ',';
print $outh $xml->{pozycja}[9]{kurs_sredni} =~ s/,/./r . ',';
print $outh $xml->{pozycja}[10]{kurs_sredni} =~ s/,/./r . "\n";
}