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