Spaces:
Runtime error
Runtime error
Upload 10 files
Browse files- app.py +36 -0
- app_exception/__init__.py +0 -0
- app_exception/__pycache__/__init__.cpython-312.pyc +0 -0
- app_exception/__pycache__/exception.cpython-312.pyc +0 -0
- app_exception/exception.py +39 -0
- app_logger/__init__.py +0 -0
- app_logger/__pycache__/__init__.cpython-312.pyc +0 -0
- app_logger/__pycache__/logger.cpython-312.pyc +0 -0
- app_logger/logger.py +26 -0
- weather_data.py +47 -0
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask
|
| 2 |
+
from flask import Flask, request, make_response
|
| 3 |
+
import os, json
|
| 4 |
+
from flask_cors import CORS,cross_origin
|
| 5 |
+
from weather_data import WeatherData
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route('/')
|
| 10 |
+
def index():
|
| 11 |
+
return 'Web App with Python Flask!'
|
| 12 |
+
|
| 13 |
+
# geting and sending response to dialogflow
|
| 14 |
+
@app.route('/webhook', methods=['POST'])
|
| 15 |
+
@cross_origin()
|
| 16 |
+
def webhook():
|
| 17 |
+
req = request.get_json(silent=True, force=True)
|
| 18 |
+
print("Request:")
|
| 19 |
+
|
| 20 |
+
print(json.dumps(req))
|
| 21 |
+
|
| 22 |
+
res = object.processRequest(req)
|
| 23 |
+
|
| 24 |
+
res = json.dumps(res)
|
| 25 |
+
print(res)
|
| 26 |
+
r = make_response(res)
|
| 27 |
+
r.headers['Content-Type'] = 'application/json'
|
| 28 |
+
return r
|
| 29 |
+
|
| 30 |
+
if __name__ == '__main__':
|
| 31 |
+
object=WeatherData()
|
| 32 |
+
#port = int(os.getenv('PORT', 5000))
|
| 33 |
+
#print("Starting app on port %d" % port)
|
| 34 |
+
app.run(debug=True)
|
| 35 |
+
# if __name__=='__main__':
|
| 36 |
+
# # app.run(host='0.0.0.0',port=8080)
|
app_exception/__init__.py
ADDED
|
File without changes
|
app_exception/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (163 Bytes). View file
|
|
|
app_exception/__pycache__/exception.cpython-312.pyc
ADDED
|
Binary file (2.15 kB). View file
|
|
|
app_exception/exception.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
class AppException(Exception):
|
| 5 |
+
|
| 6 |
+
def __init__(self, error_message: Exception, error_detail: sys):
|
| 7 |
+
"""
|
| 8 |
+
:param error_message: error message in string format
|
| 9 |
+
"""
|
| 10 |
+
super().__init__(error_message)
|
| 11 |
+
self.error_message = AppException.error_message_detail(error_message, error_detail=error_detail)
|
| 12 |
+
|
| 13 |
+
@staticmethod
|
| 14 |
+
def error_message_detail(error:Exception, error_detail:sys):
|
| 15 |
+
"""
|
| 16 |
+
error: Exception object raise from module
|
| 17 |
+
error_detail: is sys module contains detail information about system execution information.
|
| 18 |
+
"""
|
| 19 |
+
_, _, exc_tb = error_detail.exc_info()
|
| 20 |
+
#extracting file name from exception traceback
|
| 21 |
+
file_name = exc_tb.tb_frame.f_code.co_filename
|
| 22 |
+
|
| 23 |
+
#preparing error message
|
| 24 |
+
error_message = f"Error occurred python script name [{file_name}]" \
|
| 25 |
+
f" line number [{exc_tb.tb_lineno}] error message [{error}]."
|
| 26 |
+
|
| 27 |
+
return error_message
|
| 28 |
+
|
| 29 |
+
def __repr__(self):
|
| 30 |
+
"""
|
| 31 |
+
Formating object of AppException
|
| 32 |
+
"""
|
| 33 |
+
return AppException.__name__.__str__()
|
| 34 |
+
|
| 35 |
+
def __str__(self):
|
| 36 |
+
"""
|
| 37 |
+
Formating how a object should be visible if used in print statement.
|
| 38 |
+
"""
|
| 39 |
+
return self.error_message
|
app_logger/__init__.py
ADDED
|
File without changes
|
app_logger/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (160 Bytes). View file
|
|
|
app_logger/__pycache__/logger.cpython-312.pyc
ADDED
|
Binary file (1.03 kB). View file
|
|
|
app_logger/logger.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from app_exception.exception import AppException
|
| 5 |
+
import uuid
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# Creating logs directory to store log in files
|
| 9 |
+
LOG_DIR = "logs"
|
| 10 |
+
LOG_DIR = os.path.join(os.getcwd(), LOG_DIR)
|
| 11 |
+
|
| 12 |
+
#Creating LOG_DIR if it does not exists.
|
| 13 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Creating file name for log file based on current timestamp
|
| 17 |
+
CURRENT_TIME_STAMP = f"{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}"
|
| 18 |
+
file_name = f"log_{CURRENT_TIME_STAMP}.log"
|
| 19 |
+
|
| 20 |
+
#Creating file path for projects.
|
| 21 |
+
log_file_path = os.path.join(LOG_DIR, file_name)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
logging.basicConfig(filename=log_file_path,
|
| 25 |
+
filemode='w',
|
| 26 |
+
format='[%(asctime)s] %(name)s - %(levelname)s - %(message)s')
|
weather_data.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from app_logger import logger
|
| 2 |
+
from app_exception.exception import AppException
|
| 3 |
+
import pyowm
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class WeatherData:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.owmapikey='f96d1e6acb08fa0c55193d13b893bb5f'
|
| 10 |
+
self.owm = pyowm.OWM(self.owmapikey)
|
| 11 |
+
|
| 12 |
+
'''
|
| 13 |
+
processing the request from dialogflow
|
| 14 |
+
|
| 15 |
+
'''
|
| 16 |
+
def processRequest(self,req):
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
self.result = req.get("queryResult")
|
| 20 |
+
self.parameters = self.result.get("parameters")
|
| 21 |
+
self.city = self.parameters.get("city_name")
|
| 22 |
+
|
| 23 |
+
self.observation = self.owm.weather_at_place(str(self.city))
|
| 24 |
+
|
| 25 |
+
w = self.observation.get_weather()
|
| 26 |
+
self.latlon_res = self.observation.get_location()
|
| 27 |
+
|
| 28 |
+
self.lat = str(self.latlon_res.get_lat())
|
| 29 |
+
self.lon = str(self.latlon_res.get_lon())
|
| 30 |
+
|
| 31 |
+
self.wind_res = w.get_wind()
|
| 32 |
+
self.wind_speed = str(self.wind_res.get('speed'))
|
| 33 |
+
|
| 34 |
+
self.humidity = str(w.get_humidity())
|
| 35 |
+
|
| 36 |
+
self.celsius_result = w.get_temperature('celsius')
|
| 37 |
+
self.temp_min_celsius = str(self.celsius_result.get('temp_min'))
|
| 38 |
+
self.temp_max_celsius = str(self.celsius_result.get('temp_max'))
|
| 39 |
+
|
| 40 |
+
speech = "Today's the weather in " + str(self.city) + ":" + " , " +"Humidity : " + str(self.humidity) +" , " + "Wind Speed : " +str(self.wind_speed)+ " , " + "minimum temperature : " + str(self.temp_min_celsius) + " , " + "maximum temperature : " + str(self.temp_max_celsius)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise AppException(e, sys) from e
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"fulfillmentText": speech,
|
| 46 |
+
"displayText": speech
|
| 47 |
+
}
|