diff options
Diffstat (limited to 'actions/location.py')
-rw-r--r-- | actions/location.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/actions/location.py b/actions/location.py new file mode 100644 index 0000000..4eee869 --- /dev/null +++ b/actions/location.py @@ -0,0 +1,22 @@ +import urllib +from urllib.request import urlopen, Request +import ssl +import json + + +# noinspection PyUnresolvedReferences +def _action_location(_input): + try: + ctx = ssl.create_default_context() + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + + ip = urlopen(Request("https://ip.me", headers={"User-Agent": "curl/0.0.0"}), context=ctx).read().decode("utf-8")\ + .strip() + location = json.loads(urlopen(Request("https://ipinfo.io/" + ip + "/json", headers={"User-Agent": "curl/0.0.0"}), + context=ctx).read().decode("utf-8").strip()) + say(f"Vous êtes à {location['city']}, {location['region']}") + except urllib.error.URLError: + say("Désolé, je ne parviens pas à accéder à Internet en ce moment. Vérifiez que votre appareil dispose d'une" + "connexion Internet stable et réessayez.") + return |