Advertisement
peachyontop

htrhrwsthsrethg

Apr 5th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. from kivy.clock import mainthread
  2. from kivymd.app import MDApp
  3. from kivymd.uix.screen import Screen
  4. from plyer import gps, notification
  5. import requests
  6. import json
  7. from datetime import datetime
  8.  
  9. class WeatherScreen(Screen):
  10. api_key = "YOUR_API_KEY" # Get from openweathermap.org
  11.  
  12. def on_enter(self):
  13. self.start_gps()
  14.  
  15. def start_gps(self):
  16. try:
  17. gps.configure(on_location=self.on_location, on_status=self.on_status)
  18. gps.start(minTime=5000, minDistance=0)
  19. except NotImplementedError:
  20. notification.notify(title="GPS Error", message="GPS not supported")
  21.  
  22. def on_status(self, stype, status):
  23. if status == 'provider-enabled':
  24. self.start_gps()
  25.  
  26. def on_location(self, **kwargs):
  27. lat = kwargs.get('lat')
  28. lon = kwargs.get('lon')
  29. if lat and lon:
  30. self.get_weather(lat, lon)
  31.  
  32. def get_weather(self, lat, lon):
  33. url = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={self.api_key}&units=metric"
  34. try:
  35. response = requests.get(url)
  36. data = json.loads(response.text)
  37. self.update_ui(data)
  38. except Exception as e:
  39. notification.notify(title="Weather Error", message=str(e))
  40.  
  41. @mainthread
  42. def update_ui(self, data):
  43. # Update main weather card
  44. self.ids.location_label.text = f"{data['name']}, {data['sys']['country']}"
  45. self.ids.temp_label.text = f"{round(data['main']['temp'])}°C"
  46. self.ids.update_label.text = f"Last update: {datetime.now().strftime('%H:%M:%S')}"
  47.  
  48. # Update weather details
  49. self.ids.weather_icon.text = self.get_weather_icon(data['weather'][0]['id'])
  50. self.update_detail('💨', f"{data['wind']['speed']} m/s", "Wind")
  51. self.update_detail('💧', f"{data['main']['humidity']}%", "Humidity")
  52. self.update_detail('🌧️', f"{data.get('rain', {}).get('1h', 0)} mm", "Rain")
  53.  
  54. def update_detail(self, icon, value, label):
  55. container = self.ids.details_card.children[0]
  56. for child in container.children:
  57. if child.label == label:
  58. child.ids.icon_label.text = icon
  59. child.ids.value_label.text = value
  60. break
  61.  
  62. def get_weather_icon(self, code):
  63. if 200 <= code < 300: return "⛈️"
  64. if 300 <= code < 400: return "🌧️"
  65. if 500 <= code < 600: return "🌧️"
  66. if 600 <= code < 700: return "❄️"
  67. if 700 <= code < 800: return "🌫️"
  68. if code == 800: return "☀️"
  69. if 801 <= code < 900: return "⛅"
  70. return "🌍"
  71.  
  72. def refresh(self):
  73. self.start_gps()
  74. self.ids.update_label.text = "Updating..."
  75.  
  76. def on_leave(self):
  77. gps.stop()
  78.  
  79. class WeatherApp(MDApp):
  80. def build(self):
  81. self.theme_cls.primary_palette = "Blue"
  82. return WeatherScreen()
  83.  
  84. if __name__ == '__main__':
  85. WeatherApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement