aboutsummaryrefslogtreecommitdiff
path: root/actions/location.py
blob: 4eee869529c7fbde2f2140b55dec0aeab0a1f700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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