From 0f8967b9113d698cdeb2d05ca85d2d9a80461c24 Mon Sep 17 00:00:00 2001 From: Minteck Date: Sat, 9 Apr 2022 16:39:03 +0200 Subject: Commit --- actions/location.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 actions/location.py (limited to 'actions/location.py') 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 -- cgit