-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
executable file
·59 lines (43 loc) · 1.38 KB
/
weather.py
File metadata and controls
executable file
·59 lines (43 loc) · 1.38 KB
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
import requests, json, math
import re
f = open('apiKey.txt', 'r') #reading apikey from file
key = f.readline()
key = re.compile(r'\W+', re.UNICODE).split(key)
key = key[0]
f.close()
api_key = key
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = "Granada" #by default
def setCity(city):
city_name = city
def createURL(base_url, city_name, api_key):
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
return complete_url
response = requests.get(createURL(base_url,city_name, api_key))
def getJSON(response):
# json method of response object convert json format data into python format data
response = requests.get(createURL(base_url,city_name, api_key))
JSONObject = response.json()
if JSONObject["cod"] != "404":
return JSONObject
else:
return None
def getWeatherTemperature(x):
x = getJSON(response)
y = x["main"]
current_temperature = y["temp"] #Temperature in Kelvin
return current_temperature
def convertToCelsius(Kelvin):
Celsius = Kelvin - 273.15
return math.ceil(Celsius)
def getWeatherPressure(x):
x = getJSON(response)
y = x["main"]
current_pressure = y["pressure"]
return current_pressure
def getWeatherHumidity(x):
x = getJSON(response)
y = x["main"]
current_humidity = y["humidity"]
return current_humidity