-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryService.py
More file actions
208 lines (166 loc) · 6.44 KB
/
Copy pathqueryService.py
File metadata and controls
208 lines (166 loc) · 6.44 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
#-------------------------------------------------------------------------------
# Name: Universal Data Capture for ArcGIS REST Services
# Purpose: Captures data from ArcGIS REST Services and with modification to
# the code, can ouput to any desired format.
#
# Author: John Spence
#
#
#
# Created: 18 May 2023
# Modified: 20 May 2023
# Modification Purpose: Added in max output restriction on API call. Prevents
# different settings from breaking the data collection.
#
#
#-------------------------------------------------------------------------------
# 888888888888888888888888888888888888888888888888888888888888888888888888888888
# ------------------------------- Configuration --------------------------------
# Adjust the settings below to match your org. eMail functionality is not
# present currently, though obviously can be built in later.
#
# ------------------------------- Dependencies ---------------------------------
#
#
#
# 888888888888888888888888888888888888888888888888888888888888888888888888888888
# Open Data Source
dataSource = r'https://services1.arcgis.com/EYzEZbDhXZjURPbP/arcgis/rest/services/Bellevue_Permits/FeatureServer/0'
dataFields = 'PERMITSTATUS,PERMITNUMBER,PERMITTYPE,PERMITTYPEDESCRIPTION,PERMITYEAR,PROJECTNAME,PROJECTDESCRIPTION,APPLICANT,CONTRACTOR,APPLIEDDATE,ISSUEDDATE,ZONING,LOTSIZE'
dataWKID = 4326
dataFMT = r'json'
dataWhereC = '1=1' # Where clause statement... Example r'PERMITSTATUS=\'Closed\''. Set to 1=1 if you want to pull all records.
# ------------------------------------------------------------------------------
# DO NOT UPDATE BELOW THIS LINE OR RISK DOOM AND DISPAIR! Have a nice day!
# ------------------------------------------------------------------------------
import datetime
import time
import base64
import urllib
import requests
import json
import sys
import os
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
#-------------------------------------------------------------------------------
#
#
# Functions
#
#
#-------------------------------------------------------------------------------
def main():
#-------------------------------------------------------------------------------
# Name: Function - main
# Purpose: Starts the whole thing.
#-------------------------------------------------------------------------------
starttime = startup()
print ('Starup job @: {}'.format(starttime))
payloadProcessing()
stoptime = startup()
print ('Finished job @: {}'.format(stoptime))
return
def startup():
#-------------------------------------------------------------------------------
# Name: Function - main
# Purpose: Starts the whole thing.
#-------------------------------------------------------------------------------
starttime = datetime.datetime.now()
return (starttime)
def captureDS(resultOffset):
#-------------------------------------------------------------------------------
# Name: Function - captureDS
# Purpose:
#-------------------------------------------------------------------------------
values = {'f': dataFMT,
'where': dataWhereC,
'outFields': dataFields,
'outSR': dataWKID,
'resultOffset': resultOffset
'resultRecordCount': 2000
}
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
url = dataSource + r'/query'
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = None
attempt = 0
while response is None:
attempt += 1
if attempt > 3:
time.sleep (10)
attempt = 0
try:
response = urllib.request.urlopen(req)
except:
pass
the_page = response.read().decode(response.headers.get_content_charset())
payload_json = json.loads(the_page)
return (payload_json)
def captureDSCount():
#-------------------------------------------------------------------------------
# Name: Function - captureDSCount
# Purpose:
#-------------------------------------------------------------------------------
values = {'f': dataFMT,
'where': dataWhereC,
'returnCountOnly': 'true'
}
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
url = dataSource + r'/query'
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = None
attempt = 0
while response is None:
attempt += 1
if attempt > 3:
time.sleep (10)
attempt = 0
try:
response = urllib.request.urlopen(req)
except:
pass
the_page = response.read().decode(response.headers.get_content_charset())
payload_json = json.loads(the_page)
return (payload_json)
def payloadProcessing():
#-------------------------------------------------------------------------------
# Name: Function - payloadProcessing
# Purpose:
#-------------------------------------------------------------------------------
payload = captureDSCount()
toGo = int(payload['count']/2000)
toGo += 1
toDo = 0
processedPayload = []
while toDo < toGo:
if toDo == 0:
resultOffset = 0
else:
resultOffset = toDo * 2000
payload = captureDS(resultOffset)
for permits in payload['features']:
pendPayload = []
payloadFields = dataFields.split(sep=',')
for fieldName in payloadFields:
pendPayload.append('{}'.format(permits['attributes']['{}'.format(fieldName)]))
processedPayload.append(pendPayload)
toDo += 1
records = 0
for payload in processedPayload:
records += 1
#print (payload) # Review output to make sure the filters worked right....
#print ('\n')
print ('{} records reviewed'.format(records))
return()
#-------------------------------------------------------------------------------
#
#
# MAIN SCRIPT
#
#
#-------------------------------------------------------------------------------
if __name__ == "__main__":
main()