-
Notifications
You must be signed in to change notification settings - Fork 4
/
change-the-color-of-text.python
237 lines (211 loc) · 8.23 KB
/
change-the-color-of-text.python
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
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 urllib2
import json
from bs4 import BeautifulSoup as soup
from urllib2 import urlopen
import wikipedia
import random
from time import strftime
def sofiaResponse(audio):
"speaks audio passed as argument"
print(audio)
for line in audio.splitlines():
os.system("say " + audio)
def myCommand():
"listens for commands"
r = sr.Recognizer()
with sr.Microphone() as source:
print('Say something...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '\n')
#loop back to continue to listen for commands if unrecognizable speech is received
except sr.UnknownValueError:
print('....')
command = myCommand();
return command
def assistant(command):
"if statements for executing commands"
#open subreddit Reddit
if '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)
sofiaResponse('The Reddit content has been opened for you Sir.')
elif 'shutdown' in command:
sofiaResponse('Bye bye Sir. Have a nice day')
sys.exit()
#open website
elif 'open' in command:
reg_ex = re.search('open (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
print(domain)
url = 'https://www.' + domain
webbrowser.open(url)
sofiaResponse('The website you have requested has been opened for you Sir.')
else:
pass
#greetings
elif 'hello' in command:
day_time = int(strftime('%H'))
if day_time < 12:
sofiaResponse('Hello Sir. Good morning')
elif 12 <= day_time < 18:
sofiaResponse('Hello Sir. Good afternoon')
else:
sofiaResponse('Hello Sir. Good evening')
elif 'help me' in command:
sofiaResponse("""
You can use these commands and I'll help you out:
1. Open reddit subreddit : Opens the subreddit in default browser.
2. Open xyz.com : replace xyz with any website name
3. Send email/email : Follow up questions such as recipient name, content will be asked in order.
4. Tell a joke/another joke : Says a random dad joke.
5. Current weather in {cityname} : Tells you the current condition and temperture
7. Greetings
8. play me a video : Plays song in your VLC media player
9. change wallpaper : Change desktop wallpaper
10. news for today : reads top news of today
11. time : Current system time
12. top stories from google news (RSS feeds)
13. tell me about xyz : tells you about xyz
""")
#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]:
sofiaResponse(news.title.text.encode('utf-8'))
except Exception as e:
print(e)
#current weather
elif 'current weather' in command:
reg_ex = re.search('current weather in (.*)', command)
if reg_ex:
city = reg_ex.group(1)
owm = OWM(API_key='*****************')
obs = owm.weather_at_place(city)
w = obs.get_weather()
k = w.get_status()
x = w.get_temperature(unit='celsius')
sofiaResponse('Current weather in %s is %s. The maximum temperature is %0.2f and the minimum temperature is %0.2f degree celcius' % (city, k, x['temp_max'], x['temp_min']))
#time
elif 'time' in command:
import datetime
now = datetime.datetime.now()
sofiaResponse('Current time is %d hours %d minutes' % (now.hour, now.minute))
#send email
elif 'email' in command:
sofiaResponse('Who is the recipient?')
recipient = myCommand()
if 'david' in recipient:
sofiaResponse('What should I say to him?')
content = myCommand()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('nageshsinghc@gmail.com', '*************')
mail.sendmail('nageshsingh4@gmail.com', 'amdp.hauhan@gmail.com', content)
mail.close()
sofiaResponse('Email has been sent successfuly. You can check your inbox.')
else:
sofiaResponse('I don\'t know what you mean!')
#launch any application
elif 'launch' in command:
reg_ex = re.search('launch (.*)', command)
if reg_ex:
appname = reg_ex.group(1)
appname1 = appname+".app"
subprocess.Popen(["open", "-n", "/Applications/" + appname1], stdout=subprocess.PIPE)
sofiaResponse('I have launched the desired application')
#play youtube song
elif 'play me a song' in command:
path = '/Users/nageshsinghchauhan/Documents/videos/'
folder = path
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
sofiaResponse('What song shall I play Sir?')
mysong = myCommand()
if mysong:
flag = 0
url = "https://www.youtube.com/results?search_query=" + mysong.replace(' ', '+')
response = urllib2.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:
sofiaResponse('I have not found anything in Youtube ')
#change wallpaper
elif 'change wallpaper' in command:
folder = '/Users/nageshsinghchauhan/Documents/wallpaper/'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
api_key = '***************'
url = 'https://api.unsplash.com/photos/random?client_id=' + api_key #pic from unspalsh.com
f = urllib2.urlopen(url)
json_string = f.read()
f.close()
parsed_json = json.loads(json_string)
photo = parsed_json['urls']['full']
urllib.urlretrieve(photo, "/Users/nageshsinghchauhan/Documents/wallpaper/a") # Location where we download the image to.
subprocess.call(["killall Dock"], shell=True)
sofiaResponse('wallpaper changed successfully')
#ask me anything
elif 'tell me about' in command:
reg_ex = re.search('tell me about (.*)', command)
try:
if reg_ex:
topic = reg_ex.group(1)
ny = wikipedia.page(topic)
sofiaResponse(ny.content[:500].encode('utf-8'))
except Exception as e:
print(e)
sofiaResponse(e)
sofiaResponse('Hi User, I am Sophia and I am your personal voice assistant, Please give a command or say "help me" and I will tell you what all I can do for you.')
#loop to continue executing multiple commands
while True:
assistant(myCommand())