| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Finance::Currency::Convert::Esunbank; |
|
2
|
3
|
|
|
3
|
|
699610
|
use strict; |
|
|
3
|
|
|
|
|
17
|
|
|
|
3
|
|
|
|
|
90
|
|
|
3
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
122
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = v0.1.1; |
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
16
|
use Exporter 'import'; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
156
|
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw(get_currencies convert_currency); |
|
9
|
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
18
|
use POSIX qw(strftime); |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
25
|
|
|
11
|
3
|
|
|
3
|
|
6731
|
use Mojo::UserAgent; |
|
|
3
|
|
|
|
|
1250328
|
|
|
|
3
|
|
|
|
|
28
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub get_currencies { |
|
14
|
2
|
|
|
2
|
1
|
6679
|
my ($error, $result); |
|
15
|
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
0
|
my $dom; |
|
17
|
|
|
|
|
|
|
eval { |
|
18
|
2
|
|
|
|
|
7
|
$dom = _fetch_currency_exchange_web_page(); |
|
19
|
2
|
50
|
|
|
|
4
|
} or do { |
|
20
|
2
|
|
|
|
|
23
|
$error = @$; |
|
21
|
|
|
|
|
|
|
}; |
|
22
|
2
|
50
|
|
|
|
10
|
return ($error, undef) if defined $error; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @rows = $dom->find("table.tableStyle2 tr")->map( |
|
25
|
|
|
|
|
|
|
sub { |
|
26
|
0
|
|
|
0
|
|
0
|
my ($el) = @_; |
|
27
|
0
|
|
|
|
|
0
|
my @cells = $el->find("td")->each; |
|
28
|
0
|
0
|
|
|
|
0
|
return unless @cells; |
|
29
|
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
0
|
my @names = $cells[0]->all_text =~ m/ (\p{Han}+) \( (\p{Latin}{3}) \) /x; |
|
31
|
|
|
|
|
|
|
return { |
|
32
|
0
|
|
|
|
|
0
|
currency => $names[1], |
|
33
|
|
|
|
|
|
|
zh_currency_name => $names[0], |
|
34
|
|
|
|
|
|
|
en_currency_name => $names[1], |
|
35
|
|
|
|
|
|
|
buy_at => $cells[2]->all_text, |
|
36
|
|
|
|
|
|
|
sell_at => $cells[2]->all_text, |
|
37
|
|
|
|
|
|
|
}; |
|
38
|
0
|
|
|
|
|
0
|
})->each; |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
0
|
return (undef, \@rows); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub convert_currency { |
|
44
|
2
|
|
|
2
|
1
|
9080
|
my ($amount, $from_currency, $to_currency) = @_; |
|
45
|
2
|
100
|
|
|
|
16
|
return ("The convertion target must be 'TWD'. Cannot proceed with '$to_currency'", undef) unless $to_currency eq 'TWD'; |
|
46
|
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
3
|
my ($error, $result) = get_currencies(); |
|
48
|
1
|
50
|
|
|
|
6
|
return ($error, undef) if defined $error; |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $rate; |
|
51
|
0
|
|
|
|
|
|
for (@$result) { |
|
52
|
0
|
0
|
|
|
|
|
if ($_->{currency} eq $from_currency) { |
|
53
|
0
|
|
|
|
|
|
$rate = $_; |
|
54
|
0
|
|
|
|
|
|
last; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
} |
|
57
|
0
|
0
|
|
|
|
|
return ("Unknown currency: $from_currency", undef) unless $rate; |
|
58
|
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return (undef, $amount * $rate->{buy_at}); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _fetch_currency_exchange_web_page { |
|
63
|
0
|
|
|
0
|
|
|
my @t = localtime(); |
|
64
|
0
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
|
65
|
0
|
|
|
|
|
|
$ua->transactor->name('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:76.0) Gecko/20100101 Firefox/76.0'); |
|
66
|
0
|
|
|
|
|
|
my $result = $ua->get('https://www.esunbank.com.tw/bank/iframe/widget/rate/foreign-exchange-rate')->result; |
|
67
|
0
|
0
|
|
|
|
|
die $result->message if $result->is_error; |
|
68
|
0
|
|
|
|
|
|
return $result->dom; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
1; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |