forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_a_way.py
121 lines (103 loc) · 4.52 KB
/
Find_a_way.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
import logging
import webbrowser
import requests
import re
from voice_recognition import recognize_speech
from text_to_speech import speak
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Replace with your actual Google Maps API key
GOOGLE_MAPS_API_KEY = "YOUR_GOOGLE_MAPS_API_KEY"
def strip_html_tags(text: str) -> str:
"""Remove HTML tags from a string."""
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
def get_directions(start: str, destination: str) -> str:
"""Fetch directions from Google Maps."""
url = f"https://maps.googleapis.com/maps/api/directions/json?origin={start}&destination={destination}&key={GOOGLE_MAPS_API_KEY}"
try:
response = requests.get(url)
response.raise_for_status() # Raises an HTTPError for bad responses
data = response.json()
if data['status'] == 'OK':
directions = data['routes'][0]['legs'][0]['steps']
return " ".join([strip_html_tags(step['html_instructions']) for step in directions])
else:
return "I couldn't find directions for that route."
except requests.exceptions.RequestException:
return "There was an error fetching directions."
def find_nearby_places(location: str, place_type: str) -> str:
"""Fetch nearby places using Google Maps."""
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius=1500&type={place_type}&key={GOOGLE_MAPS_API_KEY}"
try:
response = requests.get(url)
response.raise_for_status() # Raises an HTTPError for bad responses
data = response.json()
places = data.get('results', [])
if places:
return [place['name'] for place in places]
else:
return []
except requests.exceptions.RequestException:
return []
def main():
response = "Welcome to the navigation tool. How can I assist you?"
logging.info(response)
speak(response)
while True:
logging.info("Listening for navigation command...")
command = recognize_speech(timeout=10)
if command:
command = command.lower()
logging.info(f"Recognized command: {command}")
# Check for exit command
if 'exit' in command or 'stop' in command:
response = "Exiting navigation tool."
logging.info(response)
speak(response)
break
# Get directions
if 'get directions' in command:
response = "What is your starting point?"
logging.info(response)
speak(response)
start_point = recognize_speech()
if start_point:
response = "What is your destination?"
logging.info(response)
speak(response)
destination = recognize_speech()
if destination:
directions = get_directions(start_point, destination)
logging.info(directions)
speak(directions)
webbrowser.open(f"https://www.google.com/maps/dir/?api=1&origin={start_point}&destination={destination}")
else:
speak("I didn't catch the destination.")
else:
speak("I didn't catch the starting point.")
# Find nearby places
elif 'find nearby' in command:
response = "What type of places are you looking for?"
logging.info(response)
speak(response)
place_type = recognize_speech()
if place_type:
response = "What is your current location?"
logging.info(response)
speak(response)
location = recognize_speech()
if location:
places = find_nearby_places(location, place_type)
if places:
response = f"Nearby {place_type} include: " + ", ".join(places)
else:
response = "No nearby places found."
logging.info(response)
speak(response)
else:
speak("I didn't catch your location.")
else:
speak("I didn't catch the type of places.")
if __name__ == "__main__":
main()