From ff74e8377d6813261bba0ee8dce9cda4b43b3748 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:09:24 +0000 Subject: [PATCH 1/8] Infer currency from local ip address --- bumblebee/modules/currency.py | 46 ++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index b01a186..32274d2 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -8,7 +8,7 @@ Requires the following python packages: Parameters: * currency.interval: Interval in minutes between updates, default is 1. - * currency.source: Source currency (defaults to "GBP") + * currency.source: Source currency (defaults to "GBP"). Set to "auto" infer the local one. * currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR") * currency.sourceformat: String format for source formatting; Defaults to "{}: {}" and has two variables, the base symbol and the rate list @@ -28,6 +28,8 @@ except ImportError: SYMBOL = { "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥" } +DEFAULT_DEST = "USD,EUR,GBP" +DEFAULT_SRC = "GBP" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" @@ -39,20 +41,30 @@ class Module(bumblebee.engine.Module): self._data = [] self.interval_factor(60) self.interval(1) - self._base = self.parameter("source", "GBP") - self._symbols = self.parameter("destination", "USD,EUR").split(",") self._nextcheck = 0 + src = self.parameter("source", "auto") + if src == "auto": + self._base = self.find_local_currency() + else: + self._base = src + + dest = [d for d in self.parameter("destination", DEFAULT_DEST).split(",") + if d != self._base] + self._symbols = dest + + def price(self, widget): if len(self._data) == 0: return "?" rates = [] for sym, rate in self._data: - rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) + rate = float(rate) + rates.append(u"{:.3g}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) - basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}")) - ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|")) + basefmt = u"{}".format(self.parameter("sourceformat", "1{}={}")) + ratefmt = u"{}".format(self.parameter("destinationdelimiter", "=")) return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates)) @@ -66,4 +78,26 @@ class Module(bumblebee.engine.Module): except Exception: pass + def find_local_currency(self): + '''Use geolocation lookup to find local currency''' + try: + r = requests.get('https://ipvigilante.com/') + if not r.ok: return DEFAULT_SRC + dt = r.json() + if dt['status'] != 'success': + return DEFAULT_SRC + country = dt['data']['country_name'] + + r = requests.get("https://raw.githubusercontent.com/samayo/country-json/master/src/country-by-currency-code.json") + if not r.ok: return DEFAULT_SRC + data = r.json() + country2curr = {} + for dt in data: + country2curr[dt['country']] = dt['currency_code'] + + return country2curr.get(country, DEFAULT_SRC) + except: + return DEFAULT_SRC + + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 75d850332901e2f6c6d7d010a605f26858046fb2 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:16:35 +0000 Subject: [PATCH 2/8] Do not format large numbers + default to "auto" Now the currency is inferred automatically from local ip. Note that if you use a VPN, you will get the wrong currency! --- bumblebee/modules/currency.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 32274d2..62b9f4e 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -26,10 +26,10 @@ except ImportError: pass SYMBOL = { - "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥" + "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩" } DEFAULT_DEST = "USD,EUR,GBP" -DEFAULT_SRC = "GBP" +DEFAULT_SRC = "auto" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" @@ -43,7 +43,7 @@ class Module(bumblebee.engine.Module): self.interval(1) self._nextcheck = 0 - src = self.parameter("source", "auto") + src = self.parameter("source", DEFAULT_SRC) if src == "auto": self._base = self.find_local_currency() else: @@ -60,8 +60,8 @@ class Module(bumblebee.engine.Module): rates = [] for sym, rate in self._data: - rate = float(rate) - rates.append(u"{:.3g}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) + rate = self.fmt_rate(rate) + rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) basefmt = u"{}".format(self.parameter("sourceformat", "1{}={}")) ratefmt = u"{}".format(self.parameter("destinationdelimiter", "=")) @@ -99,5 +99,11 @@ class Module(bumblebee.engine.Module): except: return DEFAULT_SRC + def fmt_rate(self, rate): + float_rate = float(rate.replace(',', '')) + if not 0.01 < float_rate < 100: + return rate + else: + return "%.3g" % float_rate # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 140058e7e5bcdd5eb3c3e564f4eaf1820862e614 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:18:44 +0000 Subject: [PATCH 3/8] Update comment --- bumblebee/modules/currency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 62b9f4e..f070ad1 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -8,7 +8,7 @@ Requires the following python packages: Parameters: * currency.interval: Interval in minutes between updates, default is 1. - * currency.source: Source currency (defaults to "GBP"). Set to "auto" infer the local one. + * currency.source: Source currency (ex. "GBP", "EUR"). Defaults to "auto", which infers the local one from IP address. * currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR") * currency.sourceformat: String format for source formatting; Defaults to "{}: {}" and has two variables, the base symbol and the rate list From 6ad4a559d3eccb19f8a8a4461abac982bfb76bcf Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:33:52 +0000 Subject: [PATCH 4/8] Use local file instead of distant one --- bumblebee/modules/currency.py | 20 +- .../data/country-by-currency-code.json | 974 ++++++++++++++++++ 2 files changed, 987 insertions(+), 7 deletions(-) create mode 100644 bumblebee/modules/data/country-by-currency-code.json diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index f070ad1..5876acd 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -24,12 +24,15 @@ try: import requests except ImportError: pass +import json +import os SYMBOL = { "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩" } DEFAULT_DEST = "USD,EUR,GBP" DEFAULT_SRC = "auto" +DEFAULT_SRC_FALLBACK = "GBP" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" @@ -82,22 +85,25 @@ class Module(bumblebee.engine.Module): '''Use geolocation lookup to find local currency''' try: r = requests.get('https://ipvigilante.com/') - if not r.ok: return DEFAULT_SRC + if not r.ok: + return DEFAULT_SRC_FALLBACK dt = r.json() if dt['status'] != 'success': - return DEFAULT_SRC + return DEFAULT_SRC_FALLBACK country = dt['data']['country_name'] - r = requests.get("https://raw.githubusercontent.com/samayo/country-json/master/src/country-by-currency-code.json") - if not r.ok: return DEFAULT_SRC - data = r.json() + fname = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + 'data', 'country-by-currency-code.json') + with open(fname, 'r') as f: + data = json.load(f) country2curr = {} for dt in data: country2curr[dt['country']] = dt['currency_code'] return country2curr.get(country, DEFAULT_SRC) - except: - return DEFAULT_SRC + except Exception as e: + return DEFAULT_SRC_FALLBACK def fmt_rate(self, rate): float_rate = float(rate.replace(',', '')) diff --git a/bumblebee/modules/data/country-by-currency-code.json b/bumblebee/modules/data/country-by-currency-code.json new file mode 100644 index 0000000..d29a3d7 --- /dev/null +++ b/bumblebee/modules/data/country-by-currency-code.json @@ -0,0 +1,974 @@ +[ + { + "country": "Afghanistan", + "currency_code": "AFN" + }, + { + "country": "Albania", + "currency_code": "ALL" + }, + { + "country": "Algeria", + "currency_code": "DZD" + }, + { + "country": "American Samoa", + "currency_code": "USD" + }, + { + "country": "Andorra", + "currency_code": "EUR" + }, + { + "country": "Angola", + "currency_code": "AOA" + }, + { + "country": "Anguilla", + "currency_code": "XCD" + }, + { + "country": "Antarctica", + "currency_code": "XCD" + }, + { + "country": "Antigua and Barbuda", + "currency_code": "XCD" + }, + { + "country": "Argentina", + "currency_code": "ARS" + }, + { + "country": "Armenia", + "currency_code": "AMD" + }, + { + "country": "Aruba", + "currency_code": "AWG" + }, + { + "country": "Australia", + "currency_code": "AUD" + }, + { + "country": "Austria", + "currency_code": "EUR" + }, + { + "country": "Azerbaijan", + "currency_code": "AZN" + }, + { + "country": "Bahamas", + "currency_code": "BSD" + }, + { + "country": "Bahrain", + "currency_code": "BHD" + }, + { + "country": "Bangladesh", + "currency_code": "BDT" + }, + { + "country": "Barbados", + "currency_code": "BBD" + }, + { + "country": "Belarus", + "currency_code": "BYR" + }, + { + "country": "Belgium", + "currency_code": "EUR" + }, + { + "country": "Belize", + "currency_code": "BZD" + }, + { + "country": "Benin", + "currency_code": "XOF" + }, + { + "country": "Bermuda", + "currency_code": "BMD" + }, + { + "country": "Bhutan", + "currency_code": "BTN" + }, + { + "country": "Bolivia", + "currency_code": "BOB" + }, + { + "country": "Bosnia and Herzegovina", + "currency_code": "BAM" + }, + { + "country": "Botswana", + "currency_code": "BWP" + }, + { + "country": "Bouvet Island", + "currency_code": "NOK" + }, + { + "country": "Brazil", + "currency_code": "BRL" + }, + { + "country": "British Indian Ocean Territory", + "currency_code": "USD" + }, + { + "country": "Brunei", + "currency_code": "BND" + }, + { + "country": "Bulgaria", + "currency_code": "BGN" + }, + { + "country": "Burkina Faso", + "currency_code": "XOF" + }, + { + "country": "Burundi", + "currency_code": "BIF" + }, + { + "country": "Cambodia", + "currency_code": "KHR" + }, + { + "country": "Cameroon", + "currency_code": "XAF" + }, + { + "country": "Canada", + "currency_code": "CAD" + }, + { + "country": "Cape Verde", + "currency_code": "CVE" + }, + { + "country": "Cayman Islands", + "currency_code": "KYD" + }, + { + "country": "Central African Republic", + "currency_code": "XAF" + }, + { + "country": "Chad", + "currency_code": "XAF" + }, + { + "country": "Chile", + "currency_code": "CLP" + }, + { + "country": "China", + "currency_code": "CNY" + }, + { + "country": "Christmas Island", + "currency_code": "AUD" + }, + { + "country": "Cocos (Keeling) Islands", + "currency_code": "AUD" + }, + { + "country": "Colombia", + "currency_code": "COP" + }, + { + "country": "Comoros", + "currency_code": "KMF" + }, + { + "country": "Congo", + "currency_code": "XAF" + }, + { + "country": "Cook Islands", + "currency_code": "NZD" + }, + { + "country": "Costa Rica", + "currency_code": "CRC" + }, + { + "country": "Croatia", + "currency_code": "HRK" + }, + { + "country": "Cuba", + "currency_code": "CUP" + }, + { + "country": "Cyprus", + "currency_code": "EUR" + }, + { + "country": "Czech Republic", + "currency_code": "CZK" + }, + { + "country": "Denmark", + "currency_code": "DKK" + }, + { + "country": "Djibouti", + "currency_code": "DJF" + }, + { + "country": "Dominica", + "currency_code": "XCD" + }, + { + "country": "Dominican Republic", + "currency_code": "DOP" + }, + { + "country": "East Timor", + "currency_code": "USD" + }, + { + "country": "Ecuador", + "currency_code": "ECS" + }, + { + "country": "Egypt", + "currency_code": "EGP" + }, + { + "country": "El Salvador", + "currency_code": "SVC" + }, + { + "country": "England", + "currency_code": "GBP" + }, + { + "country": "Equatorial Guinea", + "currency_code": "XAF" + }, + { + "country": "Eritrea", + "currency_code": "ERN" + }, + { + "country": "Estonia", + "currency_code": "EUR" + }, + { + "country": "Ethiopia", + "currency_code": "ETB" + }, + { + "country": "Falkland Islands", + "currency_code": "FKP" + }, + { + "country": "Faroe Islands", + "currency_code": "DKK" + }, + { + "country": "Fiji Islands", + "currency_code": "FJD" + }, + { + "country": "Finland", + "currency_code": "EUR" + }, + { + "country": "France", + "currency_code": "EUR" + }, + { + "country": "French Guiana", + "currency_code": "EUR" + }, + { + "country": "French Polynesia", + "currency_code": "XPF" + }, + { + "country": "French Southern territories", + "currency_code": "EUR" + }, + { + "country": "Gabon", + "currency_code": "XAF" + }, + { + "country": "Gambia", + "currency_code": "GMD" + }, + { + "country": "Georgia", + "currency_code": "GEL" + }, + { + "country": "Germany", + "currency_code": "EUR" + }, + { + "country": "Ghana", + "currency_code": "GHS" + }, + { + "country": "Gibraltar", + "currency_code": "GIP" + }, + { + "country": "Greece", + "currency_code": "EUR" + }, + { + "country": "Greenland", + "currency_code": "DKK" + }, + { + "country": "Grenada", + "currency_code": "XCD" + }, + { + "country": "Guadeloupe", + "currency_code": "EUR" + }, + { + "country": "Guam", + "currency_code": "USD" + }, + { + "country": "Guatemala", + "currency_code": "QTQ" + }, + { + "country": "Guinea", + "currency_code": "GNF" + }, + { + "country": "Guinea-Bissau", + "currency_code": "CFA" + }, + { + "country": "Guyana", + "currency_code": "GYD" + }, + { + "country": "Haiti", + "currency_code": "HTG" + }, + { + "country": "Heard Island and McDonald Islands", + "currency_code": "AUD" + }, + { + "country": "Holy See (Vatican City State)", + "currency_code": "EUR" + }, + { + "country": "Honduras", + "currency_code": "HNL" + }, + { + "country": "Hong Kong", + "currency_code": "HKD" + }, + { + "country": "Hungary", + "currency_code": "HUF" + }, + { + "country": "Iceland", + "currency_code": "ISK" + }, + { + "country": "India", + "currency_code": "INR" + }, + { + "country": "Indonesia", + "currency_code": "IDR" + }, + { + "country": "Iran", + "currency_code": "IRR" + }, + { + "country": "Iraq", + "currency_code": "IQD" + }, + { + "country": "Ireland", + "currency_code": "EUR" + }, + { + "country": "Israel", + "currency_code": "ILS" + }, + { + "country": "Italy", + "currency_code": "EUR" + }, + { + "country": "Ivory Coast", + "currency_code": "XOF" + }, + { + "country": "Jamaica", + "currency_code": "JMD" + }, + { + "country": "Japan", + "currency_code": "JPY" + }, + { + "country": "Jordan", + "currency_code": "JOD" + }, + { + "country": "Kazakhstan", + "currency_code": "KZT" + }, + { + "country": "Kenya", + "currency_code": "KES" + }, + { + "country": "Kiribati", + "currency_code": "AUD" + }, + { + "country": "Kuwait", + "currency_code": "KWD" + }, + { + "country": "Kyrgyzstan", + "currency_code": "KGS" + }, + { + "country": "Laos", + "currency_code": "LAK" + }, + { + "country": "Latvia", + "currency_code": "LVL" + }, + { + "country": "Lebanon", + "currency_code": "LBP" + }, + { + "country": "Lesotho", + "currency_code": "LSL" + }, + { + "country": "Liberia", + "currency_code": "LRD" + }, + { + "country": "Libyan Arab Jamahiriya", + "currency_code": "LYD" + }, + { + "country": "Liechtenstein", + "currency_code": "CHF" + }, + { + "country": "Lithuania", + "currency_code": "LTL" + }, + { + "country": "Luxembourg", + "currency_code": "EUR" + }, + { + "country": "Macao", + "currency_code": "MOP" + }, + { + "country": "North Macedonia", + "currency_code": "MKD" + }, + { + "country": "Madagascar", + "currency_code": "MGF" + }, + { + "country": "Malawi", + "currency_code": "MWK" + }, + { + "country": "Malaysia", + "currency_code": "MYR" + }, + { + "country": "Maldives", + "currency_code": "MVR" + }, + { + "country": "Mali", + "currency_code": "XOF" + }, + { + "country": "Malta", + "currency_code": "EUR" + }, + { + "country": "Marshall Islands", + "currency_code": "USD" + }, + { + "country": "Martinique", + "currency_code": "EUR" + }, + { + "country": "Mauritania", + "currency_code": "MRO" + }, + { + "country": "Mauritius", + "currency_code": "MUR" + }, + { + "country": "Mayotte", + "currency_code": "EUR" + }, + { + "country": "Mexico", + "currency_code": "MXN" + }, + { + "country": "Micronesia, Federated States of", + "currency_code": "USD" + }, + { + "country": "Moldova", + "currency_code": "MDL" + }, + { + "country": "Monaco", + "currency_code": "EUR" + }, + { + "country": "Mongolia", + "currency_code": "MNT" + }, + { + "country": "Montserrat", + "currency_code": "XCD" + }, + { + "country": "Morocco", + "currency_code": "MAD" + }, + { + "country": "Mozambique", + "currency_code": "MZN" + }, + { + "country": "Myanmar", + "currency_code": "MMR" + }, + { + "country": "Namibia", + "currency_code": "NAD" + }, + { + "country": "Nauru", + "currency_code": "AUD" + }, + { + "country": "Nepal", + "currency_code": "NPR" + }, + { + "country": "Netherlands", + "currency_code": "EUR" + }, + { + "country": "Netherlands Antilles", + "currency_code": "ANG" + }, + { + "country": "New Caledonia", + "currency_code": "XPF" + }, + { + "country": "New Zealand", + "currency_code": "NZD" + }, + { + "country": "Nicaragua", + "currency_code": "NIO" + }, + { + "country": "Niger", + "currency_code": "XOF" + }, + { + "country": "Nigeria", + "currency_code": "NGN" + }, + { + "country": "Niue", + "currency_code": "NZD" + }, + { + "country": "Norfolk Island", + "currency_code": "AUD" + }, + { + "country": "North Korea", + "currency_code": "KPW" + }, + { + "country": "Northern Ireland", + "currency_code": "GBP" + }, + { + "country": "Northern Mariana Islands", + "currency_code": "USD" + }, + { + "country": "Norway", + "currency_code": "NOK" + }, + { + "country": "Oman", + "currency_code": "OMR" + }, + { + "country": "Pakistan", + "currency_code": "PKR" + }, + { + "country": "Palau", + "currency_code": "USD" + }, + { + "country": "Palestine", + "currency_code": null + }, + { + "country": "Panama", + "currency_code": "PAB" + }, + { + "country": "Papua New Guinea", + "currency_code": "PGK" + }, + { + "country": "Paraguay", + "currency_code": "PYG" + }, + { + "country": "Peru", + "currency_code": "PEN" + }, + { + "country": "Philippines", + "currency_code": "PHP" + }, + { + "country": "Pitcairn", + "currency_code": "NZD" + }, + { + "country": "Poland", + "currency_code": "PLN" + }, + { + "country": "Portugal", + "currency_code": "EUR" + }, + { + "country": "Puerto Rico", + "currency_code": "USD" + }, + { + "country": "Qatar", + "currency_code": "QAR" + }, + { + "country": "Reunion", + "currency_code": "EUR" + }, + { + "country": "Romania", + "currency_code": "RON" + }, + { + "country": "Russian Federation", + "currency_code": "RUB" + }, + { + "country": "Rwanda", + "currency_code": "RWF" + }, + { + "country": "Saint Helena", + "currency_code": "SHP" + }, + { + "country": "Saint Kitts and Nevis", + "currency_code": "XCD" + }, + { + "country": "Saint Lucia", + "currency_code": "XCD" + }, + { + "country": "Saint Pierre and Miquelon", + "currency_code": "EUR" + }, + { + "country": "Saint Vincent and the Grenadines", + "currency_code": "XCD" + }, + { + "country": "Samoa", + "currency_code": "WST" + }, + { + "country": "San Marino", + "currency_code": "EUR" + }, + { + "country": "Sao Tome and Principe", + "currency_code": "STD" + }, + { + "country": "Saudi Arabia", + "currency_code": "SAR" + }, + { + "country": "Scotland", + "currency_code": "GBP" + }, + { + "country": "Senegal", + "currency_code": "XOF" + }, + { + "country": "Seychelles", + "currency_code": "SCR" + }, + { + "country": "Sierra Leone", + "currency_code": "SLL" + }, + { + "country": "Singapore", + "currency_code": "SGD" + }, + { + "country": "Slovakia", + "currency_code": "EUR" + }, + { + "country": "Slovenia", + "currency_code": "EUR" + }, + { + "country": "Solomon Islands", + "currency_code": "SBD" + }, + { + "country": "Somalia", + "currency_code": "SOS" + }, + { + "country": "South Africa", + "currency_code": "ZAR" + }, + { + "country": "South Georgia and the South Sandwich Islands", + "currency_code": "GBP" + }, + { + "country": "South Korea", + "currency_code": "KRW" + }, + { + "country": "South Sudan", + "currency_code": "SSP" + }, + { + "country": "Spain", + "currency_code": "EUR" + }, + { + "country": "SriLanka", + "currency_code": "LKR" + }, + { + "country": "Sudan", + "currency_code": "SDG" + }, + { + "country": "Suriname", + "currency_code": "SRD" + }, + { + "country": "Svalbard and Jan Mayen", + "currency_code": "NOK" + }, + { + "country": "Swaziland", + "currency_code": "SZL" + }, + { + "country": "Sweden", + "currency_code": "SEK" + }, + { + "country": "Switzerland", + "currency_code": "CHF" + }, + { + "country": "Syria", + "currency_code": "SYP" + }, + { + "country": "Tajikistan", + "currency_code": "TJS" + }, + { + "country": "Tanzania", + "currency_code": "TZS" + }, + { + "country": "Thailand", + "currency_code": "THB" + }, + { + "country": "The Democratic Republic of Congo", + "currency_code": "CDF" + }, + { + "country": "Togo", + "currency_code": "XOF" + }, + { + "country": "Tokelau", + "currency_code": "NZD" + }, + { + "country": "Tonga", + "currency_code": "TOP" + }, + { + "country": "Trinidad and Tobago", + "currency_code": "TTD" + }, + { + "country": "Tunisia", + "currency_code": "TND" + }, + { + "country": "Turkey", + "currency_code": "TRY" + }, + { + "country": "Turkmenistan", + "currency_code": "TMT" + }, + { + "country": "Turks and Caicos Islands", + "currency_code": "USD" + }, + { + "country": "Tuvalu", + "currency_code": "AUD" + }, + { + "country": "Uganda", + "currency_code": "UGX" + }, + { + "country": "Ukraine", + "currency_code": "UAH" + }, + { + "country": "United Arab Emirates", + "currency_code": "AED" + }, + { + "country": "United Kingdom", + "currency_code": "GBP" + }, + { + "country": "United States", + "currency_code": "USD" + }, + { + "country": "United States Minor Outlying Islands", + "currency_code": "USD" + }, + { + "country": "Uruguay", + "currency_code": "UYU" + }, + { + "country": "Uzbekistan", + "currency_code": "UZS" + }, + { + "country": "Vanuatu", + "currency_code": "VUV" + }, + { + "country": "Venezuela", + "currency_code": "VEF" + }, + { + "country": "Vietnam", + "currency_code": "VND" + }, + { + "country": "Virgin Islands, British", + "currency_code": "USD" + }, + { + "country": "Virgin Islands, U.S.", + "currency_code": "USD" + }, + { + "country": "Wales", + "currency_code": "GBP" + }, + { + "country": "Wallis and Futuna", + "currency_code": "XPF" + }, + { + "country": "Western Sahara", + "currency_code": "MAD" + }, + { + "country": "Yemen", + "currency_code": "YER" + }, + { + "country": "Yugoslavia", + "currency_code": null + }, + { + "country": "Zambia", + "currency_code": "ZMW" + }, + { + "country": "Zimbabwe", + "currency_code": "ZWD" + } +] From 2ed8a53f3db3b5eafb3d0d8d0d4ac26d84f6526b Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:38:50 +0000 Subject: [PATCH 5/8] Move logic into dedicated functions --- bumblebee/modules/currency.py | 43 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 5876acd..526821c 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -36,6 +36,26 @@ DEFAULT_SRC_FALLBACK = "GBP" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" + +def get_local_country(): + r = requests.get('https://ipvigilante.com/') + location = r.json() + return location['data']['country_name'] + + +def load_country_to_currency(): + fname = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + 'data', 'country-by-currency-code.json') + with open(fname, 'r') as f: + data = json.load(f) + country2curr = {} + for dt in data: + country2curr[dt['country']] = dt['currency_code'] + + return country2curr + + class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, @@ -84,25 +104,10 @@ class Module(bumblebee.engine.Module): def find_local_currency(self): '''Use geolocation lookup to find local currency''' try: - r = requests.get('https://ipvigilante.com/') - if not r.ok: - return DEFAULT_SRC_FALLBACK - dt = r.json() - if dt['status'] != 'success': - return DEFAULT_SRC_FALLBACK - country = dt['data']['country_name'] - - fname = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - 'data', 'country-by-currency-code.json') - with open(fname, 'r') as f: - data = json.load(f) - country2curr = {} - for dt in data: - country2curr[dt['country']] = dt['currency_code'] - - return country2curr.get(country, DEFAULT_SRC) - except Exception as e: + country = get_local_country() + currency_map = load_country_to_currency() + return currency_map.get(country, DEFAULT_SRC_FALLBACK) + except: return DEFAULT_SRC_FALLBACK def fmt_rate(self, rate): From b6a0cb9e6f32d1e0b90217dbe777ff6aab4d2317 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 13:41:17 +0000 Subject: [PATCH 6/8] Move location URL to params --- bumblebee/modules/currency.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 526821c..4e2e7c2 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -35,10 +35,11 @@ DEFAULT_SRC = "auto" DEFAULT_SRC_FALLBACK = "GBP" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" +LOCATION_URL = "https://ipvigilante.com/" def get_local_country(): - r = requests.get('https://ipvigilante.com/') + r = requests.get(LOCATION_URL) location = r.json() return location['data']['country_name'] From 3e10dca932bd8f44e15ded569aa4198fd56eb33a Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 14:06:04 +0000 Subject: [PATCH 7/8] Automatically add local currency to destination --- bumblebee/modules/currency.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 4e2e7c2..24b507a 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -73,10 +73,14 @@ class Module(bumblebee.engine.Module): else: self._base = src - dest = [d for d in self.parameter("destination", DEFAULT_DEST).split(",") - if d != self._base] - self._symbols = dest - + self._symbols = [] + for d in self.parameter("destination", DEFAULT_DEST).split(","): + if d == 'auto': + new = self.find_local_currency() + else: + new = d + if new != self._base: + self._symbols.append(new) def price(self, widget): if len(self._data) == 0: From 81f7bf50fc87916a42680eba0b686b2685f6a081 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Sun, 24 Nov 2019 14:06:58 +0000 Subject: [PATCH 8/8] Use babel for formatting numbers, if available --- bumblebee/modules/currency.py | 36 ++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 24b507a..dd4f1ce 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -24,15 +24,18 @@ try: import requests except ImportError: pass +try: + from babel.numbers import format_currency +except ImportError: + format_currency = None import json import os SYMBOL = { "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩" } -DEFAULT_DEST = "USD,EUR,GBP" -DEFAULT_SRC = "auto" -DEFAULT_SRC_FALLBACK = "GBP" +DEFAULT_DEST = "USD,EUR,auto" +DEFAULT_SRC = "GBP" API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" LOCATION_URL = "https://ipvigilante.com/" @@ -88,13 +91,22 @@ class Module(bumblebee.engine.Module): rates = [] for sym, rate in self._data: - rate = self.fmt_rate(rate) - rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) + rate_float = float(rate.replace(',','')) + if format_currency: + rates.append(format_currency(rate_float, sym)) + else: + rate = self.fmt_rate(rate) + rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) - basefmt = u"{}".format(self.parameter("sourceformat", "1{}={}")) + basefmt = u"{}".format(self.parameter("sourceformat", "{}={}")) ratefmt = u"{}".format(self.parameter("destinationdelimiter", "=")) - return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates)) + if format_currency: + base_val = format_currency(1, self._base) + else: + base_val = '1{}'.format(SYMBOL[self._base] if self._base in SYMBOL else self._base) + + return basefmt.format(base_val, ratefmt.join(rates)) def update(self, widgets): self._data = [] @@ -111,15 +123,17 @@ class Module(bumblebee.engine.Module): try: country = get_local_country() currency_map = load_country_to_currency() - return currency_map.get(country, DEFAULT_SRC_FALLBACK) + return currency_map.get(country, DEFAULT_SRC) except: - return DEFAULT_SRC_FALLBACK + return DEFAULT_SRC def fmt_rate(self, rate): float_rate = float(rate.replace(',', '')) if not 0.01 < float_rate < 100: - return rate + ret = rate else: - return "%.3g" % float_rate + ret = "%.3g" % float_rate + + return ret # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4