-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (106 loc) · 3.79 KB
/
app.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask,render_template,redirect,request
from datetime import datetime
import json, requests
app = Flask(__name__)
message_list = []
@app.route('/')
def index():
return render_template("index.html")
@app.route('/contacts')
def contacts():
return render_template("contacts.html")
@app.route('/profile')
def profile():
return redirect('/')
@app.route('/chat', methods=["GET","POST"])
def chat():
if request.method == "GET":
return render_template("chat.html")
else:
message = request.form.get("message")
addSystemMessage(message)
# print(message_list)
return render_template("chat_update.html", len = len(message_list), message_list=message_list)
@app.route("/sendemail/", methods=['POST'])
def sendemail():
if request.method == "POST":
name = request.form['name']
subject = request.form['Subject']
email = request.form['_replyto']
message = request.form['message']
# Set your credentials and send email using SMTP Server
try:
# sending an email
# server.send_message(msg)
print("Send")
except:
print("Fail to Send")
pass
return redirect('/')
def hello_world():
return 'Hello, World!'
def addSystemMessage(msg) :
e = datetime.now()
message_list.append({ "user": "user", "message" : msg, "date": "%s:%s" % (e.hour, e.minute)})
msg = msg.lower()
sys_msg = ""
if msg == "hi":
sys_msg= "Hi there"
elif msg == "hello":
sys_msg= "Hello there"
elif msg == "hola":
sys_msg= "Olaaa"
elif msg == "current weather" or msg == "current weather?":
sys_msg= getWeather()
else:
sys_msg= "Sorry! I'm not sure I udnerstand that"
message_list.append({ "user": "system", "message" : sys_msg, "date" : "%s:%s" % (e.hour, e.minute)})
def getWeather():
# Enter your API key here
api_key = "35a3f6da1630a1fc51af24468b667652"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = "St. Petersburg" # input("Enter city name : ")
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
# json method of response object
# convert json format data into
# python format data
x = response.json()
print('response',x)
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] == "401":
return "Try again tomorrow";
if x["cod"] != "404":
# store the value of "main"
# key in variable y
y = x["main"]
# store the value corresponding
# to the "temp" key of y
current_temperature = y["temp"]
# store the value corresponding
# to the "pressure" key of y
current_pressure = y["pressure"]
# store the value corresponding
# to the "humidity" key of y
current_humidity = y["humidity"]
# store the value of "weather"
# key in variable z
z = x["weather"]
# store the value corresponding
# to the "description" key at
# the 0th index of z
weather_description = z[0]["description"]
# print following values
return "Temperature (in kelvin unit) = " + str(current_temperature) + "\n atmospheric pressure (in hPa unit) = " + str(current_pressure) + "\n humidity (in percentage) = " + str(current_humidity) + "\n description = " + str(weather_description)
else:
return " City Not Found "
app.run(host="localhost", port=5000, debug=True)