-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI Assistant.py
205 lines (167 loc) · 6.03 KB
/
AI Assistant.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
import pyttsx3 ; import datetime ; import speech_recognition as sr ; from scapy.all import sniff
import wikipedia as wiki ; import smtplib
import webbrowser as wb ; import os ; import pyautogui ; import psutil ; import pyjokes ; import requests
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(" Hello Med Amine !!")
engine.runAndWait()
def speak(audio):
engine.say (audio)
engine.runAndWait()
def time() :
#time = datetime.datetime.now().strftime("%Y-%m-%d")
hour = datetime.datetime.now().hour
minute = datetime.datetime.now().minute
speak(hour)
speak(minute)
#time()
def wishme() :
current_hour = int (datetime.datetime.now().hour)
if current_hour >= 6 and current_hour < 12:
speak("i hope you doing well , Good Morning , how can i assist you")
elif current_hour >= 12 and current_hour <18 :
speak("i hope you doing well , Good Afternoon , how can i assist you")
elif current_hour >= 18 and current_hour < 24:
speak("i hope you doing well , Good Evening , how can i assist you")
else:
speak("Good Night")
#wishme()
def date() :
year = int (datetime.datetime.now().year)
month = int (datetime.datetime.now().month)
day = int (datetime.datetime.now().day)
speak(day)
speak(month)
speak(year)
#date()
def command_orders():
micro = sr.Microphone()
recognizer = sr.Recognizer()
with micro as source:
print("im listening ...")
#speak("im listening ...")
recognizer.pause_threshold = 0.5
audio = recognizer.listen(source)
try:
print("recognizing ...")
query = recognizer.recognize_google(audio, language="en-uk")
print(query)
except Exception as e:
print(e)
print("Im Sorry i can't hear youu Properly , say it again")
return "None"
return query
#command_orders()
def send_mail(to,content) :
server = smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.login("" , "****")
server.sendmail("",to,content)
def screenshot() :
img = pyautogui.screenshot()
img.save("screenshot.png")
def cpu() :
cpu_usage = str(psutil.cpu_percent())
speak((cpu_usage))
def battery () :
battery = psutil.sensors_battery()
speak(battery.percent)
def capture_network_traffic(interface, num_packets):
packets = sniff(iface=interface, count=num_packets)
print(f"{len(packets)} packets captured successfully !")
return packets
def analyse_packets(packets):
for packet in packets:
print(packet.summary())
def get_weather(api_key, city):
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
'q': city,
'appid': api_key,
'units': 'metric'
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
speak(f"Weather in {city}:")
speak(f"Temperature: {data['main']['temp']}°C")
speak(f"Description: {data['weather'][0]['description']}")
speak(f"Humidity: {data['main']['humidity']}%")
else:
speak(f"Error: Unable to retrieve weather data. Status code: {response.status_code}")
if __name__ == '__main__':
wishme()
while True:
query =command_orders().lower()
if "time" in query :
speak("the current time is")
time()
elif "date" in query :
speak("the current date is")
date()
elif "your name" in query :
speak("My name is Mohamed Amine")
elif "wikipedia" in query :
speak("Searching ...")
query = query.replace("wikipedia", "")
result = wiki.summary(query,sentences=2)
print(result)
speak(result)
elif "search in google" in query:
speak("what should i search !")
search = command_orders().lower()
print(search)
#wb.open_new_tab(search+".com")
wb.open_new_tab(search)
elif "send email" in query :
try :
speak("what should i say !")
content = command_orders()
to=""
send_mail(to, content)
speak("email has been sent successfully")
except Exception as e :
print(e)
speak("there is something wrong , please try again")
elif "save this" in query :
speak("what should i save !")
data = command_orders()
speak("as i remember you said !"+ data)
remember = open("data.txt","w")
remember.write(data)
remember.close()
elif "log out" in query :
os.system("shutdown /l")
elif "shutdown" in query :
os.system(" shutdown /s")
elif "restart" in query :
os.system(" shutdown /r")
elif "offline" in query :
quit()
elif "screenshot" in query :
screenshot()
speak("Done ")
elif "cpu" in query :
cpu()
elif "battery " in query :
battery()
elif "joke" in query:
try:
jokes=pyjokes.get_joke()
print(jokes)
speak(jokes)
except Exception as e:
print(f"An error occurred: {str(e)}")
elif "sniffing network" in query :
interface="Wi-Fi"
speak("how many packets!")
num_packets = command_orders()
captured_packets = capture_network_traffic(interface,int(num_packets))
analyse_packets(captured_packets)
elif "weather" in query :
api_key = 'ur api key'
speak("for which country ! ")
city_name = command_orders()
get_weather(api_key, city_name)