-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.py
More file actions
360 lines (301 loc) · 13.8 KB
/
Copy pathTask.py
File metadata and controls
360 lines (301 loc) · 13.8 KB
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
import wikipedia,datetime,bs4,requests,time,random,os
import serial.tools.list_ports
from GoogleNews import GoogleNews
from speak import speak
import speech_recognition as sr
from pywikihow import search_wikihow
import pyjokes as jo
import threading
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print(" ")
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source,phrase_time_limit=3)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
print(f"boss: {query}")
except Exception as e:
print(e)
return ""
return query.lower()
def Time():
time = datetime.datetime.now().strftime("%I:%M %p")
speak(time)
def Date():
date = datetime.date.today()
speak(date)
def Temp():
search = "temperature in jaipur"
url = f"https://www.google.com/search?q={search}"
r = requests.get(url)
data = bs4.BeautifulSoup(r.text,"html.parser")
temperature = data.find("div",class_ = "BNeawe").text
speak(f"The Temperature Outside Is {temperature} ")
speak("Do I Have To Tell You Another Place Temperature ?")
next = takecommand()
if 'yes' in next:
speak("Tell Me The name of the place")
name = takecommand()
search = f"temperature in {name}"
url = f"https://www.google.com/search?q={search}"
r = requests.get(url)
data = bs4.BeautifulSoup(r.text,"html.parser")
temperature = data.find("div",class_ = "BNeawe").text
speak(f"The Temperature in {name} is {temperature} ")
else:
speak("no problem")
def get_random_fun_fact():
url = "https://useless-facts.sameerkumar.website/api"
response = requests.get(url)
if response.status_code == 200:
fact = response.json()["data"]
return fact
else:
return "Failed to fetch a fun fact"
def joke():
joke = jo.get_joke(category='neutral')
speak(joke)
def normalize_time(time_string):
time_parts = time_string.split()
hours = 0
minutes = 0
for i in range(len(time_parts)):
if time_parts[i] == 'hour' or time_parts[i] == 'hours':
hours = int(time_parts[i - 1])
elif time_parts[i] == 'minute' or time_parts[i] == 'minutes':
minutes = int(time_parts[i - 1])
time_str = f"{hours:02d}:{minutes:02d}"
return time_str
def set_alarm():
speak("Would you like to set the alarm in AM or PM?")
am_or_pm = takecommand()
if am_or_pm and 'am' in am_or_pm.lower():
period = 'AM'
elif am_or_pm and 'pm' in am_or_pm.lower():
period = 'PM'
else:
speak("Sorry, I didn't understand. Please try again.")
return
speak("What time would you like to set the alarm for?")
time_string = takecommand()
try:
time_string = normalize_time(time_string)
alarm_time = datetime.datetime.strptime(time_string, "%H:%M")
current_time = datetime.datetime.now()
if period == 'PM' and alarm_time < current_time:
alarm_time += datetime.timedelta(hours=12) # Add 12 hours if setting PM alarm for the same day
alarm_time = alarm_time.replace(year=current_time.year,
month=current_time.month,
day=current_time.day)
if alarm_time < current_time:
alarm_time += datetime.timedelta(days=1) # Add 1 day if alarm time is in the past
except ValueError:
speak("Sorry, I didn't understand the time format. Please try again.")
return
time_diff = alarm_time - current_time
seconds = time_diff.seconds
speak(f"Setting the alarm for {alarm_time.strftime('%I:%M %p')}.")
time.sleep(seconds)
if period == 'AM':
speak("Good morning! Wake up!")
else:
speak("Good evening! Wake up!")
def battery():
import psutil
battery = psutil.sensors_battery()
per = battery.percent
plug = battery.power_plugged
if plug == True: status = "charging"
else: status = "not charging"
speak(f"battery is currently {status} with {per} percent")
if per < 30:
speak("you should charge your device")
def boot():
import psutil
import datetime
boot_time = psutil.boot_time()
boot_time_seconds = boot_time // 1
boot_datetime = datetime.datetime.fromtimestamp(boot_time_seconds)
boot_time_formatted = boot_datetime.strftime('%I:%M:%S %p')
speak(f"you are working since {boot_time_formatted}")
def quote():
response = requests.get("http://api.quotable.io/random")
data = response.json()
quote = data["content"]
speak(f"{quote}")
def NonInputExecution(query):
query = str(query)
if "comedy" in query:
joke()
elif "time" in query:
Time()
elif "date" in query:
Date()
elif "facts" in query:
random_fact = get_random_fun_fact()
speak(random_fact)
elif "temperature" in query:
Temp()
elif "alarm" in query:
set_alarm()
elif "battery" in query:
battery()
elif "working" in query:
boot()
elif "quote" in query:
quote()
def InputExecution(tag,query):
if "search" in tag:
query = str(query).replace("search","").replace("what is","").replace("is","").replace("who is","").replace("query","").replace("can you tell me","").replace("ok","").replace("me","")
page = wikipedia.summary(query, sentences=3)
speak(page)
elif "covid" in tag:
countries = str(query).replace("corona cases in","").replace("covid cases in","").replace("ok","").replace(" ","").replace("covid","").replace("corona","")
url = f"https://www.worldometers.info/coronavirus/country/{countries}/"
result = requests.get(url)
soups = bs4.BeautifulSoup(result.text,'lxml')
corona = soups.find_all('div',class_ = 'maincounter-number')
Data = []
for case in corona:
span = case.find('span')
Data.append(span.string)
try:
cases, Death, recovored = Data
speak(f"Cases : {cases}, Deaths : {Death}, Recovered : {recovored}")
except:
speak("no data found")
elif "forecast" in tag:
p = str(query).replace("tell me the","").replace("forecast","").replace("weather","").replace("what is the","").replace("can you tell me the","").replace("of","").replace("at","").replace("ok","").replace("in","").replace(" ","")
api = "https://api.openweathermap.org/data/2.5/weather?q="+p+"&appid=06c921750b9a82d8f5d1294e1586276f"
json_data = requests.get(api).json()
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'] - 273.15)
min_temp = int(json_data['main']['temp_min'] - 273.15)
max_temp = int(json_data['main']['temp_max'] - 273.15)
pressure = json_data['main']['pressure']
humidity = json_data['main']['humidity']
wind = json_data['wind']['speed']
sunrise = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunrise'] - 21600))
sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset'] - 21600))
speak(f"it's {condition} outside, today temperature is {temp} degree celcius, minimum temperature is {min_temp} degree celcius, maximum temperature is {max_temp} degree celcius, the pressure is {pressure}, today humidity is {humidity}, wind speed is {wind} percent, sunrise is at {sunrise}, sunset is at {sunset}")
speak("do you want another place weather forecast")
city = takecommand()
if "yes" in city:
speak("tell me the name of the place")
place = takecommand()
api = "https://api.openweathermap.org/data/2.5/weather?q="+place+"&appid=06c921750b9a82d8f5d1294e1586276f"
json_data = requests.get(api).json()
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'] - 273.15)
min_temp = int(json_data['main']['temp_min'] - 273.15)
max_temp = int(json_data['main']['temp_max'] - 273.15)
pressure = json_data['main']['pressure']
humidity = json_data['main']['humidity']
wind = json_data['wind']['speed']
sunrise = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunrise'] - 21600))
sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset'] - 21600))
speak(f"it's {condition} outside, today temperature is {temp} degree celcius, minimum temperature is {min_temp} degree celcius, maximum temperature is {max_temp} degree celcius, the pressure is {pressure}, today humidity is {humidity}, wind speed is {wind} percent, sunrise is at {sunrise}, sunset is at {sunset}")
else:
speak("no problem!")
elif 'news' in tag:
query = str(query).replace("news of", "").replace("tell me", "").replace("can you", "").replace("the","").replace("what is","").replace("latest","").replace(" ","")
googlenews = GoogleNews()
speak(f'Getting news of {query}')
googlenews.get_news(query)
googlenews.result()
a = googlenews.gettext()
speak(a[1:5])
elif "rockpaper" in tag:
while True:
speak("choose your weapon to play")
user_action = takecommand()
if "break" in user_action or "bye" in user_action or "stop" in user_action or "i don't want to play" in user_action:
break
if user_action == 'tone':
user_action = "rock"
elif user_action == 'lock':
user_action = "rock"
elif user_action == 'stone':
user_action = "rock"
if user_action != "":
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
if user_action not in possible_actions:
speak("I didn't understand. Please try again with a different weapon.")
user_action = takecommand()
speak(f"\nYou choose {user_action}, and i choose {computer_action}.\n")
if user_action == computer_action:
speak(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
speak("Rock smashes scissors! You win!")
else:
speak("Paper covers rock! You lose.")
elif user_action == "lock":
if computer_action == "scissors":
speak("Rock smashes scissors! You win!")
else:
speak("Paper covers rock! You lose.")
elif user_action == "stone":
if computer_action == "scissors":
speak("stone smashes scissors! You win!")
else:
speak("Paper covers rock! You lose.")
elif user_action == "tone":
if computer_action == "scissors":
speak("stone smashes scissors! You win!")
else:
speak("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
speak("Paper covers rock! You win!")
else:
speak("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
speak("Scissors cuts paper! You win!")
else:
speak("Rock smashes scissors! You lose.")
else:
InputExecution("rockpaper",'a')
elif "mod" in tag:
query = str(query).replace("recipe","").replace("some ","").replace("ideas","").replace("cooking ","").replace("for","").replace("about ","").replace("tell me ","")
try:
if "exit" in query or "close" in query:
speak("ok!")
else:
max_results = 1
how_to = search_wikihow(query, max_results)
assert len(how_to) == 1
title = str(how_to[0].title).replace("Make","")
nstep = how_to[0].n_steps
summary = str(how_to[0].summary).replace("Make","")
speak(f"searching for{title}. there are {nstep} steps for making{title}.")
speak("should i tell you recipe")
ans = takecommand()
if "yes" in ans or "sure" in ans:
speak(summary)
else:
speak("no problem")
except Exception as e:
speak("sorry boss, i am not able to find this")
elif "volume" in tag:
import re
n = int(re.sub(r'\D',"", query))
if n < 100:
if n < 10:
n = f"0.0{n}"
else:
n = f"0.{n}"
if n == "100":
n = "1"
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, 0, None)
volume = interface.QueryInterface(IAudioEndpointVolume)
volume.SetMasterVolumeLevelScalar(float(n), None)
n = str(float(n) * 100).replace(".0", "")
speak(f"Volume Set to {n} percent")