-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunTracker.py
More file actions
99 lines (82 loc) · 4.2 KB
/
Copy pathrunTracker.py
File metadata and controls
99 lines (82 loc) · 4.2 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
#! python3
# runTracker.py - Scans Google Drive for new .gpx file run tracker data and converts to JSON and posts to myJSON
# Create a .txt file which will contain a list of all new .gpx files
# Search '/Users/emilymoses/Google Drive/' for .gpx files and put the name of that file into the .txt list
# Checks to see if the file name is already in the file list .txt file
# Opens the new file and read it
# Create an API structure to organize how I want the text in the file to be structured
# Send the structured text to myJSON in a POST request.
# master myjson file that stores all of the uris (as links) of each individual run
import glob, os, requests, json
masterUri = "https://api.myjson.com/bins/6lj7d"
def post(data):
# { "title": "All Runs", "paths": [] }
r = requests.post('https://api.myjson.com/bins', json = data)
jsonDictionary = json.loads(r.text)
return jsonDictionary["uri"]
def get(uri):
r = requests.get(uri)
return r.text
def put(uri, data):
r = requests.put(uri, json = data)
def readNewFile(file):
readGpxFile = open(file, 'r')
lines = readGpxFile.readlines()
data = { 'path': [] }
singlePath = {}
for line in lines:
if line.find('<name>') != -1:
startName = line.find('<name>')
endName = line.find('T',startName)
name = line[startName+6:endName+1]
data["name"] = name
if line.find('lat=') != -1:
startLatitude = line.find('lat=')
endLatitude = line.find('"', startLatitude)
latitude = line[startLatitude+5:endLatitude+10]
singlePath['latitude'] = latitude
if line.find('lon=') != -1:
startLongitude = line.find('lon=')
endLongitude = line.find('"', startLongitude)
longitude = line[startLongitude+5:endLongitude+11]
singlePath['longitude'] = longitude
if line.find('<ele>') != -1:
startElevation = line.find('<ele>')
endElevation = line.find('<', startElevation)
elevation = line[startElevation+5:endElevation+13]
singlePath['elevation'] = elevation
if line.find('<time>') != -1:
startTime = line.find('<time>')
endTime = line.find('Z', startTime)
time = line[startTime+6:endTime]
singlePath['time'] = time
data['path'].append(singlePath)
data['path']
singlePath = {}
uri = post(data)
myJson = get(masterUri)
jsonDictionary = json.loads(myJson)
jsonDictionary["paths"].append(uri)
put(masterUri, jsonDictionary)
def saveFilenameToText():
os.chdir('/Users/emilymoses/Google Drive/running_tracks')
nameOfFile = open('/Users/emilymoses/Google Drive/running_tracks/listOfFiles.txt', 'r')
# os.chdir('/Users/emilymoses/test_folder')
# nameOfFile = open('/Users/emilymoses/test_folder/listOfFiles.txt', 'r')
readIt = nameOfFile.readlines()
nameOfFile.close()
for file in glob.glob('*.gpx'):
if file + '\n' not in readIt:
nameOfFile = open('/Users/emilymoses/Google Drive/running_tracks/listOfFiles.txt', 'a')
# nameOfFile = open('/Users/emilymoses/test_folder/listOfFiles.txt', 'a')
nameOfFile.write(file + '\n')
readNewFile(file)
print('Not in folder ', file)
else:
print('Already in folder')
nameOfFile.close()
saveFilenameToText()