Skip to content

Commit

Permalink
optimize app launch, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Chaudhary committed Jun 8, 2024
1 parent 311b655 commit 0a3f95a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
9 changes: 7 additions & 2 deletions src/mousam.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


# module import
from .utils import create_toast, check_internet_connection
from .utils import create_toast, check_internet_connection, get_time_difference
from .constants import bg_css
from .windowAbout import AboutWindow
from .windowPreferences import WeatherPreferences
Expand Down Expand Up @@ -219,9 +219,14 @@ def _load_weather_data(self):
apd = threading.Thread(target=fetch_current_air_pollution, name="apt")
apd.start()

apd.join()
lat,lon = settings.selected_city.split(",")
local_time = threading.Thread(target=get_time_difference,args=(lat,lon, True), name="local_time")
local_time.start()

hfd.join()
dfd.join()
apd.join()
local_time.join()
self.get_weather()

# =========== Load weather data and create UI ============
Expand Down
63 changes: 6 additions & 57 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import requests
import socket
import json
from datetime import datetime, timedelta, timezone
from datetime import datetime
import time
from gi.repository import Adw
from .config import settings

current_weather_data = None
air_pollution_data = None
forecast_weather_data = None
epoch_offset = None

global local_time_data
local_time_data = dict()

TIMEOUT = 5
domains = {
"google": "http://www.google.com",
Expand Down Expand Up @@ -55,74 +47,31 @@ def check_internet_connection():
return False


def get_selected_city_coords():
selected_city = int(str(settings.selected_city))
added_cities = list(settings.added_cities)
city_loc = added_cities[selected_city].split(",")
return city_loc[-2], city_loc[-1] # latitude,longitude


def create_toast(text, priority=0):
toast = Adw.Toast.new(text)
toast.set_priority(Adw.ToastPriority(priority))
return toast


def convert_to_local_time(timestamp, timezone_stamp):
hour_offset_from_utc = (timezone_stamp) / 3600
return datetime.fromtimestamp(timestamp, tz=timezone.utc) + timedelta(
hours=hour_offset_from_utc
)


def get_cords():
selected_city_ = settings.selected_city
return [float(x) for x in selected_city_.split(",")]


def get_tz_offset_by_cord(lat, lon):
global epoch_offset

if epoch_offset is None:
url = f"https://api.geotimezone.com/public/timezone?latitude={lat}&longitude={lon}"

res = requests.get(url)
if res.status_code != 200:
return 0

res = json.loads(res.text)
if res.get("offset") is None:
return 0

offset_arr = res.get("offset")[3:].split(":")
offset_arr = [int(x) for x in offset_arr]
epoch_hr = abs(offset_arr[0]) * 3600
epoch_s = 0

if len(offset_arr) > 1:
epoch_s = offset_arr[1] * 60

epoch_offset = epoch_hr + epoch_s
if offset_arr[0] < 0:
epoch_offset *= -1

return epoch_offset


def get_time_difference(target_latitude, target_longitude):
def get_time_difference(target_latitude, target_longitude, force=False):
global local_time_data

cord_str = f"{target_latitude}_{target_longitude}"
if local_time_data.get(cord_str) is not None:
if force is False and local_time_data.get(cord_str) is not None:
return local_time_data[cord_str]

# Get current time in the target location using timeapi.io
url = f"https://timeapi.io/api/Time/current/coordinate?latitude={target_latitude}&longitude={target_longitude}"
target_time_response = requests.get(url)
target_time_data = target_time_response.json()
target_current_time = target_time_data["dateTime"]
target_time = datetime.strptime(target_current_time[:26], "%Y-%m-%dT%H:%M:%S.%f")

epoch_diff = time.time() - target_time.timestamp()
data = {"epoch_diff": epoch_diff, "target_time": target_time.timestamp()}
local_time_data[cord_str] = data
Expand Down

0 comments on commit 0a3f95a

Please sign in to comment.