-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgps_coordinates.py
More file actions
36 lines (25 loc) · 946 Bytes
/
gps_coordinates.py
File metadata and controls
36 lines (25 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import NamedTuple
import requests
from exceptions import DontGetCoordinates
IPINFO_URL = 'https://ipinfo.io/json'
class Coordinates(NamedTuple):
latitude: float
longitude: float
def get_gps_coordinates() -> Coordinates:
"""Returns GPS coordinates by computer's ip-address"""
location_info = _get_location_info_by_ip()
coordinates = _parse_coordinates(location_info)
return coordinates
def _parse_coordinates(location_info: dict) -> Coordinates:
try:
latitude, longitude = location_info["loc"].split(",")
except Exception as exc:
raise DontGetCoordinates from exc
return Coordinates(latitude=latitude, longitude=longitude)
def _get_location_info_by_ip() -> dict:
response = requests.get(IPINFO_URL, timeout=200)
if response.status_code == 200:
return response.json()
raise DontGetCoordinates
if __name__ == "__main__":
print(get_gps_coordinates())