-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsoa2pil.py
executable file
·234 lines (198 loc) · 9.72 KB
/
soa2pil.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
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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import sys
import json
import urllib.request, urllib.error, urllib.parse
import base64
import datetime
import time
import hmac
import hashlib
import base64
import OpenSSL
import uritemplate
import pycountry
import math
import os
import socket
import config
from simplehal import HalDocument, Resolver
from pprint import pprint
#-------------------------------------------------------------------------------------------------------------------#
##################################################################
def getapidata(url, auth): # get the data from the API server
req = urllib.request.Request(url)
req.add_header('Authorization', auth) # build the authorization header
req.add_header("Accept", "application/json")
req.add_header("Content-Type", "application/hal+json")
r = urllib.request.urlopen(req) # open the url resource
j_obj = json.load(r) # convert to JSON
return j_obj # return the JSON object
###################################################################
# get the data from the soaring spot and return it as a HAL document
def gdata(url, key, prt='no'):
global auth # auth and apiurl are globals
global apiurl
j_obj = getapidata(url, auth) # call the fuction that get it
# convert to HAL
if prt == 'yes': # if print is required
print(json.dumps(j_obj, indent=4))
cd = HalDocument.get_data(HalDocument.from_python(
j_obj), apiurl+'rel/' + key) # get the data from the HAL document
return cd
def getemb(base, ctype):
global apiurl
return(base['_embedded'][apiurl+'rel/'+ctype])
def getlinks(base, ctype):
global apiurl
return (base['_links'][apiurl+'rel/'+ctype]['href'])
###################################################################
# see if index day is requestedd
day = sys.argv[1:]
if day and day[0].isdigit(): # if provided and numeric
idx = int(day[0]) # index day
else:
idx = 0
clsreq = sys.argv[2:] # if class is requested
if clsreq:
classreq = clsreq[0] # class requested
#print("TTT", classreq)
else:
classreq = ' ' # none
# ---------------------------------------------------------------- #
print("\n\n")
print("Utility to get the api.soaringspot.com data and extract all the PILOT information needed for FlyTool V1.0")
print("==========================================================================================================\n\n")
print("Index day: ", idx, " Class requested: ", classreq)
print("Reading data from clientid/secretkey files")
# ===== SETUP parameters =======================#
# where to find the SQLITE3 database
SWdbpath = config.DBpath
initials = config.Initials # initials of the files generated
cwd = os.getcwd() # get the current working directory
# where to store the JSON files
cucpath = config.cucFileLocation
# where to find the clientid and secretkey files
secpath = cwd+"/SoaringSpot/"
apiurl = "http://api.soaringspot.com/" # soaringspot API URL
rel = "v1" # we use API version 1
taskType = "SailplaneRacing" # race type
# ==============================================#
hostname = socket.gethostname() # hostname as control
print("Hostname:", hostname)
start_time = time.time() # get the time now
utc = datetime.datetime.utcnow() # the UTC time
# print the time for information only
print("UTC Time is now:", utc)
date = utc.strftime("%Y-%m-%dT%H:%M:%SZ") # get the local time
print(date) #
local_time = datetime.datetime.now() # the local time
print("Local Time is now:", local_time) # print the time for information only
fl_date_time = local_time.strftime("%Y%m%d") # get the local time
print("Config params. SECpath:", secpath)
# nonce=base64.b64encode(OpenSSL.rand.bytes(36)) # get the once base
nonce = base64.b64encode(os.urandom(36)) # get the once base
# open the file with the client id
f = open(secpath+"clientid")
client = f.read() # read it
# clear the whitespace at the end
client = client.rstrip()
# open the file with the secret key
f = open(secpath+"secretkey")
secretkey = f.read() # read it
# clear the whitespace at the end
secretkey = secretkey.rstrip().encode(encoding='utf-8')
message = nonce+date.encode(encoding='utf-8')+client.encode(encoding='utf-8') # build the message
# and the message digest
digest = hmac.new(secretkey, msg=message, digestmod=hashlib.sha256).digest()
signature = str(base64.b64encode(digest).decode()) # build the digital signature
# the AUTHORIZATION ID is built now
auth = apiurl+rel+'/hmac/v1 ClientID="'+client+'",Signature="' + \
signature+'",Nonce="'+nonce.decode(encoding='utf-8')+'",Created="'+date+'" '
#print ("URLiauth:", auth)
# get the initial base of the tree
url1 = apiurl+rel
# get the contest data, first instance
cd = gdata(url1, 'contests', prt='no')[0]
category = cd['category'] # get the main data from the contest
eventname = cd['name']
compid = cd['id']
country = cd['country'] # country code - 2 chars code
compcountry = country # contry as defaults for pilots
# convert the 2 chars ID to the 3 chars ID
ccc = pycountry.countries.get(alpha_2=country)
country = ccc.alpha_3
endate = cd['end_date']
lc = getemb(cd, 'location') # location data
lcname = lc['name'] # location name
print("\n\n= Contest ===============================")
print("Category:", category, "Comp name:", eventname, "Comp ID:", compid)
print("Loc Name:", lcname, "Country: ", country, country, "End date:", endate)
print("=========================================\n\n")
npil = 0 # init the number of pilots
nwarnings = 0 # number of warnings ...
warnings = [] # warnings glider
classes = []
pilots = []
# Build the tracks and turn points, exploring the contestants and task within each class
# go thru the different classes now within the daya
print("Classes:\n========\n\n")
for cl in getemb(cd, 'classes'):
#print "CLCLCL", cl
classname = cl["type"] # search for each class
print("Class:", classname, "\n\n") # search for each class
# search for the contestants on each class
url3 = getlinks(cl, "contestants")
ctt = gdata(url3, "contestants") # get the contestants data
#print "CTTCTT",ctt
pilots = []
for contestants in ctt:
#print "FT", ft, "\n\n"
fname = getemb(contestants, 'pilot')[0]['first_name']
lname = getemb(contestants, 'pilot')[0]['last_name']
# convert it to utf8 in order to avoid problems
pname = fname.encode('utf-8').decode('utf-8')+" "+lname.encode('utf-8').decode('utf-8')
if 'club' in contestants:
club = contestants['club'].encode('utf-8').decode('utf-8')
else:
club = "club_NOTYET"
if 'aircraft_model' in contestants:
ar = contestants['aircraft_model']
else:
ar = "am_NOTYET"
if 'contestant_number' in contestants:
cn = contestants['contestant_number']
else:
cn = "cn_NOTYET"
if 'nationality' in getemb(contestants, 'pilot')[0]:
nation = getemb(contestants, 'pilot')[0]['nationality']
else:
if compcountry != '':
nation = compcountry
else:
nation = "ES" # by default is SPAIN
# convert the 2 chars ID to the 3 chars ID
ccc = pycountry.countries.get(alpha_2=nation)
country3 = ccc.alpha_3
if 'email' in getemb(contestants, 'pilot')[0]:
email = getemb(contestants, 'pilot')[0]['email']
else:
email = "email_NOTYET"
igcid = getemb(contestants, 'pilot')[0]['igc_id']
print("Pilot:", pname, "Club:", club, "CompID:", cn, "Nation:", nation, "Country Code", country3, "Email:", email, "IGCID:", igcid)
npil += 1
pil = {"PilotName": pname, "Club": club, "CompID": cn, "Nation": nation, "CountryCode": country3,
"Email": email, "IgcID": igcid, "PortraitUrl": "http://rankingdata.fai.org/PilotImages/"+str(igcid)+".jpg"}
pilots.append(pil)
cll = {"Class": classname, "Pilots": pilots}
classes.append(cll)
print("----------------------------------------------------------------\n\n")
# print the number of pilots as a reference and control
print("= Pilots ===========================", npil, "\n\n")
FlyTool = {"Compname": eventname, "Category": category, "Country": country,
"EndDate": endate, "Location": lcname, "Classes": classes}
jsonfile = open("FlyTool.json", 'w')
j = json.dumps(FlyTool, indent=4)
jsonfile.write(j)
jsonfile.close()
print(j)