-
Notifications
You must be signed in to change notification settings - Fork 20
/
weather.py
47 lines (40 loc) · 1.4 KB
/
weather.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
import requests
import json
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def weathernews():
loc_url = "http://api.ipstack.com/check?access_key="
geo_req = requests.get(loc_url)
geo_json = json.loads(geo_req.text)
api_key = ""
url = "http://api.openweathermap.org/data/2.5/weather?"
city_name = geo_json['city']
complete_url = url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
weather_details = {}
if x["cod"] != "404":
y = x["main"]
current_temperature = y["temp"]
current_pressure = y["pressure"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
# weather_details.append(city_name)
weather_details["Temperature"] = str(int(current_temperature)-273.15)
weather_details["Pressure"] = current_pressure
weather_details["Humidity"] = current_humidiy
weather_details["Summary"] = weather_description
for key, value in weather_details.items():
speak(key)
speak(value)
speak('That is all for todays weather, have a good day!')
else:
speak('Unable to fetch todays weather')
if __name__ == '__main__':
weathernews()