Skip to content

Commit

Permalink
fix air pollution data
Browse files Browse the repository at this point in the history
  • Loading branch information
amit9838 committed Feb 11, 2024
1 parent 9d9662a commit b06338f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/backendAirPollution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class AirPollution:
@staticmethod
def current_air_pollution(latitude: float, longitude: float, **kwargs):
url = base_url + f"?latitude={latitude}&longitude={longitude}"
if "current" in kwargs:
current_fields = ",".join(kwargs.get("current"))
url = url + f"&current={current_fields}"
if "hourly" in kwargs:
hourly_fields = ",".join(kwargs.get("hourly"))
url = url + f"&hourly={hourly_fields}"

try:
url = url + f"&timeformat=unixtime"
url = url + f"&timeformat=unixtime" + f"&forecast_days=1"
response = requests.get(url)
response.raise_for_status() # Raise an exception if the request was unsuccessful
data = response.json()
Expand All @@ -22,7 +22,7 @@ def current_air_pollution(latitude: float, longitude: float, **kwargs):
print(f"Error: {e}")

def _get_current_air_pollution(self, lat, lon):
current_args = [
hourly_args = [
"european_aqi",
"us_aqi",
"pm10",
Expand All @@ -34,4 +34,4 @@ def _get_current_air_pollution(self, lat, lon):
"ammonia",
]

return self.current_air_pollution(lat, lon, current=current_args)
return self.current_air_pollution(lat, lon, hourly=hourly_args)
24 changes: 19 additions & 5 deletions src/frontendCardAirPollution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gi
import time

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
Expand All @@ -9,13 +10,26 @@

class CardAirPollution:
def __init__(self):
from .weatherData import air_apllution_data
from .weatherData import air_apllution_data,classify_aqi

self.poll_data = air_apllution_data
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'])):
if (
abs(time.time() - self.air_apllution_data['hourly']['time'][i]) // 60
) < 30:
nearest_current_time_idx = i
break

return nearest_current_time_idx

def create_card(self):
idx = self._get_nearest_time_index()

card = Gtk.Grid(margin_top=10, margin_start=5)
self.card = card
card.halign = Gtk.Align.FILL
Expand All @@ -37,21 +51,21 @@ def create_card(self):
info_box.set_margin_start(10)
info_box.set_margin_top(30)

main_val = Gtk.Label(label=self.poll_data["current"]["us_aqi"])
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.poll_data["level"])
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.poll_data["current"]["us_aqi"]
aqi = self.air_apllution_data['hourly']['us_aqi'][idx]
bar_level = aqi/600

pollution_bar = PollutionBar(bar_level)
Expand Down
4 changes: 0 additions & 4 deletions src/weatherData.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ def fetch_current_air_pollution():
global air_apllution_data
obj = AirPollution()
air_apllution_data = obj._get_current_air_pollution(*get_cords())
if air_apllution_data is not None:
air_apllution_data["level"] = classify_aqi(
air_apllution_data["current"]["us_aqi"]
)
return air_apllution_data


Expand Down

0 comments on commit b06338f

Please sign in to comment.