-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
119 lines (97 loc) · 3.36 KB
/
API.py
File metadata and controls
119 lines (97 loc) · 3.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
#%%
import requests
from requests.structures import CaseInsensitiveDict
import pandas as pd
import json
import arcpy # not installed on local, but is on Grapery VM
from arcgis.gis import GIS
from time import sleep
import sys
from copy import deepcopy
from datetime import date
# Disable warnings
requests.packages.urllib3.disable_warnings()
#%%
# run this once an hour to get a new auth token
# --------------------------------------
url = ""
# http = urllib3.PoolManager(
# cert_reqs = 'CERT_REQUIRED',
# ca_certs = certifi.where()
# )
headers_token = CaseInsensitiveDict()
headers_token["Accept"] = "application/json"
headers_token["Content-Type"] = "application/json"
data_token = '{ "username":"", "password":""}'
resp_token = requests.post(url, headers = headers_token, data = data_token, verify = False)
auth = resp_token.headers['X-AUTH-TOKEN']
# retrieve property IDs for existing stations
propNum = len(resp_token.json()['properties'])
propID = []
for prop in range(propNum):
propID.append(resp_token.json()['properties'][prop]['rsuname'])
print(auth)
# --------------------------------------
#%%
url_property = ''
headers_property = CaseInsensitiveDict()
headers_property["Content-Type"] = "application/json"
headers_property["X-AUTH-TOKEN"] = auth
params = {'format': 'json'}
props = {
"prop": None
}
shape = []
x_coord = []
y_coord = []
update_date = []
station_id = propID
for ID in propID:
props['prop'] = ID
prop_json = json.dumps(props)
data_property = '{ "jsonrpc": "2.0", "method": "loadProperty", "params": ' + prop_json + ', "id": 1}'
# print(data_property)
# type(data_property)
resp_property = requests.post(url_property, params = params, headers = headers_property, data = data_property, verify = False)
resp_property_df = resp_property.json()
no_sensors = len(resp_property_df['result']['units'])
# for sensor in range(no_sensors):
print(resp_property_df['result']['units'][0]['coord'])
shape.append(arcpy.Point(resp_property_df['result']['units'][0]['coord']['long'], \
(resp_property_df['result']['units'][0]['coord']['lat'])))
x_coord.append(resp_property_df['result']['units'][0]['coord']['long'])
y_coord.append(resp_property_df['result']['units'][0]['coord']['lat'])
update_date.append(date.today())
#%%
# Load in the SQL table
uname = ''
pwd = ''
server_name = ''
instance = ''
database = ''
dbuser = ''
dbpass = ''
db_path = ''
db_sde = ''
table_name = ''
# try:
# gis = GIS('', uname, pwd)
# print("Logged in as " + gis.properties.user.username +
# ' to ' + gis.properties.name + '.')
# except Exception:
# print('Failed to login. Check credentials and network connection and try again.')
# try:
# egdb_conn = arcpy.ArcSDESQLExecute(
# server_name, instance, database, dbuser, dbpass)
# print("Successfully connected to server.")
# print('')
# except Exception as err:
# print(err)
#%%
# insert cursor for table
fields = ['SHAPE@', 'x_coord', 'y_coord', 'station_id', 'update_date']
cursor = arcpy.da.InsertCursor(db_path + db_sde + '\\' + table_name, fields)
for i in range(len(update_date)):
print('Adding row...')
row = [shape[i], x_coord[i], y_coord[i], station_id[i], update_date[i]]
cursor.insertRow(row)