-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.py
209 lines (159 loc) · 5.38 KB
/
addresses.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
206
207
208
209
import tkinter as tk
import requests
import threading
import time
def read_allowed_zips():
f = open('allowed_zips.txt', 'r')
lines = f.readlines()
f.close()
lines2 = []
for line in lines:
lines2.append(line.strip())
return lines2
def read_saved_addresses():
f = open('saved_addresses.txt', 'r')
lines = f.readlines()
f.close()
lines2 = []
for line in lines:
lines2.append(line.strip())
return lines2
def write_addresses(addresses):
f = open('saved_addresses.txt', 'w')
for address in addresses:
f.write(address + '\n')
f.close()
def add_address(address):
addresses = read_saved_addresses()
if not address in addresses:
addresses.append(address)
write_addresses(addresses)
def delete_address(address):
addresses = read_saved_addresses()
if address in addresses:
addresses.remove(address)
write_addresses(addresses)
class GoogleAPI:
def __init__(self):
self.read_api_key()
def read_api_key(self):
api_file = open('api-key.txt', 'r')
self.api_key = api_file.read()
api_file.close()
def call_autocomplete(self, input_text):
url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input={}&key={}".format(input_text, self.api_key)
payload={}
headers = {}
response = requests.request("GET", url, headers = headers, data = payload)
return response.json()
def call_place_details(self, place_id):
url = "https://maps.googleapis.com/maps/api/place/details/json?place_id={}&key={}".format(place_id, self.api_key)
payload={}
headers = {}
response = requests.request("GET", url, headers = headers, data = payload)
return response.json()
LIST_WIDTH = 100
window_width = 700
window_height = 350
class AddressWindow:
def __init__(self):
self.allowed_zips = read_allowed_zips()
self.frame = tk.Tk()
self.frame.title("Addresses")
self.center_window()
self.label = tk.Label(self.frame, text = "Where are you located?\nSo, we know where to drop off the stuff")
self.label.pack()
sv = tk.StringVar()
sv.trace("w", lambda name, index, mode, sv = sv: self.entry_modified(sv))
self.input = tk.Entry(self.frame, textvariable = sv)
self.input.pack()
self.input.config(width = LIST_WIDTH)
self.list = tk.Listbox(self.frame, name = 'listbox')
self.list.pack()
self.list.bind('<<ListboxSelect>>', self.on_select)
self.list.config(width = LIST_WIDTH)
self.label2 = tk.Label(self.frame, text = "Your saved addresses:")
self.label2.pack()
self.list2 = tk.Listbox(self.frame, name = 'listbox2')
self.list2.pack()
self.list2.bind("<BackSpace>", self.delete_saved_address)
self.list2.config(width = LIST_WIDTH)
self.maps = GoogleAPI()
self.refresh_saved_addresses()
self.thread = threading.Thread(target = self.counter_thread)
self.thread.start()
self.frame.protocol("WM_DELETE_WINDOW", self.on_closing)
self.frame.mainloop()
def on_closing(self):
self.stop_thread = True
self.frame.destroy()
def center_window(self):
self.frame.resizable(False, False)
screen_width = self.frame.winfo_screenwidth()
screen_height = self.frame.winfo_screenheight()
x_cordinate = int((screen_width - window_width) / 2)
y_cordinate = int((screen_height - window_height) / 2)
self.frame.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
def on_select(self, event):
widget = event.widget
if len(widget.curselection()) == 0: return
index = int(widget.curselection()[0])
value = widget.get(index)
place_id = self.predictions[index]['place_id']
json = self.maps.call_place_details(place_id)
components = json['result']['address_components']
postal_code = None
for component in components:
if component['types'][0] == 'postal_code':
postal_code = component['short_name']
#postal_code = component['long_name']
allowed_zip = postal_code in self.allowed_zips
allowed_string = 'ALLOWED' if allowed_zip else 'NOT ALLOWED'
print('postal_code={} ({})'.format(postal_code, allowed_string))
if allowed_zip:
add_address(place_id)
self.refresh_saved_addresses()
def refresh_saved_addresses(self):
self.list2.delete(0, tk.END)
addresses = read_saved_addresses()
n = len(addresses)
for i in range(n):
place_id = addresses[i]
json = self.maps.call_place_details(place_id)
address_name = json['result']['name']
self.list2.insert(i, address_name)
def delete_saved_address(self, event):
widget = event.widget
if len(widget.curselection()) == 0: return
index = int(widget.curselection()[0])
addresses = read_saved_addresses()
address = addresses[index]
delete_address(address)
self.refresh_saved_addresses()
def entry_modified(self, sv):
self.input_text = sv.get()
self.last_modification = time.time()
self.count_time = True
def counter_thread(self):
self.last_modification = time.time()
self.count_time = False
self.stop_thread = False
while not self.stop_thread:
time.sleep(0.1)
if self.count_time:
dt = time.time() - self.last_modification
if dt > 3:
self.count_time = False
self.call_google_maps()
def call_google_maps(self):
self.list.delete(0, tk.END)
json = self.maps.call_autocomplete(self.input_text)
self.predictions = json['predictions']
n = len(self.predictions)
for i in range(n):
prediction = self.predictions[i]
self.list.insert(i, prediction['description'])
def main():
AddressWindow()
if __name__ == '__main__':
main()