forked from AlexanderHaug/mirrulations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIKeySetup.py
More file actions
258 lines (190 loc) · 7.36 KB
/
APIKeySetup.py
File metadata and controls
258 lines (190 loc) · 7.36 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
import requests
import os
import random
import string
import json
from appJar import gui
from pathlib import Path
'''
This program will prompt the user for their regulations.gov API Key, as well as the IP and port
of the work server they want to connect to. It will set up the config.json file, store the user's
API Key, and generate a random ClientID.
'''
# Set of overwrite_config to toggle config overwriting. If this is true, it will prompt the user for an IP and port
# every time, and rewrite the config everytime. Otherwise it will only prompt the user for config info if the config
# is missing or corrupt.
overwrite_config = False
submitName = " Submit "
cancelName = " Cancel "
config_ip_submitName = " Enter IP "
config_port_submitName = " Enter Port "
def exit(buttonName):
'''
Closes an error window, called by buttons in error dialogs.
:param buttonName: Passed by appJar when the method is called.
:return:
'''
app.hideSubWindow("errorWindow")
app.hideSubWindow("invalidKeyWindow")
def end(buttonName):
'''
Closes the GUI, called by a button in the final success dialog.
:param buttonName: Passed by appJar when the method is called.
:return:
'''
app.hideSubWindow("successWindow")
app.stop()
def writeAPIKey(key, directory):
'''
Writes the user's API Key to ~/.env/regulationskey.txt
:param key: APIKey to be written to the file.
:return:
'''
if not os.path.exists(directory):
os.makedirs(directory)
try:
f = open(directory + "/regulationskey.txt", "r")
contents = f.read()
f.close()
except FileNotFoundError:
contents = ""
if contents == "":
#Writes the user's API key to the file, with a random string for the client's ID.
file = open(directory + "/regulationskey.txt", "w")
file.write(key + "\n" + ''.join(random.choices(string.ascii_letters + string.digits, k=16)))
file.close()
# Called when a button is pressed on the API key window
def press(buttonName):
'''
Runs when one of the two buttons on the api window is clicked. Does all of the work of processing the API key they gave.
:param buttonName: Passed by appJar when the method is called.
:return:
'''
if buttonName == cancelName:
app.stop()
elif buttonName == submitName:
apiKey = app.getEntry("APIKey")
try:
r = requests.get("https://api.data.gov/regulations/v3/documents.json?api_key=" + apiKey)
except requests.ConnectionError:
app.showSubWindow("errorWindow")
return
'''
Anything 300 & above is an error
429 is the error for a key that's run out of requests
403 is the error for an invalid key
'''
if r.status_code > 299 and r.status_code != 429:
if r.status_code == 403:
app.showSubWindow("invalidKeyWindow")
else:
app.showSubWindow("errorWindow")
else:
writeAPIKey(apiKey, os.getenv("HOME") + "/.env")
app.showSubWindow("successWindow")
def configPress(buttonName):
'''
Called when the button of the config setup window is pressed
:param buttonName: Passed by appJar when the method is called.
:return:
'''
if buttonName == config_ip_submitName:
f = open("config.json", "w")
f.write("{\n" + '\"ip\":' + "\"" + app.getEntry("IP") + "\",\n")
f.close()
app.hideSubWindow("config_ip_window")
app.showSubWindow("config_port_window")
if buttonName == config_port_submitName:
f = open("config.json", "a")
f.write('\"port\":' + "\"" + app.getEntry("Port") + "\"\n}")
f.close()
app.hideSubWindow("config_port_window")
app.showSubWindow("api_key_window")
if __name__ == "__main__":
app = gui("Mirrulations")
# This code builds a window to display an error message.
app.startSubWindow("errorWindow", "Error")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (10, 8)
app.guiPadding = (10, 30)
app.addLabel("errorCode", "We weren't able to connect to regulations.gov.")
app.addLabel("errorMessage", "Please try again later.")
app.addButton(" Okay ", exit)
app.stopSubWindow()
# Done building window.
# This code builds a window to display an invalid API key message.
# The window can be shown by calling: app.showSubWindow("invalidKeyWindow")
app.startSubWindow("invalidKeyWindow", "Error")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (50, 2)
app.addLabel("errorLabel1", "Invalid API Key!")
app.addLabel("errorLabel2", "Please visit:")
app.link("regulations.gov", "https://regulationsgov.github.io/developers/")
app.addLabel("errorLabel3", "for an API Key.")
app.addButton(" Back ", exit)
app.stopSubWindow()
# Done building window.
# Builds a window for the final message, to be displayed if/when everything finishes correctly.
# The window can be shown by calling: app.showSubWindow("successWindow")
app.startSubWindow("successWindow", "Mirrulations")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (50, 2)
app.addLabel("successMessage", "Successfully stored API Key!")
app.addNamedButton(" Done ", "doneButton", end)
app.stopSubWindow()
# Done building window
# Below code builds the API key window
app.startSubWindow("api_key_window")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (10, 8)
app.guiPadding = (10, 30)
app.addLabel("header", "Please enter your regulations.gov API Key.")
app.addLabelEntry("APIKey")
app.addButtons([submitName, cancelName], press)
app.stopSubWindow()
# Done building API key window
#Below code builds the config ip setup window
app.startSubWindow("config_ip_window")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (10, 8)
app.guiPadding = (10, 30)
app.addLabel("config_header_ip", " Please enter the IP of the server \n you wish to connect to.")
app.addLabelEntry("IP")
app.addButton(config_ip_submitName, configPress)
app.stopSubWindow()
# Done building config setup window
# Below code builds the config port setup window
app.startSubWindow("config_port_window")
app.top = True
app.resizable = False
app.font = {'size': 18, 'family': 'Gill Sans'}
app.padding = (10, 8)
app.guiPadding = (10, 30)
app.addLabel("config_header_port", " Please enter the port of the server \n you wish to connect on.")
app.addLabelEntry("Port")
app.addButton(config_port_submitName, configPress)
app.stopSubWindow()
# Done building config setup window
if Path("config.json").exists() and not overwrite_config:
try:
contents = json.loads(open("config.json", "r").read())
contents["ip"]
contents["port"]
except:
print("Exceptional")
app.showSubWindow("config_ip_window")
else:
app.showSubWindow("api_key_window")
else:
app.showSubWindow("config_ip_window")
app.go()