Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7350746

Browse files
Added currency converter programs
1 parent 39cb037 commit 7350746

File tree

3 files changed

+144
-13
lines changed

3 files changed

+144
-13
lines changed

‎README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![](https://github.com/rai-gaurav/Data-Structures-and-Algorithms-Perl/workflows/Mac_CI/badge.svg)](https://github.com/rai-gaurav/Data-Structures-and-Algorithms-Perl/actions)
99
[![](https://github.com/rai-gaurav/Data-Structures-and-Algorithms-Perl/workflows/Windows_CI/badge.svg)](https://github.com/rai-gaurav/Data-Structures-and-Algorithms-Perl/actions)  
1010

11-
![](https://img.shields.io/github/repo-size/rai-gaurav/Data-Structures-and-Algorithms-Perl.svg?label=Repo%20size&style=flat)
11+
![](https://img.shields.io/github/repo-size/rai-gaurav/Data-Structures-and-Algorithms-Perl.svg?label=Repo%20size&style=flat)
1212
![](https://img.shields.io/coveralls/github/badges/shields.svg?style=flat)
1313
![](https://img.shields.io/github/license/rai-gaurav/Data-Structures-and-Algorithms-Perl)  
1414

@@ -23,17 +23,17 @@ Hopefully will work on older version too (as old as 5.10) and across diferent OS
2323

2424
| Backtracking | Status |
2525
| ---------------- | --------- |
26-
| 8 Queens Problem |
26+
| 8 Queens Problem |
2727
| Combinations | Completed |
2828
| Permutations | Completed |
29-
| Sudoku |
29+
| Sudoku |
3030

31-
| Blockchain | Status |
32-
| ---------- | ------ |
31+
| Blockchain | Status |
32+
| ------------------------- | --------- |
3333
| chinese_remainder_theorem | Completed |
3434

35-
| Client_Server | Status |
36-
| ------------- | ------ |
35+
| Client_Server | Status |
36+
| --------------------- | --------- |
3737
| Client Server example | Completed |
3838

3939
| Compression | Status |
@@ -129,9 +129,12 @@ Hopefully will work on older version too (as old as 5.10) and across diferent OS
129129
| Problem 7 | Completed |
130130
| Problem 8 | Completed |
131131

132-
| Web Programming | Status |
133-
| --------------- | --------- |
134-
| Catalyst |
135-
| Dancer2 |
136-
| Mojolicious |
137-
| Web Crawling |
132+
| Web Programming | Status |
133+
| ------------------ | --------- |
134+
| Catalyst |
135+
| Dancer2 |
136+
| Mojolicious |
137+
| Web Crawling |
138+
| Currency Converter | Completed |
139+
| Weather |
140+
| Stock Price |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env perl
2+
# There are many api available to get conversion rates. Some free with limitations.
3+
# You can use any of them but there is less chance that any of them will be real time.
4+
# Some of them are -
5+
# https://www.amdoren.com/currency-api/
6+
# https://fixer.io/
7+
# https://currencylayer.com/
8+
# https://www.exchangerate-api.com/docs/perl-currency-api
9+
10+
# There are also different client lib available to perform these request in Perl.
11+
# Current used one - https://metacpan.org/pod/LWP::UserAgent
12+
13+
use strict;
14+
use warnings;
15+
use LWP::UserAgent;
16+
use URI;
17+
use IO::Socket::SSL;
18+
use JSON;
19+
use Carp;
20+
21+
# This will return the fingerprint of the given hostname
22+
sub get_fingerprint {
23+
my ($dst) = @_;
24+
my $client = IO::Socket::SSL->new(
25+
PeerAddr => $dst,
26+
PeerPort => 443,
27+
28+
# certificate cannot be validated the normal way, so we need to
29+
# disable validation this one time in the hope that there is
30+
# currently no man in the middle attack
31+
SSL_verify_mode => 0,
32+
) or croak "connect failed";
33+
my $fp = $client->get_fingerprint;
34+
return $fp;
35+
}
36+
37+
sub convert_currency {
38+
my ($ua, $parameters) = @_;
39+
40+
my $url = URI->new("https://www.amdoren.com/api/currency.php");
41+
$url->query_form(%{$parameters});
42+
43+
my $response = $ua->get($url);
44+
if ($response->is_success) {
45+
my $message = $response->decoded_content;
46+
my $json_response = from_json($message);
47+
print "\n"
48+
. $parameters->{'amount'} . " "
49+
. $parameters->{'from'} . " = "
50+
. $json_response->{'amount'} . " "
51+
. $parameters->{'to'};
52+
}
53+
else {
54+
croak $response->status_line;
55+
}
56+
}
57+
58+
sub main {
59+
60+
# This server certificate cannot be properly validated by normal means hence,
61+
# we are getting fingerprint and passing that in LWP options.
62+
# This step is not needed if we have proper server certificate.
63+
# Reference - https://stackoverflow.com/a/35078145/2001559
64+
my $fp = get_fingerprint("www.amdoren.com");
65+
my $ua = LWP::UserAgent->new(ssl_opts => {SSL_fingerprint => $fp});
66+
$ua->show_progress(1);
67+
68+
# Add your API key here or in your shell environment and remove this line
69+
local $ENV{"AMDOREN_API_KEY"} = "";
70+
71+
# Convert 10 USD to INR, API key will be taken from env variable
72+
convert_currency($ua,
73+
{"api_key" => $ENV{"AMDOREN_API_KEY"}, "from" => "USD", "to" => "INR", "amount" => 10});
74+
}
75+
76+
main;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env perl
2+
# There are many api available to get conversion rates. Some free with limitations.
3+
# You can use any of them but there is less chance that any of them will be real time.
4+
# Some of them are -
5+
# https://www.amdoren.com/currency-api/
6+
# https://fixer.io/
7+
# https://currencylayer.com/
8+
# https://www.exchangerate-api.com/docs/perl-currency-api
9+
10+
# There are also different client lib available to perform these request in Perl.
11+
# Current used one - https://metacpan.org/pod/Mojo::UserAgent
12+
13+
use strict;
14+
use warnings;
15+
use Mojo::UserAgent;
16+
17+
sub convert_currency {
18+
my ($ua, $parameters) = @_;
19+
20+
my $url = "https://www.amdoren.com/api/currency.php";
21+
22+
# This server certificate cannot be properly validated by normal means hence,
23+
# we are disabling TLS certificate verification.
24+
# There are other ways also like this - https://stackoverflow.com/a/35208289/2001559
25+
$ua->insecure(1);
26+
27+
my $response = $ua->get($url => form => $parameters)->result;
28+
if ($response->is_success) {
29+
my $message = $response->json;
30+
print "\n"
31+
. $parameters->{'amount'} . " "
32+
. $parameters->{'from'} . " = "
33+
. $message->{'amount'} . " "
34+
. $parameters->{'to'};
35+
}
36+
elsif ($response->is_error) {
37+
print $response->message;
38+
}
39+
}
40+
41+
sub main {
42+
my $ua = Mojo::UserAgent->new();
43+
44+
# Add your API key here or in your shell environment and remove this line
45+
local $ENV{"AMDOREN_API_KEY"} = "";
46+
47+
# Convert 10 USD to INR, API key will be taken from env variable
48+
convert_currency($ua,
49+
{"api_key" => $ENV{"AMDOREN_API_KEY"}, "from" => "USD", "to" => "INR", "amount" => 10});
50+
}
51+
52+
main;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /