-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_currency.py
256 lines (231 loc) · 8.36 KB
/
google_currency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
import logging
import json
import re
import requests
__author__ = "Hariom"
logger = logging.getLogger(__name__)
CODES = {
"AFN": "Afghan Afghani",
"ALL": "Albanian Lek",
"DZD": "Algerian Dinar",
"AOA": "Angolan Kwanza",
"ARS": "Argentine Peso",
"AMD": "Armenian Dram",
"AWG": "Aruban Florin",
"AUD": "Australian Dollar",
"AZN": "Azerbaijani Manat",
"BSD": "Bahamian Dollar",
"BHD": "Bahraini Dinar",
"BBD": "Bajan dollar",
"BDT": "Bangladeshi Taka",
"BYR": "Belarusian Ruble",
"BYN": "Belarusian Ruble",
"BZD": "Belize Dollar",
"BMD": "Bermudan Dollar",
"BTN": "Bhutan currency",
"BTC": "Bitcoin",
"BCH": "Bitcoin Cash",
"BOB": "Bolivian Boliviano",
"BAM": "Bosnia-Herzegovina Convertible Mark",
"BWP": "Botswanan Pula",
"BRL": "Brazilian Real",
"BND": "Brunei Dollar",
"BGN": "Bulgarian Lev",
"BIF": "Burundian Franc",
"XPF": "CFP Franc",
"KHR": "Cambodian riel",
"CAD": "Canadian Dollar",
"CVE": "Cape Verdean Escudo",
"KYD": "Cayman Islands Dollar",
"XAF": "Central African CFA franc",
"CLP": "Chilean Peso",
"CLF": "Chilean Unit of Account (UF)",
"CNY": "Chinese Yuan",
"CNH": "Chinese Yuan (offshore)",
"COP": "Colombian Peso",
"KMF": "Comorian franc",
"CDF": "Congolese Franc",
"CRC": "Costa Rican Colón",
"HRK": "Croatian Kuna",
"CUP": "Cuban Peso",
"CZK": "Czech Koruna",
"DKK": "Danish Krone",
"DJF": "Djiboutian Franc",
"DOP": "Dominican Peso",
"XCD": "East Caribbean Dollar",
"EGP": "Egyptian Pound",
"ETH": "Ether",
"ETB": "Ethiopian Birr",
"EUR": "Euro",
"FJD": "Fijian Dollar",
"GMD": "Gambian dalasi",
"GEL": "Georgian Lari",
"GHC": "Ghanaian Cedi",
"GHS": "Ghanaian Cedi",
"GIP": "Gibraltar Pound",
"GTQ": "Guatemalan Quetzal",
"GNF": "Guinean Franc",
"GYD": "Guyanaese Dollar",
"HTG": "Haitian Gourde",
"HNL": "Honduran Lempira",
"HKD": "Hong Kong Dollar",
"HUF": "Hungarian Forint",
"ISK": "Icelandic Króna",
"INR": "Indian Rupee",
"IDR": "Indonesian Rupiah",
"IRR": "Iranian Rial",
"IQD": "Iraqi Dinar",
"ILS": "Israeli New Shekel",
"JMD": "Jamaican Dollar",
"JPY": "Japanese Yen",
"JOD": "Jordanian Dinar",
"KZT": "Kazakhstani Tenge",
"KES": "Kenyan Shilling",
"KWD": "Kuwaiti Dinar",
"KGS": "Kyrgystani Som",
"LAK": "Laotian Kip",
"LBP": "Lebanese pound",
"LSL": "Lesotho loti",
"LRD": "Liberian Dollar",
"LYD": "Libyan Dinar",
"LTC": "Litecoin",
"MOP": "Macanese Pataca",
"MKD": "Macedonian Denar",
"MGA": "Malagasy Ariary",
"MWK": "Malawian Kwacha",
"MYR": "Malaysian Ringgit",
"MVR": "Maldivian Rufiyaa",
"MRO": "Mauritanian Ouguiya (1973–2017)",
"MUR": "Mauritian Rupee",
"MXN": "Mexican Peso",
"MDL": "Moldovan Leu",
"MAD": "Moroccan Dirham",
"MZM": "Mozambican metical",
"MZN": "Mozambican metical",
"MMK": "Myanmar Kyat",
"TWD": "New Taiwan dollar",
"NAD": "Namibian dollar",
"NPR": "Nepalese Rupee",
"ANG": "Netherlands Antillean Guilder",
"NZD": "New Zealand Dollar",
"NIO": "Nicaraguan Córdoba",
"NGN": "Nigerian Naira",
"NOK": "Norwegian Krone",
"OMR": "Omani Rial",
"PKR": "Pakistani Rupee",
"PAB": "Panamanian Balboa",
"PGK": "Papua New Guinean Kina",
"PYG": "Paraguayan Guarani",
"PHP": "Philippine Piso",
"PLN": "Poland złoty",
"GBP": "Pound sterling",
"QAR": "Qatari Rial",
"ROL": "Romanian Leu",
"RON": "Romanian Leu",
"RUR": "Russian Ruble",
"RUB": "Russian Ruble",
"RWF": "Rwandan franc",
"SVC": "Salvadoran Colón",
"SAR": "Saudi Riyal",
"CSD": "Serbian Dinar",
"RSD": "Serbian Dinar",
"SCR": "Seychellois Rupee",
"SLL": "Sierra Leonean Leone",
"SGD": "Singapore Dollar",
"PEN": "Sol",
"SBD": "Solomon Islands Dollar",
"SOS": "Somali Shilling",
"ZAR": "South African Rand",
"KRW": "South Korean won",
"VEF": "Sovereign Bolivar",
"XDR": "Special Drawing Rights",
"LKR": "Sri Lankan Rupee",
"SSP": "Sudanese pound",
"SDG": "Sudanese pound",
"SRD": "Surinamese Dollar",
"SZL": "Swazi Lilangeni",
"SEK": "Swedish Krona",
"CHF": "Swiss Franc",
"TJS": "Tajikistani Somoni",
"TZS": "Tanzanian Shilling",
"THB": "Thai Baht",
"TOP": "Tongan Paʻanga",
"TTD": "Trinidad & Tobago Dollar",
"TND": "Tunisian Dinar",
"TRY": "Turkish lira",
"TMM": "Turkmenistan manat",
"TMT": "Turkmenistan manat",
"UGX": "Ugandan Shilling",
"UAH": "Ukrainian hryvnia",
"AED": "United Arab Emirates Dirham",
"USD": "United States Dollar",
"UYU": "Uruguayan Peso",
"UZS": "Uzbekistani Som",
"VND": "Vietnamese dong",
"XOF": "West African CFA franc",
"YER": "Yemeni Rial",
"ZMW": "Zambian Kwacha"
}
def convert(currency_from, currency_to, amnt, replace_commas=True):
"""
Used to convert amount from one currency to another
:param currency_from: Currency code from which amount needs to be converted
:param currency_to: Currency code in which amount needs to be converted to
:param amnt: Amount which needs to be converted
:param replace_commas: If True than the commas will be removed from converted amount and the converted amount will
be like 70000 otherwise it will return with comma like 70,000
:return: Json
"""
# Validate the parameters
if not isinstance(currency_from, str):
raise TypeError("currency_from should be of type str, passed %s" % type(currency_from))
if not isinstance(currency_to, str):
raise TypeError("currency_to should be of type str, passed %s" % type(currency_to))
if not isinstance(amnt, float) and not isinstance(amnt, int):
raise TypeError("amount should be either int or float, passed %s" % type(amnt))
url = "http://216.58.221.46/search?q=convert+{amount}+{frm}+to+{to}&hl=en&lr=lang_en".format(amount = str(amnt),
frm = currency_from,
to = currency_to)
currency_from = currency_from.upper()
currency_to = currency_to.upper()
# This will be returned as default if the given code are not present in our database
default_response = {
"from" : currency_from, # From currency code
"to" : currency_to, # To currency code
"amount" : 0, # Amount of currency to be returned
"converted": False # Flag indicating whether the currency is converted or not
}
try:
currency_to_name = CODES[currency_to]
# Just to check whether this currency exists in out currency code base or not
currency_from_name = CODES[currency_from]
# If currency_to_name and currency_from_name is same then user is just trying to convert the same currency and
# we need to return the same value
if currency_to_name == currency_from_name:
default_response["converted"] = True
default_response["amount"] = float(amnt)
return default_response
# response = requests.get(url)
response = requests.get(url, headers={"Range": "bytes=0-1"})
html = response.text
results = re.findall("[\d*\,]*\.\d* {currency_to_name}".format(currency_to_name=currency_to_name), html)
# converted_amount_str = "0.0 {to}".format(to=currency_to)
if results.__len__() > 0:
converted_amount_str = results[0]
converted_currency = re.findall('[\d*\,]*\.\d*', converted_amount_str)[0]
if replace_commas:
converted_currency = converted_currency.replace(',', '')
default_response["amount"] = converted_currency
default_response["converted"] = True
return converted_currency
else:
raise Exception("Unable to convert currency, failed to fetch results from Google")
except KeyError as error:
logger.error("Invalid currency codes passed in parameters, original exception message is -> %s" % error)
except TypeError as error:
logger.error(error)
except Exception as error:
logger.error(error)
finally:
return converted_currency