-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyWeather.py
executable file
·77 lines (58 loc) · 2 KB
/
MyWeather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class WeatherClass:
class ForecastClass:
maxtemp = ''
mintemp = ''
condition = ''
def __init__(self, jsonData):
self.mintemp = jsonData['forecastday'][0]['day']['mintemp_c']
self.maxtemp = jsonData['forecastday'][0]['day']['maxtemp_c']
self.condition = jsonData['forecastday'][0]['day']['condition']['text']
city = ''
region = ''
country = ''
localtime = ''
currenttemp = ''
condition = ''
humidity = ''
cloud = ''
forecast = None
def __init__(self, jsonData):
self.city = jsonData['location']['name']
self.region = jsonData['location']['region']
self.country = jsonData['location']['country']
self.localtime = jsonData['location']['localtime']
self.currenttemp = jsonData['current']['temp_c']
self.condition = jsonData['current']['condition']['text']
self.humidity = jsonData['current']['humidity']
self.cloud = jsonData['current']['cloud']
jsonForecastData = jsonData['forecast']
self.forecast = self.ForecastClass(jsonForecastData)
import requests
import json
import os
import time
key = os.getenv("WEATHER_KEY")
def getWeather(local):
time.sleep(2)
request = requests.get(f'http://api.weatherapi.com/v1/forecast.json?key={key}={local}&days=1')
weatherJson = request.json()
return WeatherClass(weatherJson)
def getIconUrl(condition):
condition = condition.lower()
id = '01d'
if(condition.find('cloud') != -1):
id = '02d'
elif(condition.find('rain') != -1):
if(condition.find('heavy') != -1):
id = '09d'
else:
id= '10d'
elif(condition.find('clear') != -1):
id= '01d'
elif(condition.find('snow') != -1):
id= '13d'
elif(condition.find('thunderstorm') != -1):
id= '11d'
elif(condition.find('') != -1):
id= '11d'
return f"http://openweathermap.org/img/wn/{id}@4x.png"