-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.py
515 lines (449 loc) · 22.5 KB
/
bot.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
######### Amelia Bot #########
######### Created By Kaustubh Saxena #########
#Library Import
import aiml
import os,time,sys,webbrowser,datetime,random,subprocess
from PyQt4 import QtCore, QtGui
from googlefinance import getQuotes
import pyowm
import json
from random import randint
import cv2
import pyttsx
from wikiapi import WikiApi
import urllib2
from bs4 import BeautifulSoup
from PyDictionary import PyDictionary
import pyglet
from twilio.rest import TwilioRestClient
import wolframalpha
client = wolframalpha.Client('LVHL84-HKV36GTR2J')
########################## Initializers ##############################
wiki = WikiApi()
engine = pyttsx.init()
owm = pyowm.OWM('956035a102b23ad1ee02de887ca99ad2')
client = TwilioRestClient('AC604129f034e7162b4017a3eb37cb39aa','bc5f3197c08157164b4bda1a71913e8b')
rand=(randint(0,90000))
############################Splash Screen############################
app = QtGui.QApplication(sys.argv)
splash_pix = QtGui.QPixmap('conti.jpg')
splash = QtGui.QSplashScreen(splash_pix)
splash.setMask(splash_pix.mask())
splash.show()
time.sleep(2)
splash.hide()
###########################Welcome Message############################
text2speak = "Hello, I am Amelia."
engine.setProperty('rate', 180)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak)
engine.runAndWait()
########################### Kernel Code Start ############################
kernel = aiml.Kernel()
########################### Temporary Brain File #########################
if os.path.isfile("bot_brain.brn"):
kernel.bootstrap(brainFile = "bot_brain.brn")
else:
kernel.bootstrap(learnFiles = "std-startup.xml", commands = "load aiml b")
kernel.saveBrain("bot_brain.brn")
########################### kernel now ready for use ######################
while True:
message = raw_input("************* Say Something To Amelia : ")
########################### Exit #########################
if message == "quit" or message == "exit":
splash_pix = QtGui.QPixmap('bye.jpg')
splash = QtGui.QSplashScreen(splash_pix)
splash.setMask(splash_pix.mask())
splash.show()
time.sleep(0.7)
print "Amelia : Nice Talking To You. Bye Bye"
text2speak = "Nice Talking to You, Bye Bye"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak)
engine.runAndWait()
time.sleep(0)
splash.hide()
exit()
########################### Save The Brain ########################
elif message == "save":
kernel.saveBrain("bot_brain.brn")
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say('Saving Kernel')
engine.runAndWait()
########################## RESULT #################################
elif message == "result":
kuk = "http://result.kuk.ac.in/index.php?nu=9894274"
page = urllib2.urlopen(kuk)
soup = BeautifulSoup(page, "html.parser")
# print soup.prettify()
contents = [str(x.text) for x in soup.find(id="txtClassId").find_all('option')] # List Comprehension
# print contents
matching_a = [s for s in contents if "B.TECH" in s]
matching_b = [s for s in contents if "B.Tech" in s]
matching_c = [s for s in contents if "BTECH" in s]
res_count = len(matching_a) + len(matching_b) + len(matching_c)
print "Total Number Of B.Tech Results Out : {0}".format(res_count)
if res_count!=0:
for res_a in matching_a:
print res_a
client.messages.create(from_='+13343848545',
to='+919034232578',
body='{0} - Check http://result.kuk.ac.in/index.php?nu=9894274'.format(res_a))
for res_b in matching_b:
print res_b
client.messages.create(from_='+13343848545',
to='+919034232578',
body='{0} - Check http://result.kuk.ac.in/index.php?nu=9894274'.format(res_b))
for res_c in matching_c:
print res_c
client.messages.create(from_='+13343848545',
to='+919034232578',
body='{0} - Check http://result.kuk.ac.in/index.php?nu=9894274'.format(res_c))
print "SMS Sent Successfully."
######################### IMDb ################################
elif message == "imdb":
query_entertain = raw_input("Enter Movie, Character or Cast : ")
print "Processing and Opening IMDb for " +query_entertain+ ",Please Wait..."
print "Opening Browser.."
webbrowser.open_new("http://www.imdb.com/find?ref_=nv_sr_fn&q={0}&s=all".format(query_entertain))
############################ Play Music #########################
elif message =="play music":
randomfile = random.choice(os.listdir("D:\\Music\\Collection\\"))
file1 = 'D:\\Music\\Collection\\' + randomfile
music = pyglet.media.load(file1)
print ("Song : {0}").format(randomfile)
print ("Duration(in Seconds) : {0}").format(music.duration)
print "Starting Playback"
subprocess.call(['C:\\Program Files\\Windows Media Player\\wmplayer.exe', file1])
print "Playback Stopped"
############################ Camera #############################
elif message == "selfie":
text2speak4 = "Starting Amelia Cam"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak4)
engine.runAndWait()
cam = cv2.VideoCapture(0)
cv2.namedWindow("Amelia Cam")
img_counter = 0
text2speak5 = "Press Spacebar To Take A Photo, Press Q To View Photos and Escape Key To Quit"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak5)
engine.runAndWait()
while True:
ret, frame = cam.read()
cv2.imshow("Amelia Cam", frame)
if not ret:
break
k = cv2.waitKey(1)
if k % 256 == 27:
# ESC pressed
print("Escape hit, closing...")
cam_exit = "Exiting"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(cam_exit)
engine.runAndWait()
break
elif k % 256 == 32:
# SPACE pressed
img_name = "camera/amelia_cam_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
photo_click = "Photo Clicked"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(photo_click)
engine.runAndWait()
elif k % 256 == 113:
# Q pressed For View Gallery
os.system('explorer "e:\jarvis\camera"')
viewer = "Opening Photos"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(viewer)
engine.runAndWait()
cam.release()
cv2.destroyAllWindows()
######################### Location #############################
elif "where is" in message:
message = message.split(" ")
location = message[2]
print "Hold on Boss, I will show you where " + location + " is."
voic_comm="Hold on Boss, I will show you where" + location + "is."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(voic_comm)
engine.runAndWait()
webbrowser.open("https://www.google.co.in/maps?q={0}".format(location))
######################### Weather ##############################
elif message == "weather":
city =raw_input("Enter City : ")
webbrowser.open('https://www.google.co.in/?gfe_rd=cr&ei=o7ahV5PkMt3mugTXnbugCw#q=weather in '+ city)
########################## Dictionary ###########################
elif message == "dictionary":
message_dict = raw_input("Enter Word : ")
dictionary = PyDictionary(message_dict)
print (dictionary.printMeanings())
########################### Railways ###########################
elif message == "rail":
voic_rail = "Welcome To Amelia Railway Information System (ARIS)"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(voic_rail)
engine.runAndWait()
print "*******************---------- Amelia Railway Information System (ARIS) ----------**********************"
choice = raw_input("Input : \n 0 for Train Info \n 1 for Live Train Status \n 2 for Train Route \n 3 for Train Fare Enquiry \n 4 for Train arrivals at a Station \n 5 for Cancelled Trains \n 6 Station Name to Code \n 7 Station Code to Name \n Choice : ")
if choice == "0":
choice_0 = "Train Information Selected, Enter Train Number."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_0)
engine.runAndWait()
print "********** Train Information **********"
train = raw_input("Enter Train Number or Name : ")
url = "http://api.railwayapi.com/route/train/{0}/apikey/25psa0aw/".format(train)
response = urllib2.urlopen(url)
data = json.loads(response.read())
print "Train Name : {0}".format(data['train']['name'])
print "Train Number : {0}".format(data['train']['number'])
classes = data['train']['classes']
for class_info in classes:
print "Classes : {0} - {1}".format(class_info['class-code'],class_info['available'])
days = data['train']['days']
for day in days:
print "Days : {0} - {1}".format(day['day-code'],day['runs'])
elif choice == "1":
choice_1 = "Live Train Status selected."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_1)
engine.runAndWait()
print "********** Live Train Status **********"
train = raw_input("Enter Train Number : ")
doj = raw_input("Enter Date of Journey in YYYYMMDD Format : ")
url = "http://api.railwayapi.com/live/train/{0}/doj/{1}/apikey/25psa0aw/".format(train,doj)
response = urllib2.urlopen(url)
data = json.loads(response.read())
print "Live Status For {0} : {1}".format(train,data['position'])
elif choice == "2":
choice_2 = "Train Route Selected."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_2)
engine.runAndWait()
print "********** Train Route **********"
train = raw_input("Enter Train Number : ")
url = "http://api.railwayapi.com/route/train/{0}/apikey/25psa0aw/".format(train)
response = urllib2.urlopen(url)
data = json.loads(response.read())
print "Train Name : {0}".format(data['train']['name'])
for route_info in data['route']:
print "Station Name : {0}".format(route_info['fullname'])
print "Station Code : {0}".format(route_info['code'])
print "Station Number : {0}".format(route_info['no'])
print "Distance From Source : {0}".format(route_info['distance'])
print "Scheduled Arrival : {0}".format(route_info['scharr'])
print "Scheduled Departure : {0}".format(route_info['schdep'])
print "Day of Running : {0}".format(route_info['day'])
print "State : {0}".format(route_info['state'])
print "---------------------------------------------------------"
elif choice == "3":
choice_3 = "Train Fare Enquiry Selected."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_3)
engine.runAndWait()
print "********** Train Fare Enquiry **********"
train = raw_input("Enter Train No. : ")
source = raw_input("Enter Source Station Code: ")
dest = raw_input("Enter Destination Station Code: ")
age = raw_input("Enter Age : ")
quota = raw_input("Enter Quota Code (To Know - http://www.indianrail.gov.in/quota_Code.html) : ")
doj = raw_input("Enter Date Of Journey in DD-MM-YYYY format : ")
url = "http://api.railwayapi.com/fare/train/{0}/source/{1}/dest/{2}/age/{3}/quota/{4}/doj/{5}/apikey/25psa0aw/".format(train,source,dest,age,quota,doj)
response = urllib2.urlopen(url)
data = json.loads(response.read())
print "*_*_*_*_*_*_*_*** Fare Enquiry ***_*_*_*_*_*_*_*"
print "Train Number : {0} , {1}".format(data['train']['number'],data['train']['name'])
print "From : {0} ({1})".format(data['from']['name'], data['from']['code'])
print "To : {0} ({1})".format(data['to']['name'],data['to']['code'])
print "Quota : {0} ({1})".format(data['quota']['name'],data['quota']['code'])
for fare_info in data['fare']:
print "{0} : {1} INR".format(fare_info['name'],fare_info['fare'])
elif choice == "4":
choice_4 = "Train Arrivals At a Station."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_4)
engine.runAndWait()
print "********** Train Arrivals at Station **********"
stat_code = raw_input("Enter Station Code : ")
hours = raw_input("Hours to search within : ")
url = "http://api.railwayapi.com/arrivals/station/{0}/hours/{1}/apikey/25psa0aw/".format(stat_code,hours)
response = urllib2.urlopen(url)
data = json.loads(response.read())
for train_stat in data['train']:
print "Train Number : {0}".format(train_stat['number'])
print "Train Name : {0}".format(train_stat['name'])
print "Scheduled Arrival {0}".format(train_stat['scharr'])
print "Scheduled Departure {0}".format(train_stat['schdep'])
print "Actual Arrival {0}".format(train_stat['actarr'])
print "Acitual Departure {0}".format(train_stat['actdep'])
print "Delayed Arrival : {0}".format(train_stat['delayarr'])
print "Delayed Departure : {0}".format(train_stat['delaydep'])
print "--------------------------------"
elif choice == "5":
choice_5 = "Cancelled Trains."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_5)
engine.runAndWait()
print "****************Cancelled Trains******************"
cancel_date = raw_input("Enter Date In DD-MM-YYYY Format : ")
url = "http://api.railwayapi.com/cancelled/date/{0}/apikey/25psa0aw/".format(cancel_date)
response = urllib2.urlopen(url)
data = json.loads(response.read())
for train_stat in data['trains']:
print "Source : {0} - {1}".format(train_stat['source']['name'],train_stat['source']['code'])
print "Destination : {0} - {1}".format(train_stat['dest']['code'],train_stat['dest']['code'])
print "Train : {0} {1} ; Start Time {2}:".format(train_stat['train']['number'],train_stat['train']['name'],train_stat['train']['start_time'])
print "------------------------------------------"
elif choice == "6":
choice_6 = "Station Name To Code."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_6)
engine.runAndWait()
print "********************** Station Name to Code **********************"
station = raw_input("Enter Enter Station Name : ")
url = "http://api.railwayapi.com/name_to_code/station/{0}/apikey/25psa0aw/".format(station)
response = urllib2.urlopen(url)
data = json.loads(response.read())
for stat_name in data['stations']:
print "Fullname :{0}".format(stat_name['fullname'])
print "Station Code :{0}".format(stat_name['code'])
print "State : {0}".format(stat_name['state'])
print "Latitude Location : {0}".format(stat_name['lat'])
print "Longitude Location : {0}".format(stat_name['lng'])
print "----------------------------------------------------------------"
elif choice == "7":
choice_7 = "Station Code to Name."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(choice_7)
engine.runAndWait()
print "*********************** Station Code to Name ************************"
code = raw_input("Enter Station Code : ")
url = "http://api.railwayapi.com/code_to_name/code/{0}/apikey/25psa0aw/".format(code)
response = urllib2.urlopen(url)
data = json.loads(response.read())
for stat_code in data['stations']:
print "Station Code : {0}".format(stat_code['code'])
print "Station Name : {0}".format(stat_code['fullname'])
print "Latitude : {0}".format(stat_code['lat'])
print "Longitude : {0}".format(stat_code['lng'])
print "State : {0}".format(stat_code['state'])
print "--------------------------------------------------------------------"
######################### Stock Market #########################
########################### Stock ##############################
elif message == "stock" :
stock_symbol = raw_input("Enter a Stock Symbol : ")
stock_data = getQuotes(stock_symbol)
for stock_info in stock_data:
print "Stock Symbol : {0}".format(stock_info['StockSymbol'])
print "ID : {0}".format(stock_info['ID'])
print "Index : {0}".format(stock_info['Index'])
print "Last Trade With Currency : {0}".format(stock_info['LastTradeWithCurrency'])
print "Last Trade Date and Time : {0}".format(stock_info['LastTradeDateTimeLong'])
print "Last Trade Price : {0}".format(stock_info['LastTradePrice'])
########################### Google #############################
elif message == "google":
text2speak6 = "Enter Your Term To Google"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak6)
engine.runAndWait()
goog_term = raw_input("Enter Your Term to Google : ")
webbrowser.open('https://www.google.co.in/?gfe_rd=cr&ei=o7ahV5PkMt3mugTXnbugCw#q={0}'.format(goog_term))
text2speak7 = "Googling it, Check Your Browser."
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak7)
engine.runAndWait()
print "Googling it, Check Your Browser."
########################## Date and Time##########################
elif "date" in message:
date_today="Today's Date: {0}".format(datetime.datetime.utcnow().date())
print date_today
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(date_today)
engine.runAndWait()
elif "time" in message:
time="Current Time: {0}".format(time.strftime("%I:%M:%S"))
print time
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(time)
engine.runAndWait()
######################### Status ########################
elif message=="status":
all_stat = "All Components Functioning Normally"
python_stat="Python Core : Normal Functionality"
aiml_stat="Aiml Load : Normal"
kernel_stat="Kernel : Normal"
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(all_stat)
engine.runAndWait()
print all_stat
engine.say(python_stat)
engine.runAndWait()
print python_stat
engine.say(aiml_stat)
engine.runAndWait()
print aiml_stat
engine.say(kernel_stat)
engine.runAndWait()
print kernel_stat
######################### Help ##########################
elif message == "help":
print "********************* Project Amelia Handbook ******************** \n"
print "save - Save The Brain in a .brn File."
print "quit - Quit Amelia and Shut Down."
print "google - Google Your Search Term."
print "play music - Play a Random Music File."
print "date - Today's Date."
print "time - Current Time."
print "dictionary - Search for your term meanings."
print "weather - Current Weather Details."
print "selfie - Opens Camera To Click Photos."
print "rail - Enter ARIS System to get information regarding Indian Railways."
print "stock - Get Information Of Stock Market."
print "imdb - Search For any Movie, Actor, TV Show in IMDb."
print "result - Get B.tech results Notification on your phone."
print "status - Check Status of the Amelia's Working. \n"
elif message == "":
print "*****************Please Enter Input*****************"
######################### Normal Response From AIML And if Not found WIKI and Google######################
else:
text2speak = kernel.respond(message)
print "Amelia : {0}".format(text2speak)
splash_pix = QtGui.QPixmap('robo.jpg')
splash = QtGui.QSplashScreen(splash_pix)
splash.setMask(splash_pix.mask())
splash.show()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(text2speak)
engine.runAndWait()
splash.hide()
if kernel.respond(message) == "Lemme Think":
res = client.query(message)
print(next(res.results).text)