-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
250 lines (194 loc) · 8.34 KB
/
lambda.py
File metadata and controls
250 lines (194 loc) · 8.34 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
import json
import os
import boto3
import urllib3
from io import BytesIO
import pacificCrestTrailMileMarkers
def lambda_handler(event, context):
startTime = event["startTime"]
startDate = event["startDate"]
totalTime = event["duration"]
startMile = event["startMile"]
finishMile = event["finishMile"]
authCode = event["authCode"]
http = urllib3.PoolManager()
# authorization data
client_id = 'XXXXX'
client_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
code = authCode
grant_type = 'authorization_code'
urlToken = 'https://www.strava.com/api/v3/oauth/token'
headers = {
'Content-Type': 'application/json'
}
body = {
'client_id': client_id,
'client_secret': client_secret,
'code': code,
'grant_type': grant_type
}
# READING IN FILE FROM S3 BUCKET
# create an S3 client
s3 = boto3.client('s3')
# define the bucket and file name
bucket_name = 'baconmaps'
file_name = 'Full_PCT_Updated_Final.gpx'
# download the file from S3 to a temporary file in the /tmp directory
with open('/tmp/' + file_name, 'wb') as f:
s3.download_fileobj(bucket_name, file_name, f)
# create new gpx file with data
# getting time
fullTime = "<time>"+startDate+"T"
# editing gpx file
full = ""
checker = "/ele"
count = 0
countStart = 0
countFinish = 0
# get line number for start and finish
countStart=pacificCrestTrailMileMarkers.lineReturn(int(startMile))
countFinish=pacificCrestTrailMileMarkers.lineReturn(int(finishMile))+299
# read through main gpx file and parse through the data points we need.
# set time stamps for each gpx point aswell
front = ""
full=""
var_start = countStart
var_fin = countFinish
totalLines = (var_fin-var_start)/3
increments = float(totalTime)/float(totalLines)
largeIncAdjust = increments
while largeIncAdjust>1:
largeIncAdjust=largeIncAdjust-1
currentHour = int(startTime[:2])
currentMinute = int(startTime[-2:])
currentSecond = 0
incAdjust = 0
# parse through file to find the gpx points we want
with open("/tmp/Full_PCT_Updated_Final.gpx", 'r') as myFile3:
data=[]
for line_num, line in enumerate(myFile3, start=1):
if line_num<7:
full+=line
if line_num>=var_start:
data.append(line)
if line_num == var_fin:
break
timeStampCount = 0
# add time stamps
for line in data:
# if var_start<count and count<var_fin:
if count<var_fin:
full += line
found = line.find(checker)
if found != -1:
tempHour = str(currentHour)
tempMinute = str(currentMinute)
tempSecond = str(currentSecond)
if int(currentHour) < 10:
tempHour = "0" + str(currentHour)
if int(currentMinute) < 10:
tempMinute = "0" + str(currentMinute)
if int(currentSecond) < 10:
tempSecond = "0" + str(currentSecond)
# original
# full += fullTime + tempHour + ":" + tempMinute + ":" + tempSecond + "Z</time></trkpt>\n"
# new
full += fullTime + tempHour + ":" + tempMinute + ":" + tempSecond + "Z</time>\n"
if increments<1:
incAdjust+=increments
currentSecond+=int(incAdjust)
if incAdjust>1:
incAdjust=incAdjust-1
if increments>1:
incAdjust+=largeIncAdjust
if incAdjust<1:
currentSecond+=int(increments)
if incAdjust>1:
currentSecond+=int(increments)+1
incAdjust=incAdjust-1
if currentSecond > 59:
currentMinute += 1
currentSecond -= 60
if currentMinute > 59:
currentHour += 1
currentMinute -= 60
if currentHour>23:
# make date go to next date
day=startDate[-2:]
month = startDate[5:7]
year = startDate[:4]
if month=="01" or month=="03" or month=="05" or month=="07" or month=="08" or month=="10" or month=="12":
if int(day)>30:
month=int(month)+1
if int(month)>9:
month=str(month)
else:
month = "0"+str(month)
day="01"
if int(month)>12:
month="01"
year = int(year)+1
year=str(year)
else:
day=int(day)+1
day="0"+str(day)
elif month=="02":
if int(day)>27:
day="01"
month="03"
else:
day=int(day)+1
day="0"+str(day)
else:
if int(day)>29:
day="01"
month=int(month)+1
if int(month)>9:
month=str(month)
else:
month = "0"+str(month)
else:
day=int(day)+1
day="0"+str(day)
startDate = year+"-"+month+"-"+day
fullTime = "<time>"+startDate+"T"
# print(startDate)
currentHour-=24
count += 1
full = front + full + "</trkseg></trk></gpx>"
# wrtie the new gpx to this file
with open("/tmp/Full_PCT_Updated_Final.gpx", 'w+') as newFile4:
newFile4.write(full)
# read this new file so we can package it and send to stravas server
with open("/tmp/Full_PCT_Updated_Final.gpx", 'r') as newFile5:
gpx_data=newFile5.read()
# print(gpx_data)
# STRAVA API
# take in strava_access_token from the front end that was sent to back end
response = http.request('POST', urlToken, headers=headers, body=json.dumps(body).encode('utf-8'))
response_data = json.loads(response.data.decode('utf-8'))
strava_access_token = response_data['access_token']
# # read the contents of the file
with open('/tmp/Full_PCT_Updated_Final.gpx', 'r') as f:
gpx_data = f.read()
# create the request headers
headers = {
'Authorization': f'Bearer {strava_access_token}'
}
# print(gpx_data)
# print("above is gpx data")
# create the data payload
data = {
'name':'Uploaded with BaconMaps',
'file': (file_name, gpx_data, 'application/gpx+xml')
}
# send the request
response = http.request(
method='POST',
url='https://www.strava.com/api/v3/uploads?data_type=gpx',
headers=headers,
fields=data
)
# print the response
print(response.data.decode())
return message