-
Notifications
You must be signed in to change notification settings - Fork 0
/
axel.py
230 lines (182 loc) · 7.01 KB
/
axel.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
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
import speech_recognition as sr
import os
import sys
import re
import webbrowser
import smtplib
import requests
import subprocess
from pyowm import OWM
import youtube_dl
#import vlc
import urllib
import urllib.request
import json
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
import wikipedia
import random
from time import strftime
import playsound
from gtts import gTTS
import cosinesimilarity
def axelResponse(audio):
"speaks audio passed as argument"
# print(audio)
#os.system("say " + audio)
tts = gTTS(text=audio,lang ='en')
r = random.randint(1,1000000)
audio_file = 'audio-' + str(r) + '.mp3'
tts.save(audio_file)
print(audio)
playsound.playsound(audio_file)
os.remove(audio_file)
return audio
def assistant(command):
"if statements for executing commands"
if 'open' in command:
reg_ex = re.search('open (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
print(domain)
url = 'https://www.' + domain + ".com"
webbrowser.open(url)
return axelResponse('The website you have requested has been opened for you Sir.')
else:
pass
#open subreddit Reddit
elif 'open reddit' in command:
reg_ex = re.search('open reddit (.*)', command)
url = 'https://www.reddit.com/'
if reg_ex:
subreddit = reg_ex.group(1)
url = url + 'r/' + subreddit
webbrowser.open(url)
return axelResponse('The Reddit content has been opened for you Sir.')
elif 'shutdown' in command:
return axelResponse('Bye')
sys.exit()
#open website
#greetings
elif 'hello' in command:
day_time = int(strftime('%H'))
if day_time < 12:
return axelResponse('Hello Sir. Good morning')
elif 12 <= day_time < 18:
return axelResponse('Hello Sir. Good afternoon')
else:
return axelResponse('Hello Sir. Good evening')
elif 'help me' in command:
return ("""
You can use these commands and I'll help you out:\n
1. Open reddit subreddit : Opens the subreddit in default browser.\n
2. Open xyz.com : replace xyz with any website name\n
3. Send email/email : Follow up questions such as recipient name, content will be asked in order.\n
5. Current weather in {cityname} : Tells you the current condition and temperture\n
7. Greetings\n
8. news for today : reads top news of today\n
9. time : Current system time\n
10. top stories from google news (RSS feeds)\n
11. tell me about xyz : tells you about xyz\n
12. It can hold a regular small talk experience too .
""")
#top stories from google news
elif 'news for today' in command:
try:
news_url="https://news.google.com/news/rss"
Client=urlopen(news_url)
xml_page=Client.read()
Client.close()
soup_page=soup(xml_page,"xml")
news_list=soup_page.findAll("item")
for news in news_list[:15]:
axelResponse(news.title.text.encode('utf-8'))
except Exception as e:
print(e)
#current weather
elif 'current weather' in command:
# US english
session = requests.Session()
session.headers['User-Agent'] = USER_AGENT
session.headers['Accept-Language'] = "en-US,en;q=0.5"
session.headers['Content-Language'] = "en-US,en;q=0.5"
reg_ex = re.search('current weather (.*)', command)
LC = ''
try:
loc = "+"
loc = loc + reg_ex.group(1).split()[1]
LC = loc
except:
print("Scraping Local Weather Data...\n")
finally:
page= session.get("https://www.google.com/search?q=weather"+LC)
s = soup(page.text,'html.parser')
# store all results on this dictionary
id1 = s.find(id = 'wob_wc')
location = id1.find(class_ = "vk_gy vk_h").text
time =id1.find(class_ = "vk_gy vk_sh" , id = "wob_dts").text
weather = id1.find(class_ = "vk_gy vk_sh", id = "wob_dc" ).text
id2 = s.find(id = 'wob_d')
temperature = id2.find(class_ = 'wob_t', id = 'wob_tm').text
return "Location : {} \nUpdated Time : {} \nWeather: {} deg Celsius, {}".format(location,time,temperature,weather)
#time
elif 'time' in command:
import datetime
now = datetime.datetime.now()
return axelResponse('Current time is %d hours %d minutes' % (now.hour, now.minute))
#send email
elif 'email' in command:
return axelResponse('Who is the recipient?')
recipient = myCommand()
if 'david' in recipient:
axelResponse('What should I say to him?')
content = myCommand()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('[email protected]', '1233445')
mail.sendmail('[email protected]', '[email protected]', content)
mail.close()
return axelResponse('Email has been sent successfuly. You can check your inbox.')
else:
return axelResponse('I don\'t know what you mean!')
#play youtube song
elif 'play me a song' in command:
return axelResponse('What song shall I play Sir?')
mysong = myCommand()
if mysong:
flag = 0
url = "https://www.youtube.com/results?search_query=" + mysong.replace(' ', '+')
response = urlopen(url)
html = response.read()
soup1 = soup(html,"lxml")
url_list = []
for vid in soup1.findAll(attrs={'class':'yt-uix-tile-link'}):
if ('https://www.youtube.com' + vid['href']).startswith("https://www.youtube.com/watch?v="):
flag = 1
final_url = 'https://www.youtube.com' + vid['href']
url_list.append(final_url)
url = url_list[0]
ydl_opts = {}
os.chdir(path)
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
#vlc.play(path)
if flag == 0:
return axelResponse('I have not found anything in Youtube ')
#ask me anything
elif 'tell me about' or 'i want to know' in command:
reg_ex = re.search('tell me about (.*)', command)
try:
if reg_ex:
topic = reg_ex.group(1)
ny = wikipedia.page(topic)
return ny.content[:500]
except Exception as e:
print(e)
axelResponse(e)
#runningtfidfchatbot
else :
print(cosinesimilarity.chats(command))
return axelResponse(cosinesimilarity.chats(command))