From 304625d31b51d3b8f059c79634287da77f719b22 Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Wed, 16 Sep 2020 16:49:32 +0530 Subject: [PATCH 1/9] Currency Converter --- web_programming/currency_converter.py | 196 ++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 web_programming/currency_converter.py diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py new file mode 100644 index 000000000000..f6d442257fa6 --- /dev/null +++ b/web_programming/currency_converter.py @@ -0,0 +1,196 @@ +""" +This is used to convert the currency from one currency to another using the Amdoren Currency API +""" + +import requests + +# API_KEY = "" # <-- Put your API Key here +API_KEY = "frSPXKMN93QNsmyQSjTcA4E26z6ryP" +URL_BASE = "https://www.amdoren.com/api/currency.php" + +# If you are using it on a website and free api key please add the below line in your website +# Powered by Amdoren + + +# Currency and their description +list_of_currencies = """ +AED United Arab Emirates Dirham +AFN Afghan Afghani +ALL Albanian Lek +AMD Armenian Dram +ANG Netherlands Antillean Guilder +AOA Angolan Kwanza +ARS Argentine Peso +AUD Australian Dollar +AWG Aruban Florin +AZN Azerbaijani Manat +BAM Bosnia & Herzegovina Convertible Mark +BBD Barbadian Dollar +BDT Bangladeshi Taka +BGN Bulgarian Lev +BHD Bahraini Dinar +BIF Burundian Franc +BMD Bermudian Dollar +BND Brunei Dollar +BOB Bolivian Boliviano +BRL Brazilian Real +BSD Bahamian Dollar +BTN Bhutanese Ngultrum +BWP Botswana Pula +BYN Belarus Ruble +BZD Belize Dollar +CAD Canadian Dollar +CDF Congolese Franc +CHF Swiss Franc +CLP Chilean Peso +CNY Chinese Yuan +COP Colombian Peso +CRC Costa Rican Colon +CUC Cuban Convertible Peso +CVE Cape Verdean Escudo +CZK Czech Republic Koruna +DJF Djiboutian Franc +DKK Danish Krone +DOP Dominican Peso +DZD Algerian Dinar +EGP Egyptian Pound +ERN Eritrean Nakfa +ETB Ethiopian Birr +EUR Euro +FJD Fiji Dollar +GBP British Pound Sterling +GEL Georgian Lari +GHS Ghanaian Cedi +GIP Gibraltar Pound +GMD Gambian Dalasi +GNF Guinea Franc +GTQ Guatemalan Quetzal +GYD Guyanaese Dollar +HKD Hong Kong Dollar +HNL Honduran Lempira +HRK Croatian Kuna +HTG Haiti Gourde +HUF Hungarian Forint +IDR Indonesian Rupiah +ILS Israeli Shekel +INR Indian Rupee +IQD Iraqi Dinar +IRR Iranian Rial +ISK Icelandic Krona +JMD Jamaican Dollar +JOD Jordanian Dinar +JPY Japanese Yen +KES Kenyan Shilling +KGS Kyrgystani Som +KHR Cambodian Riel +KMF Comorian Franc +KPW North Korean Won +KRW South Korean Won +KWD Kuwaiti Dinar +KYD Cayman Islands Dollar +KZT Kazakhstan Tenge +LAK Laotian Kip +LBP Lebanese Pound +LKR Sri Lankan Rupee +LRD Liberian Dollar +LSL Lesotho Loti +LYD Libyan Dinar +MAD Moroccan Dirham +MDL Moldovan Leu +MGA Malagasy Ariary +MKD Macedonian Denar +MMK Myanma Kyat +MNT Mongolian Tugrik +MOP Macau Pataca +MRO Mauritanian Ouguiya +MUR Mauritian Rupee +MVR Maldivian Rufiyaa +MWK Malawi Kwacha +MXN Mexican Peso +MYR Malaysian Ringgit +MZN Mozambican Metical +NAD Namibian Dollar +NGN Nigerian Naira +NIO Nicaragua Cordoba +NOK Norwegian Krone +NPR Nepalese Rupee +NZD New Zealand Dollar +OMR Omani Rial +PAB Panamanian Balboa +PEN Peruvian Nuevo Sol +PGK Papua New Guinean Kina +PHP Philippine Peso +PKR Pakistani Rupee +PLN Polish Zloty +PYG Paraguayan Guarani +QAR Qatari Riyal +RON Romanian Leu +RSD Serbian Dinar +RUB Russian Ruble +RWF Rwanda Franc +SAR Saudi Riyal +SBD Solomon Islands Dollar +SCR Seychellois Rupee +SDG Sudanese Pound +SEK Swedish Krona +SGD Singapore Dollar +SHP Saint Helena Pound +SLL Sierra Leonean Leone +SOS Somali Shilling +SRD Surinamese Dollar +SSP South Sudanese Pound +STD Sao Tome and Principe Dobra +SYP Syrian Pound +SZL Swazi Lilangeni +THB Thai Baht +TJS Tajikistan Somoni +TMT Turkmenistani Manat +TND Tunisian Dinar +TOP Tonga Paanga +TRY Turkish Lira +TTD Trinidad and Tobago Dollar +TWD New Taiwan Dollar +TZS Tanzanian Shilling +UAH Ukrainian Hryvnia +UGX Ugandan Shilling +USD United States Dollar +UYU Uruguayan Peso +UZS Uzbekistan Som +VEF Venezuelan Bolivar +VND Vietnamese Dong +VUV Vanuatu Vatu +WST Samoan Tala +XAF Central African CFA franc +XCD East Caribbean Dollar +XOF West African CFA franc +XPF CFP Franc +YER Yemeni Rial +ZAR South African Rand +ZMW Zambian Kwacha +""" + + +def convert_currency( + baseCurrency: str = "USD", + targetCurrency: str = "INR", + amount: str = "25", + apiKey: str = API_KEY, +): + """https://www.amdoren.com/currency-api/""" + res = requests.get( + f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&amount={amount}" + ).json() + if res["error"] == 0: + return res["amount"] + return res["error_message"] + + +if __name__ == "__main__": + baseCurrency = input("Enter base currency: ").strip() + targetCurrency = input("Enter target currency: ").strip() + amount = input("Enter the amount: ").strip() + print( + convert_currency( + baseCurrency=baseCurrency, targetCurrency=targetCurrency, amount=amount + ) + ) From 963a912799294988984d55a76be2b8a5135de36c Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Wed, 16 Sep 2020 17:01:20 +0530 Subject: [PATCH 2/9] currency converter --- web_programming/currency_converter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index f6d442257fa6..0cf9f5d39523 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -186,11 +186,11 @@ def convert_currency( if __name__ == "__main__": - baseCurrency = input("Enter base currency: ").strip() - targetCurrency = input("Enter target currency: ").strip() + base_currency = input("Enter base currency: ").strip() + target_currency = input("Enter target currency: ").strip() amount = input("Enter the amount: ").strip() print( convert_currency( - baseCurrency=baseCurrency, targetCurrency=targetCurrency, amount=amount + baseCurrency=base_currency, targetCurrency=target_currency, amount=amount ) ) From 87533eddda7504de7bd3688bda3391be6c8540f3 Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Wed, 16 Sep 2020 17:08:43 +0530 Subject: [PATCH 3/9] Currency Converter --- web_programming/currency_converter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 0cf9f5d39523..e9e2464ee1bb 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -173,22 +173,22 @@ def convert_currency( baseCurrency: str = "USD", targetCurrency: str = "INR", - amount: str = "25", + amount: float = 1.0, apiKey: str = API_KEY, -): +) -> str: """https://www.amdoren.com/currency-api/""" res = requests.get( f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&amount={amount}" ).json() if res["error"] == 0: - return res["amount"] + return str(res["amount"]) return res["error_message"] if __name__ == "__main__": base_currency = input("Enter base currency: ").strip() target_currency = input("Enter target currency: ").strip() - amount = input("Enter the amount: ").strip() + amount = float(input("Enter the amount: ").strip()) print( convert_currency( baseCurrency=base_currency, targetCurrency=target_currency, amount=amount From 47cb760366148be6b7ce6d5227e122bddd6612f0 Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Wed, 16 Sep 2020 17:25:18 +0530 Subject: [PATCH 4/9] currency converter --- web_programming/currency_converter.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index e9e2464ee1bb..267437652ac3 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -1,14 +1,14 @@ """ -This is used to convert the currency from one currency to another using the Amdoren Currency API +This is used to convert the currency using the Amdoren Currency API """ import requests -# API_KEY = "" # <-- Put your API Key here -API_KEY = "frSPXKMN93QNsmyQSjTcA4E26z6ryP" +API_KEY = "" # <-- Put your API Key here URL_BASE = "https://www.amdoren.com/api/currency.php" -# If you are using it on a website and free api key please add the below line in your website +# If you are using it on a website and free api key please add the below +# line in your website # Powered by Amdoren @@ -178,7 +178,8 @@ def convert_currency( ) -> str: """https://www.amdoren.com/currency-api/""" res = requests.get( - f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&amount={amount}" + f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\ + amount={amount}" ).json() if res["error"] == 0: return str(res["amount"]) From 10c7f4fbd618958d831c79de5ab8f1a7c31154bf Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Sat, 26 Sep 2020 09:29:11 +0530 Subject: [PATCH 5/9] implemented changes --- web_programming/currency_converter.py | 37 ++++++++++++--------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 267437652ac3..887d8f01776a 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -1,15 +1,16 @@ """ This is used to convert the currency using the Amdoren Currency API +https://www.amdoren.com """ +import os import requests -API_KEY = "" # <-- Put your API Key here -URL_BASE = "https://www.amdoren.com/api/currency.php" -# If you are using it on a website and free api key please add the below -# line in your website -# Powered by Amdoren +URL_BASE = "https://www.amdoren.com/api/currency.php" +if "AMDOREN_API_KEY" not in os.environ: + raise KeyError("Please put your API key in an environment variable.") +API_KEY = os.environ["AMDOREN_API_KEY"] # Currency and their description @@ -171,27 +172,23 @@ def convert_currency( - baseCurrency: str = "USD", - targetCurrency: str = "INR", - amount: float = 1.0, - apiKey: str = API_KEY, + from_: str = "USD", + to: str = "INR", + amount: float = 1.0, + api_key: str = API_KEY ) -> str: """https://www.amdoren.com/currency-api/""" - res = requests.get( - f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\ - amount={amount}" - ).json() - if res["error"] == 0: - return str(res["amount"]) - return res["error_message"] + params = locals() + params["from"] = params.pop("from_") + res = requests.get(URL_BASE, params=params).json() + return str(res["amount"]) if res["error"] == 0 else res["error_message"] if __name__ == "__main__": - base_currency = input("Enter base currency: ").strip() - target_currency = input("Enter target currency: ").strip() - amount = float(input("Enter the amount: ").strip()) print( convert_currency( - baseCurrency=base_currency, targetCurrency=target_currency, amount=amount + input("Enter from currency: ").strip(), + input("Enter to currency: ").strip(), + float(input("Enter the amount: ").strip()), ) ) From 6070213e831bc8ce3e30b118fb9baa8296f3a282 Mon Sep 17 00:00:00 2001 From: niranjan hegde Date: Sat, 26 Sep 2020 10:03:25 +0530 Subject: [PATCH 6/9] Implemented changes requested --- web_programming/currency_converter.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 887d8f01776a..906e0435862f 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -172,10 +172,7 @@ def convert_currency( - from_: str = "USD", - to: str = "INR", - amount: float = 1.0, - api_key: str = API_KEY + from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY ) -> str: """https://www.amdoren.com/currency-api/""" params = locals() From ea45c82875b797a69a4d4c8e4195d52e0397669d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 26 Sep 2020 12:55:04 +0200 Subject: [PATCH 7/9] TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) --- web_programming/currency_converter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 906e0435862f..655897f128cb 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -8,9 +8,10 @@ URL_BASE = "https://www.amdoren.com/api/currency.php" -if "AMDOREN_API_KEY" not in os.environ: +TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) +API_KEY = os.getenv("AMDOREN_API_KEY") +if not API_KEY and not TESTING: raise KeyError("Please put your API key in an environment variable.") -API_KEY = os.environ["AMDOREN_API_KEY"] # Currency and their description From 2776706f11bcd08b41a97db47664220236b51509 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Nov 2020 08:54:07 +0100 Subject: [PATCH 8/9] Update currency_converter.py --- web_programming/currency_converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 655897f128cb..dda4a47fa982 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -8,7 +8,7 @@ URL_BASE = "https://www.amdoren.com/api/currency.php" -TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) +TESTING = os.getenv("CI", False) API_KEY = os.getenv("AMDOREN_API_KEY") if not API_KEY and not TESTING: raise KeyError("Please put your API key in an environment variable.") From e77e96dde461111612b67fb8004884c8888db2f8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Nov 2020 08:58:33 +0100 Subject: [PATCH 9/9] Update currency_converter.py --- web_programming/currency_converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index dda4a47fa982..6aed2a5578a5 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -4,8 +4,8 @@ """ import os -import requests +import requests URL_BASE = "https://www.amdoren.com/api/currency.php" TESTING = os.getenv("CI", False)