line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Currency::Convert::SCSB; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
692549
|
use strict; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
87
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
122
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = v0.1.1; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
17
|
use Exporter 'import'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
151
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw(get_currencies convert_currency); |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
1802
|
use Mojo::UserAgent; |
|
3
|
|
|
|
|
1269009
|
|
|
3
|
|
|
|
|
28
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub get_currencies { |
14
|
2
|
|
|
2
|
1
|
6772
|
my ($error, $result); |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
0
|
my $dom; |
17
|
|
|
|
|
|
|
eval { |
18
|
2
|
|
|
|
|
5
|
$dom = _fetch_currency_exchange_web_page(); |
19
|
2
|
50
|
|
|
|
5
|
} or do { |
20
|
2
|
|
|
|
|
23
|
$error = $@; |
21
|
|
|
|
|
|
|
}; |
22
|
2
|
50
|
|
|
|
10
|
return ($error, undef) if defined $error; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
my @col_names = qw(zh_currency_name en_currency_name buy_at sell_at); |
25
|
0
|
|
|
|
|
0
|
my @cols = ( |
26
|
|
|
|
|
|
|
$dom->find("td.txt09 > span")->map('all_text')->to_array(), |
27
|
|
|
|
|
|
|
$dom->find("td.txt09 + td > span")->map('all_text')->to_array(), |
28
|
|
|
|
|
|
|
$dom->find("td.txt09 + td + td.txt101 > span")->map('all_text')->to_array(), |
29
|
|
|
|
|
|
|
$dom->find("td.txt09 + td + td.txt101 + td.txt101 > span")->map('all_text')->to_array(), |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
0
|
my @rows = (); |
33
|
0
|
|
|
|
|
0
|
for my $i (0..$#{$cols[0]}) { |
|
0
|
|
|
|
|
0
|
|
34
|
0
|
|
|
|
|
0
|
my $currency = $cols[1][$i] =~ s/ CASH//r; |
35
|
0
|
|
|
|
|
0
|
push @rows, { |
36
|
|
|
|
|
|
|
currency => $currency, |
37
|
|
|
|
|
|
|
zh_currency_name => $cols[0][$i], |
38
|
|
|
|
|
|
|
en_currency_name => $cols[1][$i], |
39
|
|
|
|
|
|
|
buy_at => $cols[2][$i], |
40
|
|
|
|
|
|
|
sell_at => $cols[3][$i], |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return (undef, \@rows); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub convert_currency { |
48
|
2
|
|
|
2
|
1
|
8849
|
my ($amount, $from_currency, $to_currency) = @_; |
49
|
2
|
100
|
|
|
|
16
|
return ("The convertion target must be 'TWD'. Cannot proceed with '$to_currency'", undef) unless $to_currency eq 'TWD'; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
2
|
my $dom; |
52
|
1
|
|
|
|
|
3
|
my ($error, $result) = get_currencies(); |
53
|
1
|
50
|
|
|
|
6
|
return ($error, undef) if defined $error; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $rate; |
56
|
0
|
|
|
|
|
|
for (@$result) { |
57
|
0
|
0
|
|
|
|
|
next if $_->{en_currency_name} =~ /CASH/; |
58
|
0
|
0
|
|
|
|
|
if ($_->{currency} eq $from_currency) { |
59
|
0
|
|
|
|
|
|
$rate = $_; |
60
|
0
|
|
|
|
|
|
last; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
0
|
0
|
|
|
|
|
return ("Unknown currency: $from_currency", undef) unless $rate; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return (undef, $amount * $rate->{buy_at}); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _fetch_currency_exchange_web_page { |
69
|
0
|
|
|
0
|
|
|
my $ua = Mojo::UserAgent->new; |
70
|
0
|
|
|
|
|
|
my $result = $ua->get('https://ibank.scsb.com.tw/netbank.portal?_nfpb=true&_pageLabel=page_other12&_nfls=false')->result; |
71
|
0
|
0
|
|
|
|
|
die $result->message if $result->is_error; |
72
|
0
|
|
|
|
|
|
return $result->dom; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
__END__ |