Skip to content

Commit

Permalink
Use old positioning method via ipinfo.io as a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
l0drex committed May 17, 2023
1 parent 63dd4c9 commit bb8b608
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions yin_yang/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from time import sleep
from typing import Union, Optional

import requests
from PySide6.QtCore import QObject
from PySide6.QtPositioning import QGeoPositionInfoSource, QGeoPositionInfo, QGeoCoordinate
from psutil import process_iter, NoSuchProcess
Expand Down Expand Up @@ -119,6 +120,7 @@ def get_sun_time(latitude, longitude) -> tuple[time, time]:
locationSource = QGeoPositionInfoSource.createDefaultSource(parent)


@cache
def get_current_location() -> QGeoCoordinate:
if locationSource is None:
logger.error("No location source is available")
Expand All @@ -134,8 +136,13 @@ def get_current_location() -> QGeoCoordinate:
sleep(1)
coordinate = pos.coordinate()
if not coordinate.isValid():
logger.error('Location could not be determined')
return QGeoCoordinate(0, 0)
logger.warning('Location could not be determined. Using ipinfo.io to get location')
# use the old method as a fallback
loc_response = requests.get('https://www.ipinfo.io/loc').text.split(',')
loc: [float] = [float(coordinate) for coordinate in loc_response]
assert len(loc) == 2, 'The returned location should have exactly 2 values.'
coordinate = QGeoCoordinate(loc[0], loc[1])
assert coordinate.isValid()
return coordinate


Expand Down

0 comments on commit bb8b608

Please sign in to comment.