2019-04-28 09:50:35 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-04-28 11:11:23 +00:00
|
|
|
URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json"
|
2019-04-28 09:50:35 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-04-28 11:11:23 +00:00
|
|
|
def on_load(self):
|
|
|
|
self.exports.add("set", {"setting": "location",
|
2019-05-07 09:59:24 +00:00
|
|
|
"help": "Set your location", "validate": self._get_location})
|
2019-04-28 11:11:23 +00:00
|
|
|
|
|
|
|
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"]
|
2019-04-28 14:38:26 +00:00
|
|
|
lat = result["geometry"]["lat"]
|
|
|
|
lon = result["geometry"]["lng"]
|
2019-04-28 11:11:23 +00:00
|
|
|
|
2019-04-28 14:38:26 +00:00
|
|
|
return {"timezone": timezone, "lat": lat, "lon": lon}
|