aboutsummaryrefslogtreecommitdiff
path: root/modules/location.py
diff options
context:
space:
mode:
Diffstat (limited to 'modules/location.py')
-rw-r--r--modules/location.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/modules/location.py b/modules/location.py
index e4f769aa..85e147da 100644
--- a/modules/location.py
+++ b/modules/location.py
@@ -1,16 +1,25 @@
from src import ModuleManager, utils
import pytz
-_lower_timezones = {}
-for tz in pytz.all_timezones:
- if "/" in tz:
- _lower_timezones[tz.split("/", 1)[1].lower()] = tz
- _lower_timezones[tz.lower()] = tz
+URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json"
-def _find_tz(s):
- return _lower_timezones.get(s.lower(), None)
-
-@utils.export("set", {"setting": "location", "help": "Set your location",
- "validate": _find_tz})
class Module(ModuleManager.BaseModule):
- pass
+ def on_load(self):
+ self.exports.add("set", {"setting": "location",
+ "help": "Set your location", "validate": self._get_location,
+ "human": lambda x: "%s, %s" % (x["city"], x["country"])})
+
+ def _get_location(self, s):
+ page = utils.http.request(URL_OPENCAGE, get_params={
+ "q": s, "key": self.bot.config["opencagedata-api-key"], "limit": "1"
+ }, json=True)
+ if page and page.data["results"]:
+ result = page.data["results"][0]
+ timezone = result["annotations"]["timezone"]["name"]
+ continent = result["components"]["continent"]
+ country = result["components"]["country"]
+ city = result["components"]["city"]
+
+ print("yes")
+ return {"timezone": timezone, "continent": continent,
+ "country": country, "city": city}