From a3b4505b5fe3feca4910f5b33b17826fcd303d78 Mon Sep 17 00:00:00 2001 From: Amit Chaudhary Date: Sun, 11 Feb 2024 17:32:45 +0530 Subject: [PATCH] format code --- src/frontendCardAirPollution.py | 19 ++++++++++-------- src/utils.py | 35 ++++++++++++++++++--------------- src/weatherData.py | 15 +++++++------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/frontendCardAirPollution.py b/src/frontendCardAirPollution.py index c1fa700..097e092 100644 --- a/src/frontendCardAirPollution.py +++ b/src/frontendCardAirPollution.py @@ -10,7 +10,7 @@ class CardAirPollution: def __init__(self): - from .weatherData import air_apllution_data,classify_aqi + from .weatherData import air_apllution_data, classify_aqi self.air_apllution_data = air_apllution_data self.classify_aqi = classify_aqi @@ -18,9 +18,10 @@ def __init__(self): self.create_card() def _get_nearest_time_index(self): - for i in range(len(self.air_apllution_data['hourly']['time'])): + + for i in range(len(self.air_apllution_data["hourly"]["time"])): if ( - abs(time.time() - self.air_apllution_data['hourly']['time'][i]) // 60 + abs(time.time() - self.air_apllution_data["hourly"]["time"][i]) // 60 ) < 30: nearest_current_time_idx = i break @@ -51,13 +52,15 @@ def create_card(self): info_box.set_margin_start(10) info_box.set_margin_top(30) - main_val = Gtk.Label(label=self.air_apllution_data['hourly']['us_aqi'][idx]) + main_val = Gtk.Label(label=self.air_apllution_data["hourly"]["us_aqi"][idx]) main_val.set_css_classes(["text-l3", "bold"]) main_val.set_halign(Gtk.Align.START) main_val.set_margin_end(10) info_box.append(main_val) - desc = Gtk.Label(label=self.classify_aqi(self.air_apllution_data['hourly']['us_aqi'][idx])) + desc = Gtk.Label( + label=self.classify_aqi(self.air_apllution_data["hourly"]["us_aqi"][idx]) + ) desc.set_css_classes(["text-3", "light-2", "bold-2"]) desc.set_margin_bottom(10) desc.set_valign(Gtk.Align.END) @@ -65,9 +68,9 @@ def create_card(self): info_box.append(desc) # Pollution bar - aqi = self.air_apllution_data['hourly']['us_aqi'][idx] - bar_level = aqi/600 - + aqi = self.air_apllution_data["hourly"]["us_aqi"][idx] + bar_level = aqi / 600 + pollution_bar = PollutionBar(bar_level) # pollution_bar.set_margin_top() card.attach(pollution_bar, 0, 4, 4, 1) diff --git a/src/utils.py b/src/utils.py index 618a69f..78ca912 100644 --- a/src/utils.py +++ b/src/utils.py @@ -7,6 +7,7 @@ current_weather_data = None air_pollution_data = None forecast_weather_data = None +epoch_offset = None def check_internet_connection(): @@ -58,27 +59,29 @@ def get_my_tz_offset_from_utc(): def get_tz_offset_by_cord(lat, lon): - url = f"https://api.geotimezone.com/public/timezone?latitude={lat}&longitude={lon}" + global epoch_offset - res = requests.get(url) - if res.status_code != 200: - return 0 + if epoch_offset is None: + url = f"https://api.geotimezone.com/public/timezone?latitude={lat}&longitude={lon}" - res = json.loads(res.text) - if res.get("offset") is None: - return 0 + res = requests.get(url) + if res.status_code != 200: + 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 + res = json.loads(res.text) + if res.get("offset") is None: + return 0 - if len(offset_arr) > 1: - epoch_s = offset_arr[1] * 60 + 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 - epoch_offset = epoch_hr + epoch_s + if len(offset_arr) > 1: + epoch_s = offset_arr[1] * 60 - if offset_arr[0] < 0: - epoch_offset *= -1 + epoch_offset = epoch_hr + epoch_s + if offset_arr[0] < 0: + epoch_offset *= -1 return epoch_offset diff --git a/src/weatherData.py b/src/weatherData.py index ea8cdb6..6591542 100644 --- a/src/weatherData.py +++ b/src/weatherData.py @@ -15,7 +15,6 @@ air_apllution_data = None - def fetch_current_weather(): global current_weather_data # Get current weather data from api @@ -48,7 +47,9 @@ def fetch_hourly_forecast(): # print(hourly_forecast_data.get("hourly").items()) nearest_current_time_idx = 0 for i in range(len(hourly_forecast_data.get("hourly").get("time"))): - if (abs(time.time() - hourly_forecast_data.get("hourly").get("time")[i]) // 60) < 30: + if ( + abs(time.time() - hourly_forecast_data.get("hourly").get("time")[i]) // 60 + ) < 30: nearest_current_time_idx = i break @@ -66,7 +67,7 @@ def fetch_hourly_forecast(): } current_weather_data.visibility = transform_visibility_data( hourly_forecast_data.visibility["unit"], - hourly_forecast_data.visibility["data"][0], + hourly_forecast_data.visibility["data"][nearest_current_time_idx], ) return hourly_forecast_data @@ -158,15 +159,15 @@ def classify_wind_speed_level(wind_speed): def transform_visibility_data(unit, data): settings = Gio.Settings(schema_id="io.github.amit9838.mousam") - measurement_type = settings.get_string('measure-type') + measurement_type = settings.get_string("measure-type") dist_unit = "km" - dist = data/1000 + dist = data / 1000 if measurement_type == "imperial": dist_unit = "miles" - dist = data/1609.34 + dist = data / 1609.34 if unit.lower() == "m": - data=dist + data = dist unit = dist_unit return {"unit": unit, "data": data}