Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
amit9838 committed Feb 11, 2024
1 parent b06338f commit a3b4505
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
19 changes: 11 additions & 8 deletions src/frontendCardAirPollution.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

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
self.card = None
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
Expand Down Expand Up @@ -51,23 +52,25 @@ 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)
desc.set_halign(Gtk.Align.START)
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)
35 changes: 19 additions & 16 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
current_weather_data = None
air_pollution_data = None
forecast_weather_data = None
epoch_offset = None


def check_internet_connection():
Expand Down Expand Up @@ -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
15 changes: 8 additions & 7 deletions src/weatherData.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
air_apllution_data = None



def fetch_current_weather():
global current_weather_data
# Get current weather data from api
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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}

0 comments on commit a3b4505

Please sign in to comment.