-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherClass.py
182 lines (156 loc) · 8.53 KB
/
weatherClass.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import requests
import pandas as pd
import json
degree_sign= u'\N{DEGREE SIGN}' # degree sign
class forecastByCity(object):
def __init__(self, location ,count):
openWeatherAPI_key = {API-TOKEN}
openForecastUrl= "http://api.openweathermap.org/data/2.5/forecast?"
if (location.find("@#$") == -1):
openForecastByCity = openForecastUrl + "q=" + location + "&APPID=" + openWeatherAPI_key
else:
locationSplit = location.split("@#$")
latlonConcatenate = "lat=" + locationSplit[0] + "&lon=" + locationSplit[1]
openForecastByCity = openForecastUrl + latlonConcatenate + "&APPID=" + openWeatherAPI_key
forecastData = requests.get(openForecastByCity)
if (forecastData.status_code == 200):
self.forecastJson = forecastData.json()
self.forecastCode = self.__forecastCode(count)
emojiClass = emoji(self.forecastCode)
self.weatherEmoji = emojiClass.smiley
self.forecastDescription = str(self.__forecastDescription(count))
self.maxTemp = str(self.__maxTemp(count))
self.minTemp = str(self.__minTemp(count))
self.humidity= str(self.__humidity(count))
self.windSpeed = str(self.__windSpeed(count))
self.date = str(self.__date(count))
self.forecastCollection = "\nThe weather Report on Date : " + self.date + "\nWeather : " + self.forecastDescription + " " + self.weatherEmoji + "\n->Max/Min Temp : " + self.maxTemp + degree_sign + "C/" + self.minTemp + degree_sign + "C" + "\n-> Humidity : " + self.humidity + "\n-> Wind Speed : " + self.windSpeed + "\n"
elif(forecastData.status_code == 401):
self.forecastCollection = "Issue with API key"
elif(forecastData.status_code == 404):
self.forecastCollection = "City not found"
def __forecastCode(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
idTable = forecastDataFrame.iloc[count]["weather"]
IdFrame = pd.DataFrame(idTable)
return IdFrame.iloc[0]["id"]
def __forecastDescription(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
idTable = forecastDataFrame.iloc[count]["weather"]
IdFrame = pd.DataFrame(idTable)
return IdFrame.iloc[0]["description"]
def __currentTemp(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
currentTemp = (forecastDataFrame.iloc[count]["main"])
return (round(currentTemp["temp"] -273.15 , 2))
def __windSpeed(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
windSpeed = (forecastDataFrame.iloc[count]["wind"])
return windSpeed["speed"]
def __humidity(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
humidity = (forecastDataFrame.iloc[count]["main"])
return humidity["humidity"]
def __maxTemp(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
maxTemp = (forecastDataFrame.iloc[count]["main"])
return (round(maxTemp["temp_max"]-273.15 , 2))
def __minTemp(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
minTemp = (forecastDataFrame.iloc[count]["main"])
return (round(minTemp["temp_min"]-273.15 , 2))
def __date(self,count):
forecastTable = self.forecastJson["list"]
forecastDataFrame = pd.DataFrame(forecastTable)
date = (forecastDataFrame.iloc[count]["dt_txt"])
date = date.split(" ")
return date[0]
class currentWeatherByCity(object):
def __init__(self, location):
openWeatherAPI_key = "API_KEY"
openWeatherUrl= "http://api.openweathermap.org/data/2.5/weather?"
if (location.find("@#$") == -1):
openWeatherByCity = openWeatherUrl + "q=" + location + "&APPID=" + openWeatherAPI_key
else:
locationSplit = location.split("@#$")
latlonConcatenate = "lat=" + locationSplit[0] + "&lon=" + locationSplit[1]
openWeatherByCity = openWeatherUrl + latlonConcatenate + "&APPID=" + openWeatherAPI_key
weatherData = requests.get(openWeatherByCity)
if (weatherData.status_code == 200):
self.weatherJson = weatherData.json()
self.weatherCode = self.__weatherCode()
self.weatherDescription = self.__weatherDescription()
emojiClass = emoji(self.weatherCode)
self.weatherEmoji = emojiClass.smiley
self.currentTemp = str(self.__currentTemp())
self.maxTemp = str(self.__maxTemp())
self.minTemp = str(self.__minTemp())
self.humidity= str(self.__humidity())
self.windSpeed = str(self.__windSpeed())
self.weatherOutput = "Weather : " + self.weatherDescription + " " + self.weatherEmoji + "\n-> Current Temperature : " + self.currentTemp + degree_sign + "C" + "\n-> Max/Min Temperature : " + self.maxTemp + degree_sign + "C" + "/" + self.minTemp + degree_sign + "C" + "\n-> Humidity : " + self.humidity + "\n-> Wind Speed : " + self.windSpeed
elif(weatherData.status_code == 401):
self.weatherOutput = "Issue with API key"
elif(weatherData.status_code == 404):
self.weatherOutput = "City not found"
def __weatherCode(self):
weatherTable = self.weatherJson["weather"]
weatherDataFrame = pd.DataFrame(weatherTable)
return (weatherDataFrame.iloc[0]['id'])
def __weatherDescription(self):
weatherTable = self.weatherJson["weather"]
weatherDataFrame = pd.DataFrame(weatherTable)
return (weatherDataFrame.iloc[0]['description'])
def __currentTemp(self):
return (round(self.weatherJson['main']['temp']-273.15 , 2))
def __maxTemp(self):
return (round(self.weatherJson['main']['temp_max']-273.15 , 2))
def __minTemp(self):
return (round(self.weatherJson['main']['temp_min']-273.15 , 2))
def __humidity(self):
return (self.weatherJson['main']['humidity'])
def __windSpeed(self):
return (self.weatherJson['wind']['speed'])
class emoji(object):
def __init__(self, weatherCode):
self.smiley = self.__getSmiley(weatherCode)
def __getSmiley(self,weatherCode):
# Unicodes for Emoji for respective weather code
thunderstorm = u'\U0001F4A8' # Code: 200's, 900, 901, 902, 905
drizzle = u'\U0001F4A7' # Code: 300's
rain = u'\U00002614' # Code: 500's
snowflake = u'\U00002744' # Code: 600's snowflake
snowman = u'\U000026C4' # Code: 600's snowman, 903, 906
atmosphere = u'\U0001F301' # Code: 700's foogy
clearSky = u'\U00002600' # Code: 800 clear sky
fewClouds = u'\U000026C5' # Code: 801 sun behind clouds
clouds = u'\U00002601' # Code: 802-803-804 clouds general
hot = u'\U0001F525' # Code: 904
defaultEmoji = u'\U0001F300' # default emojis
if (weatherCode):
if str(weatherCode)[0] == '2' or weatherCode == 900 or weatherCode==901 or weatherCode==902 or weatherCode==905:
return thunderstorm
elif str(weatherCode)[0] == '3':
return drizzle
elif str(weatherCode)[0] == '5':
return rain
elif str(weatherCode)[0] == '6' or weatherCode==903 or weatherCode== 906:
return snowflake + ' ' + snowman
elif str(weatherCode)[0] == '7':
return atmosphere
elif weatherCode == 800:
return clearSky
elif weatherCode == 801:
return fewClouds
elif weatherCode==802 or weatherCode==803 or weatherCode==803:
return clouds
elif weatherCode == 904:
return hot
else:
return defaultEmoji