From c8ac752ca4341d1e969d0ba0a77841ff7f8ead0b Mon Sep 17 00:00:00 2001 From: Puranjay Savar Mattas <68191388+psavarmattas@users.noreply.github.com> Date: Wed, 16 Dec 2020 10:53:17 +0530 Subject: [PATCH] Added: Weather Support In this build I added the functionally to ask for the weather (only for New Delhi, India). --- main.py | 20 +++++++++++++++++- readme.md | 10 ++++++++- weather.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 weather.py diff --git a/main.py b/main.py index 6c506e6..80840f3 100644 --- a/main.py +++ b/main.py @@ -9,13 +9,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import speech_recognition as sr import pyttsx3 import pywhatkit import datetime import wikipedia import pyjokes - +import weather listener = sr.Recognizer() engine = pyttsx3.init() @@ -71,6 +72,23 @@ if __name__ == "__main__": elif 'joke' in command: talk('Let me get you laughing') talk(pyjokes.get_joke()) + elif 'weather' in command: + command = command.replace('weather', '') + if weather.response.status_code == 200: + talk('The Weather In:') + talk(weather.weather_city) + print(weather.weather_city) + talk(weather.weather_temperature) + print(weather.weather_temperature) + talk(weather.weather_humidity) + print(weather.weather_humidity) + talk(weather.weather_pressure) + print(weather.weather_pressure) + talk(weather.weather_report) + print(weather.weather_report) + else: + talk(weather.weather_error) + print(weather.weather_error) elif 'stop' in command: break else: diff --git a/readme.md b/readme.md index 7fdad72..a1bbbb9 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ resolved before opening an issues. 1. Open PyCharm & create a new project with your virtual environment ready. 2. Open the terminal and paste the following in sequential order line by line(Execute it one by one): -`pip install speechRecognition` +`pip install speechRecognistion` `pip install pyttsx3` @@ -53,3 +53,11 @@ _Install this if necessary (Only when the code gives error)_ 1. Learn all the above commands on terminal. 2. Make sure to use pip3, because in linux pip refers for python2 and pip3 refers to python3. 3. Install these too - `pip3 install pyAudio` + + +## Feature List: + +1. Play videos on YouTube. +2. Search wikipedia with your queries. +3. Listen to jokes. +4. Ask for the weather (only New Delhi,India available right now) \ No newline at end of file diff --git a/weather.py b/weather.py new file mode 100644 index 0000000..34c5156 --- /dev/null +++ b/weather.py @@ -0,0 +1,60 @@ +# Copyright (c) 2020 PSMForums. All rights reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# importing requests and json +import requests, json + +# base URL +BASE_URL = "https://api.openweathermap.org/data/2.5/weather?" + +# City Name +CITY = "New Delhi" + +# API key +API_KEY = "a4212b9586f6bf848e1c47839ebfc5e9" + +# Updating the URL +URL = BASE_URL + "q=" + CITY + "&appid=" + API_KEY + +# HTTP request +response = requests.get(URL) + +# checking the status code of the request + +if response.status_code == 200: + + # getting data in the json format + data = response.json() + + # getting the main dict block + main = data['main'] + + # getting temperature + temperature = main['temp'] + + # getting the humidity + humidity = main['humidity'] + + # getting the pressure + pressure = main['pressure'] + + # weather report + + report = data['weather'] + weather_city = f"{CITY:-^30}" + weather_temperature = f"Temperature: {temperature}" + weather_humidity = f"Humidity: {humidity}" + weather_pressure = f"Pressure: {pressure}" + weather_report = f"Weather Report: {report[0]['description']}" + +else: + # showing the error message + weather_error = "Error in the HTTP request please try again"