-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
189 lines (143 loc) · 7.08 KB
/
test.py
File metadata and controls
189 lines (143 loc) · 7.08 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
import csv
import requests
from bs4 import BeautifulSoup
import random
import math
import heapq
from datetime import datetime
#Prompt User for input which will be used to generate restarurants in that radius
#radius = input("Please enter how far the restaurants should be spaced apart from each other") 40
#List of cities that will be used to generate the datapoints required for backend
list_cities = []
#Function to get cities from an online database which will be used to generate restaurants all over florida
def getcities(list_cities):
html = requests.get("https://www.florida-demographics.com/cities_by_population")
soup = BeautifulSoup(html.text, "html.parser")
list_cities_a = soup.find_all("a")
actual_list_cities_a = list_cities_a[8:-9]
for city in actual_list_cities_a:
list_cities.append(city.text)
#Function that will create the csv data so that the backend can use it for sql
def csvdatacreator(restaurant_list, restaurants):
for restaurant in restaurants:
restaurantname = restaurant["name"]
restaurantrating = restaurant["rating"]
restaurantlatitude = restaurant["coordinates"]["latitude"]
restaurantlongitude = restaurant["coordinates"]["longitude"]
tempdictionary = {"Restaurant name": restaurantname, "Restaurant Rating": restaurantrating,
"Restaurant latitude": restaurantlatitude, "Restaurant longitude": restaurantlongitude}
restaurant_list.append(tempdictionary)
#Functiojn that will write the csv data into a file so that the backend can use it
def csvdatawriter(restaurant_list):
fields = ["Restaurant name", "Restaurant Rating", "Restaurant latitude", "Restaurant longitude"]
filename = "testdata.csv"
with open(filename, "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fields)
writer.writeheader()
writer.writerows(restaurant_list)
#Code that would gather restaruants and write them down into a csv file
'''
getcities(list_cities)
amount_of_cities = 0
Yelp_url = "https://api.yelp.com/v3/businesses/search?sort_by=best_match&limit=20"
Yelp_headers = {"accept": "application/json",
"Authorization": "Bearer z443Oizo4G3j02LHspG3dN1v12Ecs0Q9TOf9Hn87YCWhhvUV-e-Lx2a6mSDHNovu1lJ1DuCirTeYn-16ym520JSeJamlRL_kqCkuFgFDVrQg1IsdZueW3mlbhlwZZnYx"
}
#Restaurant list that will keep hold of all the various restaurants in various cities
restaurant_list = []
for city in list_cities:
Yelp_query_params = {"location": city,
"radius": 40000,
"offset": 700
}
response = requests.get(Yelp_url, headers=Yelp_headers, params=Yelp_query_params)
restaurants = response.json()["businesses"]
amount_of_cities += len(restaurants)
csvdatacreator(restaurant_list, restaurants)
csvdatawriter(restaurant_list)
print(amount_of_cities)
'''
google_url = "https://maps.googleapis.com/maps/api/directions/json"
google_query_params = {"key": "API_KEY",
"origin": "Gainesville",
"destination": "Orlando"}
#current_time = datetime.now()
#Code that would get data regarding the direction to be taken from beginning to destination
response = requests.get(google_url, params=google_query_params)
directions = response.json()["routes"][0]["legs"][0]["steps"]
direction_list = []
distance = 0
Yelp_url = "https://api.yelp.com/v3/businesses/search?sort_by=best_match&limit=20"
Yelp_headers = {"accept": "application/json",
"Authorization": "Bearer z443Oizo4G3j02LHspG3dN1v12Ecs0Q9TOf9Hn87YCWhhvUV-e-Lx2a6mSDHNovu1lJ1DuCirTeYn-16ym520JSeJamlRL_kqCkuFgFDVrQg1IsdZueW3mlbhlwZZnYx"
}
restaurant_list = []
sum = 0
#Restarurant dictionary that will map restaurants to integers for future use
restaurant_dictionary = {}
coordinate_dictionary = {}
#Put stuff from beginning position and ending position into the restaurant_list
for direction in directions:
print(direction)
distance_text = direction["distance"]["text"]
text_split = distance_text.split(" ", 1)
miles_text = text_split[0]
sum += float(miles_text)
if sum > 40: # Change this to input radius
Yelp_query_params = {"latitude": direction["end_location"]["lat"],
"longitude": direction["end_location"]["lng"],
"radius": 40000,
"offset": 0
}
yelp_response = requests.get(Yelp_url, headers=Yelp_headers, params=Yelp_query_params)
restaurants = yelp_response.json()["businesses"]
print(restaurants)
for i in range(3):
restaurant = random.choice(restaurants)
restaurant_dictionary[restaurant["name"]] = len(restaurant_dictionary)
coordinate_dictionary[restaurant["name"]] = (restaurant["coordinates"]["latitude"], restaurant["coordinates"]["longitude"])
restaurant_list.append(restaurant["name"])
sum = 0
direction_list.append(direction["end_location"])
rows = len(restaurant_list)
#Create a vertex graph for A*
two_d_list_zeros = [[0 for _ in range(rows)] for _ in range(rows)]
#Fill the vertex graph with edges
for key1 in restaurant_dictionary:
for key2 in restaurant_dictionary:
if key1 == key2:
continue
else:
google_query_params = {"key": "AIzaSyDVy-49BqkQL_ZSZDzG_0ckrzrnd1LUYQU",
"origin": f"{coordinate_dictionary[key1][0]}, {coordinate_dictionary[key1][1]}",
"destination": f"{coordinate_dictionary[key2][0]}, {coordinate_dictionary[key2][1]}"}
google_response = requests.get(google_url, params=google_query_params)
print(google_response.json())
distance_text = google_response.json()["routes"][0]["legs"][0]["distance"]["text"]
text_split = distance_text.split(" ", 1)
miles_text = text_split[0]
two_d_list_zeros[restaurant_dictionary[key1]][restaurant_dictionary[key2]] = float(miles_text)
# AN work in progress A* implementation before issues were detceted and changes were made
'''
def Astar(G):
#Potential of node in this case wil always be the last element any list
#Thus change the code in such a way that the potential will always be the ending of the given list or something similar to it
boundary_nodes = {0}
distances = {0: 0}
while len(boundary_nodes) != 0:
for node in boundary_nodes.copy():
boundary_nodes.remove(node)
if node == len(restaurant_list)-1:
return distances[node]
for i in range(len(restaurant_list)):
if G[node][i] == 0:
continue
proposed_distance = distances[node] + G[node][i]
if i not in distances or distances[i] > proposed_distance:
distances[i] = proposed_distance
boundary_nodes.add(i)
return -1
'''
#response_time = datetime.now()
#changeintime = response_time-current_time
#print(changeintime)