-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
408 lines (356 loc) · 16.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import time
start = time.time()
import cryptocode
import multiprocessing
from flask import Flask, render_template,request,url_for
from gevent.pywsgi import WSGIServer
import webview
import pickle
import random
from datetime import datetime
import sqlite3
import os
from multiprocessing import Process
import urllib.request
##Danger zone below, editing the key even by one word will render the app useless
key = "nhdjhvfjnvhfjdfnvhfjdfnhfjdjhhjrhkjhjjgblkbgth"
##Danger zone above, editing the key even by one word will render the app useless.
now = datetime.now().strftime("%H:%M")
bot_response = []
greetings = ["hey","hi","hello","welcome","yo"]
affirm = ["sure","yes","yup","y","why not","of cource"]
confirm = ["you got it","sure","as you wish"]
reject = ["I'm sorry I cannot do that"]
thanks = ["thanks","thank","thnx"]
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.static_folder = 'static'
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
def add_reminder(content:str, time:str):
"""
Adds a reminder.
"""
conn = sqlite3.connect("data\\misc\\reminders.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS reminders (content text, time text)")
c.execute("INSERT INTO reminders VALUES (:content, :time)", {"content":content, "time":time})
conn.commit()
conn.close()
def check_reminder():
"""
Checks for reminders.
"""
while True:
conn = sqlite3.connect("data\\misc\\reminders.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS reminders (content text, time text)")
c.execute("SELECT * FROM reminders")
now = datetime.now().strftime("%d-%m-%y %H:%M")
for i in c.fetchall():
if i[1] == now:
print("reminder up")
c.execute("DELETE FROM reminders WHERE time = :time", {"time":now})
conn.commit()
conn.close()
from plyer import notification
notification.notify(title="Reminder from Wall-E",message=f"You asked me to remind you to `{i[0]}` now.",app_name="Wall-E",app_icon="icon.ico",timeout = 15)
def check_for_connection():
try:
urllib.request.urlopen("https://www.google.com")
return True
except:
return False
if check_for_connection():
@app.route("/")
def home():
try:
global f
f=open("data\\user\\user-data.dat","rb")
global dict2
dict2 = pickle.load(f)
return render_template("index.html",time = now,User = cryptocode.decrypt(dict2["first-name"],key))
except:
global f1
f1 = open("data\\user\\user-data.dat","wb")
global dict1
dict1 = {}
print("file created")
return render_template("new-user.html")
@app.route("/success",methods = ["POST"])
def success():
print("Inside Success")
name_first = request.form["first-name"]
name_last = request.form["last-name"]
email = request.form["email-id"]
password = request.form["password"]
f2 = open("data\\default-data\\data_def.dat","rb")
dict3 = pickle.load(f2)
if password=="":
dict1["first-name"] = cryptocode.encrypt(name_first,key)
dict1["last-name"] = cryptocode.encrypt(name_last,key)
dict1["email"] = dict3["email_def"]
dict1["password"] = dict3["passwd_def"]
f2.close()
pickle.dump(dict1,f1)
else:
dict1["first-name"] = cryptocode.encrypt(name_first,key)
dict1["last-name"] = cryptocode.encrypt(name_last,key)
dict1["email"] = cryptocode.encrypt(email,key)
dict1["password"] =cryptocode.encrypt(password,key)
pickle.dump(dict1,f1)
f1.close()
global f
f=open("data\\user\\user-data.dat","rb")
global dict2
dict2 = pickle.load(f)
print("Done")
return render_template("index.html",time = now,User = cryptocode.decrypt(dict2["first-name"],key))
@app.route("/get")
def get_bot_response():
##functions to be defined below this line
userText = request.args.get('msg').lower()
print(userText)
lst = userText.split(" ")
print(lst)
for i in lst:
if i.lower() in greetings and len(lst)<3:
lst.clear()
return f"{random.choice(['hi','Hey','Hello','Welcome','Yo'])} {cryptocode.decrypt(dict2['first-name'],key)}"
if i.lower() in thanks:
lst.clear()
return random.choice(["You're Welcome 😊","Oh! that's my job 😊"])
elif ("mail" or "email" or "message") in userText and "@" in userText:
for i in lst:
if "@" in i:
global mail_addr
mail_addr=i
try:
from email_validator import validate_email, EmailNotValidError
valid = validate_email(mail_addr)
mail_addr = valid.email
bot_response.append("mail-with-id")
lst.clear()
return random.choice(["Please specify the message","What is the message?","What will be the message?"])
except Exception:
lst.clear()
return "Looks like the provided mail id is not valid. Please check the mail id you have provided. "
elif ("mail" or "email" or "message") in i.lower():
bot_response.append("mail-without-id")
lst.clear()
return random.choice(["Please specify a mail-id","Please mention the mail-id of the recipient."])
if bot_response!=[] and bot_response[-1]=="mail-with-id":
import yagmail
print(cryptocode.decrypt(dict2["email"],key))
print(cryptocode.decrypt(dict2["password"],key))
mailer = yagmail.SMTP(cryptocode.decrypt(dict2["email"],key),cryptocode.decrypt(dict2["password"],key))
try:
mailer.send(mail_addr,"Message from "+cryptocode.decrypt(dict2["first-name"],key),userText)
bot_response.clear()
lst.clear()
return random.choice(["Got it! The mail has been sent 👍","The message has been sent 👍","Mail sent 👍"])
except:
lst.clear()
return 'Oops! Looks like I\'m unable to send the mail because Google is blocking me from doing so. Please go to <a href="https://www.google.com/settings/security/lesssecureapps">this link</a> to allow me to send mails'
elif "how" and "you" in lst:
bot_response.append(userText)
return random.choice(["I'm fine. Thanks for asking","I'm good","I'm doing fine"])
elif "who" and "are" in lst or "what" and "name" and "your" in lst:
bot_response.append(userText)
lst.clear()
return random.choice(["My name is wall-e","My developers named me wall-e","you can call me wall-e"])
elif ("open" and "google") in userText:
import webbrowser
bot_response.append(userText)
webbrowser.get().open("https://www.google.com",new=2)
lst.clear()
return "Opening Google 👍"
elif ("open" and "discord") in lst:
import webbrowser
bot_response.append(userText)
webbrowser.get().open("https://discord.com/channels/@me")
lst.clear()
return "Opening Discord 🎮"
elif ("how are you" or "how do you do") in userText:
lst.clear()
return random.choice(["I'm good! Thanks for asking","I'm doing great","I'm fine"])
elif "news" in userText:
from bs4 import BeautifulSoup
from urllib.request import urlopen
news_url = "https://news.google.com/news/rss"
Client = urlopen(news_url)
xml_page = Client.read()
Client.close()
soup_page = BeautifulSoup(xml_page, "html.parser")
news_list = soup_page.findAll("item")
news_t = "Here are some news"
for news in news_list[0:3]:
news_final = "<u>"+news_t + "</u><br>" + news.title.text
lst.clear()
return news_final
elif "wikipedia" in lst:
import wikipedia
query = userText.replace("wikipedia", "").lstrip()
summary = wikipedia.summary(query, sentences = 3)
lst.clear()
return "According to Wikipedia <br> "+summary
elif "play music" in userText:
import os
music_directory = os.path.join(os.path.expanduser("~"), "Music")
music_ext = ["mp3", "wav", "ogg"]
playables = [i for i in os.listdir(music_directory) if i.split(".")[-1] in music_ext]
if len(playables) == 0:
return f"Found no audio file in {music_directory}. Try adding some songs into the music directory."
lst.clear()
song = random.choice(playables)
os.startfile(os.path.join(music_directory, song))
return f"Sure, playing {song}."
elif "add a todo" in userText:
todo = userText.replace("add a todo", "").lstrip()
conn = sqlite3.connect("data\\misc\\todos.db")
date = datetime.now().strftime("%d/%m/%Y")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS todos (todo text, date text)")
c.execute("INSERT INTO todos VALUES (:todo, :date)", {"todo":todo, "date":date})
conn.commit()
conn.close()
lst.clear()
return f"Todo named {todo} added successfully into the database!"
elif ('delete a todo' or "complete a todo") in userText:
todo = userText.replace("delete a todo", "").replace("complete a todo", '').lstrip()
conn = sqlite3.connect("data\\misc\\todos.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS todos (todo text, date text)")
c.execute("SELECT * FROM todos WHERE todo = :todo", {"todo":todo})
if len(c.fetchall()) == 0:
lst.clear()
return f"No todo named {todo} found in the database!"
else:
c.execute("DELETE FROM todos WHERE todo = :todo", {"todo":todo})
conn.commit()
conn.close()
lst.clear()
return f"Todo named {todo} deleted successfully from the database!"
elif "view todos" in userText:
conn = sqlite3.connect("data\\misc\\todos.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS todos (todo text, date text)")
c.execute("SELECT * FROM todos")
data = "Here are all the todos you've added:<br>"
for i in c.fetchall():
todo = i[0]
date = i[1]
data += f"{todo} - {date}<br>"
lst.clear()
return data
elif "youtube" in lst:
if "search" or "play" in lst:
import requests
query = userText.replace("youtube", "").lstrip()
url = 'https://www.youtube.com/results?q=' + query
count = 0
cont = requests.get(url)
data = str(cont.content)
lst = data.split('"')
for i in lst:
count+=1
if i == 'WEB_PAGE_TYPE_WATCH':
break
if lst[count-5] == "/results":
return "No video found."
else:
vid_id_lst = lst[count-5].split("/watch?v=")
vid = "https://www.youtube.com/embed/"+vid_id_lst[-1]
return f'Sure! here you go 👍<br><iframe src={vid} title="Video" width="420" height="300"></iframe>'
else:
import webbrowser
bot_response.append(userText)
webbrowser.get().open("https://www.youtube.com",new = 2)
lst.clear()
return "opening youtube 📺"
elif "joke" in userText:
import pyjokes
return pyjokes.get_joke()
elif any(cond in userText for cond in ["facts", "fact","intresting thing","surprise me"]):
import requests
from bs4 import BeautifulSoup
data = requests.get("https://www.generatormix.com/random-facts-generator").content
soup = BeautifulSoup(data)
fact = soup.find("blockquote",attrs = {'class':"text-left"})
return "<p>Here's a fun fact</p><br>"+fact.text
elif "remind me to" in userText:
import re
userText = userText.replace("remind me to", "").lstrip()
pattern = re.compile(r"\d\d-\d\d-\d\d \d\d:\d\d")
dates = re.findall(pattern, userText)
if len(dates) == 0:
return "Invalid time input! It must be in the `DD-MM-YY HH:MM` format."
time = dates[0]
reminder = userText.replace(time, "").lstrip().rstrip()
add_reminder(reminder, time)
return f"Reminder '{reminder}' for {time} added successfully!"
elif "help" in userText:
import webbrowser
webbrowser.get().open_new_tab("https://github.com/amalthomas-exe/Wall-E#readme")
elif "weather" in userText:
from requests import get as rget
from json import loads as jloads
place = userText.replace("weather", "").lstrip()
url = f"http://api.weatherapi.com/v1/current.json?key=2f081a6878a747a5be135553200709&q={place.capitalize()}"
try:
r = rget(url).text
a = jloads(r)
current_weather = a.get('current')
condition = current_weather['condition']
text = condition['text']
if current_weather['is_day'] == 0:
return f"It is currently night in {place} with current temperature being {current_weather['temp_c']} C, though it feels like {current_weather['feelslike_c']}. The weather condition in {place} is {text}."
elif current_weather['is_day'] == 1:
return f"It is currently day in {place} with current temperature being {current_weather['temp_c']} C, though it feels like {current_weather['feelslike_c']}. The weather condition in {place} is {text}."
else:
return "An unknown error occured!"
except Exception as e:
print(e)
return "Some error occured!"
else:
try:
import wolframalpha
client = wolframalpha.Client("UXJ7K4-27QR8YUARX")
lst.clear()
return next(client.query(userText).results).text
except:
import pywhatkit
pywhatkit.search(userText)
lst.clear()
return "Sorry. I do not have the answer to your query, so I'm searching the web for the answer ."
else:
@app.route("/")
def no_connection():
return render_template("not-connected.html")
def run_server():
print("Server started")
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
def on_close():
t2.kill()
if __name__ == "__main__":
multiprocessing.freeze_support()
p1 = Process(target=check_reminder)
p1.start()
print("Thread started")
t2 = multiprocessing.Process(target=run_server)
t2.start()
window = webview.create_window("Wall-E","http://localhost:5000")
window.closing += on_close
end = time.time()
print(f"It took {end-start:.2f} seconds to complete")
webview.start()