-
Notifications
You must be signed in to change notification settings - Fork 0
/
karoosync.py
executable file
·321 lines (249 loc) · 10.5 KB
/
karoosync.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env /opt/homebrew/bin/python3
import requests
import json
import jwt
import os
import re
from datetime import datetime
import random
import time
BASE_URL = 'https://dashboard.hammerhead.io/v1'
# Produktion
CONFIG_FILE_PATH = "ksync-prod.json"
def random_wait():
wait_time = random.uniform(1, 4)
wait_time = int(wait_time) # Convert wait_time to an integer
print(f"Waiting for {wait_time} seconds... ", end='', flush=True)
for seconds in reversed(range(1, wait_time + 1)):
print(seconds, end=' ', flush=True)
time.sleep(1)
def read_config(file_path):
with open(file_path, 'r') as f:
config = json.load(f)
return config
def get_token(user, password):
DebugLog("Authenticating...")
payload = {
'grant_type': 'password',
'username': user,
'password': password,
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
r = requests.post(
BASE_URL + '/auth/token',
data=payload,
headers=headers,
)
response = r.json()
if 'access_token' in response:
DebugLog("Authentication successful.")
token = response['access_token']
decoded_jwt = jwt.decode(token, algorithms=["HS256"], options={"verify_signature": False})
user_id = decoded_jwt['sub']
return token, user_id
else:
DebugLog(f"Authentication failed. Response: {response}")
return None, None
def DebugLog(message, wait=False):
print(f'{datetime.now()} - {message}')
if wait:
input("Press Enter to continue...")
def get_all_collections(user_id, access_token):
DebugLog("Getting all collections...")
# HTTP request to get all collections
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get(f'https://dashboard.hammerhead.io/v1/users/{user_id}/collections', headers=headers)
if response.status_code != 200:
DebugLog("Failed to fetch collections")
return None
collections = json.loads(response.text)
DebugLog(f" Collections: {collections}")
return collections
def create_collection(user_id, access_token, collection_name, description=""):
DebugLog(f"Creating collection: {collection_name}...")
# HTTP request to create a collection
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
payload = {
'name': collection_name,
'description': description
}
response = requests.post(f'https://dashboard.hammerhead.io/v1/users/{user_id}/collections',
headers=headers, data=json.dumps(payload))
# Handle the response
if response.status_code == 201 or response.status_code == 200:
DebugLog(f"Collection {collection_name} created successfully")
created_collection = json.loads(response.text)
random_wait
return created_collection
else:
DebugLog(f"Failed to create collection - {response.text}")
return None # Handle the failed import scenario
def delete_collection(user_id, access_token, collection_id):
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.delete(f'{BASE_URL}/users/{user_id}/collections/{collection_id}', headers=headers)
if response.status_code != 200:
DebugLog(f"Failed to delete collection {collection_id}")
return False
DebugLog(f"Successfully deleted collection {collection_id}")
random_wait
return True
def get_routes_in_collection(user_id, access_token, collection_id):
DebugLog(f"Getting routes in collection: {collection_id}")
# Prepare the request headers
headers = {
'Authorization': f'Bearer {access_token}',
}
# Prepare the request parameters
params = {
'per_page': 50,
'page': 1,
'search': '',
'order_by': 'NEWEST',
'ascending': 'true',
'include': collection_id,
'exclude': 'archive'
}
# Send the GET request
response = requests.get(BASE_URL + '/users/' + user_id + '/routes', headers=headers, params=params)
# Handle the response
if response.status_code == 200:
routes_data = response.json()
routes = routes_data['data']
DebugLog(f"Successfully fetched {len(routes)} routes in collection {collection_id}")
return routes
else:
DebugLog(f"Failed to fetch routes in collection {collection_id}: {response.status_code}")
return []
def delete_route(user_id, access_token, route_id):
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.delete(f'{BASE_URL}/users/{user_id}/routes/{route_id}', headers=headers)
if response.status_code != 200:
DebugLog(f"Failed to delete route {route_id}")
return False
DebugLog(f"Successfully deleted route {route_id}")
random_wait
return True
def import_route(user_id, access_token, collection_id, file_path):
DebugLog(f"Importing route: {file_path}")
DebugLog(f"Collection_id: {collection_id}")
config = read_config(CONFIG_FILE_PATH)
# Prepare the request headers
headers = {
'Authorization': f'Bearer {access_token}',
}
# Prepare the files data
files = {'file': open(file_path, 'rb')}
# Send the POST request with multipart/form-data
url = f"{BASE_URL}/users/{user_id}/routes/import/file?collectionId={collection_id}"
response = requests.post(url, headers=headers, files=files)
# Handle the response
if response.status_code == 201:
DebugLog("Route import successful")
route_data = response.json()
# for item in route_data:
# DebugLog(str(item))
random_wait()
else:
DebugLog(f"Route import failed with status code: {response.status_code}")
random_wait()
# Handle the failed import scenario
def fix_filenames(directory, recursive=False):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.gpx'):
safe_filename = re.sub(r'[\\/:*?"<>|]', '-', file)
safe_filename = re.sub(r'\s{2,}', ' ', safe_filename).strip()
new_path = os.path.join(root, safe_filename)
if new_path != os.path.join(root, file):
os.rename(os.path.join(root, file), new_path)
DebugLog(f"Renamed file {file} to {safe_filename}")
if not recursive:
break
if not recursive:
break
def find_gpx_files(directory, recursive=False):
gpx_files = {}
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.gpx'):
if root not in gpx_files:
gpx_files[root] = []
gpx_files[root].append(file)
DebugLog(f"Found GPX file {file} in directory {root}")
if not recursive:
break
return gpx_files
def match_routes_and_files(collections, gpx_files, base_directory):
matched = {}
new_collections = {}
for folder, files in gpx_files.items():
collection_name = os.path.basename(folder)
if folder == base_directory:
DebugLog(f"Skipping files in base folder: {base_directory}")
continue
matched_collection = None
for collection in collections:
if collection['name'].lower() == collection_name.lower():
matched_collection = collection
break
if matched_collection:
if matched_collection['name'] not in matched:
matched[matched_collection['name']] = []
matched[matched_collection['name']].extend(files)
DebugLog(f"Matched Collection: {matched_collection['name']}")
else:
if collection_name not in new_collections:
new_collections[collection_name] = []
new_collections[collection_name].extend(files)
DebugLog(f"New Collection: {collection_name}")
DebugLog(f"Matched {len(matched)} collections with GPX files - {matched}")
DebugLog(f"Found {len(new_collections)} new collections - {new_collections}")
return matched, new_collections
def main():
DebugLog("Starting script...")
config = read_config(CONFIG_FILE_PATH)
if config['clean_filenames']: fix_filenames(config['directory'], config['scan_subfolders'])
for user in config['users']:
access_token, user_id = get_token(user['username'], user['password'])
if access_token is None:
DebugLog(f"Skipping user {user['username']} due to authentication failure.")
continue
collections = get_all_collections(user_id, access_token)
file_collections = find_gpx_files(config['directory'], config['scan_subfolders'])
matched_collections, new_collections = match_routes_and_files(collections, file_collections, config['directory'])
for collection_name, file_paths in matched_collections.items():
DebugLog(f"Matching collection name: {collection_name}")
collection_id = next((coll['id'] for coll in collections if coll['name'] == collection_name), None)
DebugLog(f"Matched collection ID: {collection_id}")
if collection_id:
if config['clear_collections']:
DebugLog('Clearing Collection of all Routes')
routes = get_routes_in_collection(user_id, access_token, collection_id)
for route in routes:
delete_route(user_id, access_token, route['id'])
collection_folder = os.path.join(config['directory'], collection_name)
files_in_folder = os.listdir(collection_folder)
for file in files_in_folder:
full_file_path = os.path.join(collection_folder, file)
import_route(user_id, access_token, collection_id, full_file_path)
for new_collection_name in new_collections:
collection_folder = os.path.join(config['directory'], new_collection_name)
collection_id = create_collection(user_id, access_token, new_collection_name)
file_paths = new_collections.get(new_collection_name, [])
for file_path in file_paths:
full_file_path = os.path.abspath(os.path.join(collection_folder, file_path))
import_route(user_id, access_token, collection_id, full_file_path)
DebugLog("Script finished.")
if __name__ == "__main__":
main()