11import discord
2+ import requests
23from discord .ext import commands
3- from pyowm .owm import OWM
4- from pyowm .utils .config import get_default_config
5- from pyowm .commons .exceptions import NotFoundError
64
7- config_dict = get_default_config ()
8- config_dict ['language' ] = 'de'
9- owm = OWM ('18bca685e2b3a0f410b5f71a66a8a621' , config_dict )
10- mgr = owm .weather_manager ()
11- mgg = owm .geocoding_manager ()
5+ api_key = open ("data.txt" , "r" ).readlines ()[17 ]
6+ units = 'metric'
7+ lang = 'de'
128
139
14- #observation = mgr.weather_at_place('Paris, FR')
15-
1610def decoder (ort ):
17- ruckgabe = []
18- list_of_locations = mgg .geocode (ort )
19- gps = list_of_locations [0 ]
20- ruckgabe .append (gps .lon )
21- ruckgabe .append (gps .lat )
22- return ruckgabe
11+ api_data = requests .get (f'https://api.openweathermap.org/geo/1.0/direct?limit=5&q={ ort } &appid={ api_key } ' ).json ()
12+ return api_data [0 ]['lon' ], api_data [0 ]['lat' ]
2313
2414
2515def emoji_lookup (status ):
@@ -39,12 +29,16 @@ class Weather(commands.Cog):
3929 description = 'Frage Kiddo nach dem Wetter :)' )
4030 async def get_weather (self , ctx , location ):
4131 try :
42- observation = mgr .weather_at_place (location )
43- w = observation .weather
44- temp = w .temperature ('celsius' )['temp' ]
45- daten = w .detailed_status
46- daten_einfach = w .status
47- emoji = emoji_lookup (daten_einfach )
32+ gps = decoder (location )
33+
34+ api_data = requests .get (
35+ f'https://api.openweathermap.org/data/3.0/onecall?lat={ gps [1 ]} &lon={ gps [0 ]} &exclude=current,minutely,'
36+ f'daily,alerts&appid={ api_key } &units={ units } &lang={ lang } ' ).json ()
37+
38+ api_data = api_data ['hourly' ][0 ]
39+ temp = api_data ['temp' ]
40+ daten = api_data ['weather' ][0 ]['description' ]
41+ emoji = emoji_lookup (api_data ['weather' ][0 ]['main' ])
4842
4943 if temp <= 0 :
5044 color = 0x34c0eb
@@ -58,22 +52,23 @@ async def get_weather(self, ctx, location):
5852 embedVar .add_field (name = "**Temperatur**" , value = f'In { location } hat es gerade { temp } °C 🌡' , inline = False )
5953
6054 await ctx .send (embed = embedVar )
61- except NotFoundError :
62- await ctx .send (f'Ich konnte { location } nicht finden :(' )
6355 except IndexError :
64- await ctx .send ('Bitte gib einen Ort an :)' )
56+ await ctx .send ('Bitte gib einen echten Ort an :)' )
6557
6658 @commands .hybrid_command (name = "morgen" , aliases = ['Wettervorhersage' ], brief = 'Gibt die Wettervorhersage aus' ,
6759 description = 'Wie wird denn wohl das Wetter morgen?' )
6860 async def get_weather_forecast (self , ctx , location ):
69- gps = decoder (location )
7061 try :
71- one_call = mgr .one_call (lon = gps [0 ], lat = gps [1 ])
72- w = one_call .forecast_daily
73- temp = w [0 ].temperature ('celsius' )
74- daten = w [0 ].detailed_status
75- daten_einfach = w [0 ].status
76- emoji = emoji_lookup (daten_einfach )
62+ gps = decoder (location )
63+
64+ api_data = requests .get (
65+ f'https://api.openweathermap.org/data/3.0/onecall?lat={ gps [1 ]} &lon={ gps [0 ]} &exclude=current,minutely,'
66+ f'&appid={ api_key } &units={ units } &lang={ lang } ' ).json ()
67+
68+ api_data = api_data ['daily' ][1 ]
69+ temp = api_data ['temp' ]
70+ daten = api_data ['weather' ][0 ]['description' ]
71+ emoji = emoji_lookup (api_data ['weather' ][0 ]['main' ])
7772 if temp ["min" ] <= 0 :
7873 color = 0x34c0eb
7974 elif temp ["max" ] >= 30 :
@@ -88,32 +83,30 @@ async def get_weather_forecast(self, ctx, location):
8883
8984 await ctx .send (embed = embedVar )
9085
91- except NotFoundError :
92- await ctx .send (f'Ich konnte { location } nicht finden :(' )
9386 except IndexError :
94- await ctx .send ('Bitte gib einen Ort an :)' )
87+ await ctx .send ('Bitte gib einen echten Ort an :)' )
9588
9689 @commands .hybrid_command (name = "alarm" , aliases = ['Wetterwarnung' , 'warnung' ], brief = 'Gibt die Wetterwarnung aus' ,
9790 description = 'Frage nach, ob es in deiner Umgebung gerade eine Wetterwarnung gibt' )
9891 async def get_weather_alert (self , ctx , location ):
9992 try :
10093 gps = decoder (location )
10194
102- one_call = mgr .one_call (lon = gps [0 ], lat = gps [1 ])
95+ api_data = (requests .get (f'https://api.openweathermap.org/data/3.0/onecall?lat={ gps [1 ]} &lon={ gps [0 ]} '
96+ 'f&exclude=current,minutely,hourly,daily&appid={api_key}&units={units}'
97+ 'f&lang={lang}' ).json ())
98+
10399 try :
104- w = one_call .national_weather_alerts
105- daten = w [0 ].description
106100 embedVar = discord .Embed (title = "**Wetterwarnung**" , color = 0xff0000 )
107- embedVar .add_field (name = "**Warnung**" , value = f'In { location } gibt es eine Wetterwarnung: { daten } ' ,
108- inline = False )
109- except TypeError :
101+ embedVar .add_field (name = "**Warnung**" ,
102+ value = f'In { location } gibt es eine Wetterwarnung: '
103+ f'{ api_data ["alerts" ][0 ]["description" ]} ' , inline = False )
104+ except KeyError :
110105 embedVar = discord .Embed (title = "**Wetterwarnung**" , color = 0x36a822 )
111106 embedVar .add_field (name = "**Warnung**" , value = f'In { location } gibt es keine Wetterwarnung.' ,
112107 inline = False )
113108
114109 await ctx .send (embed = embedVar )
115110
116- except NotFoundError :
117- await ctx .send (f'Ich konnte { location } nicht finden :(' )
118- except AssertionError :
119- await ctx .send ('Bitte gib einen Ort an :)' )
111+ except IndexError :
112+ await ctx .send ('Bitte gib einen echten Ort an :)' )
0 commit comments