Added: Weather Support

In this build I added the functionally to ask for the weather (only for New Delhi, India).
This commit is contained in:
Puranjay Savar Mattas
2020-12-16 10:53:17 +05:30
committed by GitHub
parent 141e4e2696
commit c8ac752ca4
3 changed files with 88 additions and 2 deletions

20
main.py
View File

@@ -9,13 +9,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import speech_recognition as sr import speech_recognition as sr
import pyttsx3 import pyttsx3
import pywhatkit import pywhatkit
import datetime import datetime
import wikipedia import wikipedia
import pyjokes import pyjokes
import weather
listener = sr.Recognizer() listener = sr.Recognizer()
engine = pyttsx3.init() engine = pyttsx3.init()
@@ -71,6 +72,23 @@ if __name__ == "__main__":
elif 'joke' in command: elif 'joke' in command:
talk('Let me get you laughing') talk('Let me get you laughing')
talk(pyjokes.get_joke()) 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: elif 'stop' in command:
break break
else: else:

View File

@@ -31,7 +31,7 @@ resolved before opening an issues.
1. Open PyCharm & create a new project with your virtual environment ready. 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): 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` `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. 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. 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` 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)

60
weather.py Normal file
View File

@@ -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"