|
1 | 1 | """
|
2 | 2 | This is used to convert the currency using the Amdoren Currency API
|
| 3 | +https://www.amdoren.com |
3 | 4 | """
|
4 | 5 |
|
| 6 | +import os |
5 | 7 | import requests
|
6 | 8 |
|
7 |
| -API_KEY = "" # <-- Put your API Key here |
8 |
| -URL_BASE = "https://www.amdoren.com/api/currency.php" |
9 | 9 |
|
10 |
| -# If you are using it on a website and free api key please add the below |
11 |
| -# line in your website |
12 |
| -# Powered by <a href="https://www.amdoren.com">Amdoren</a> |
| 10 | +URL_BASE = "https://www.amdoren.com/api/currency.php" |
| 11 | +if "AMDOREN_API_KEY" not in os.environ: |
| 12 | + raise KeyError("Please put your API key in an environment variable.") |
| 13 | +API_KEY = os.environ["AMDOREN_API_KEY"] |
13 | 14 |
|
14 | 15 |
|
15 | 16 | # Currency and their description
|
|
171 | 172 |
|
172 | 173 |
|
173 | 174 | def convert_currency(
|
174 |
| - baseCurrency: str = "USD", |
175 |
| - targetCurrency: str = "INR", |
176 |
| - amount: float = 1.0, |
177 |
| - apiKey: str = API_KEY, |
| 175 | + from_: str = "USD", |
| 176 | + to: str = "INR", |
| 177 | + amount: float = 1.0, |
| 178 | + api_key: str = API_KEY |
178 | 179 | ) -> str:
|
179 | 180 | """https://www.amdoren.com/currency-api/"""
|
180 |
| - res = requests.get( |
181 |
| - f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\ |
182 |
| - amount={amount}" |
183 |
| - ).json() |
184 |
| - if res["error"] == 0: |
185 |
| - return str(res["amount"]) |
186 |
| - return res["error_message"] |
| 181 | + params = locals() |
| 182 | + params["from"] = params.pop("from_") |
| 183 | + res = requests.get(URL_BASE, params=params).json() |
| 184 | + return str(res["amount"]) if res["error"] == 0 else res["error_message"] |
187 | 185 |
|
188 | 186 |
|
189 | 187 | if __name__ == "__main__":
|
190 |
| - base_currency = input("Enter base currency: ").strip() |
191 |
| - target_currency = input("Enter target currency: ").strip() |
192 |
| - amount = float(input("Enter the amount: ").strip()) |
193 | 188 | print(
|
194 | 189 | convert_currency(
|
195 |
| - baseCurrency=base_currency, targetCurrency=target_currency, amount=amount |
| 190 | + input("Enter from currency: ").strip(), |
| 191 | + input("Enter to currency: ").strip(), |
| 192 | + float(input("Enter the amount: ").strip()), |
196 | 193 | )
|
197 | 194 | )
|
0 commit comments