forked from dtcooper/python-fitparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.py
More file actions
72 lines (54 loc) · 1.9 KB
/
Copy pathimport.py
File metadata and controls
72 lines (54 loc) · 1.9 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
#!/usr/bin/python
from fitparse import FitFile
import sys
import datetime
import os
import simplejson as json
from datetime import date
def importFitAndReturnJson(fileName, fieldName):
fitfile = FitFile(fileName)
return json.dumps(fitfile.get_messages(fieldName, False, True), sort_keys=True, default=str, iterable_as_array=True)
def importFit(fileName, fieldName):
now = datetime.datetime.now()
if fileName:
print ("importing file %s" % (fileName))
print
print ("Start " + now.strftime("%H:%M:%S"))
print
fitfile = FitFile(fileName)
# Get all data messages that are of type record
for record in fitfile.get_messages(fieldName):
# Go through all the data entries in this record
for record_data in record:
# Print the records name and value (and units if it has any)
if record_data.units:
print (" * %s: %s %s" % (
record_data.name, record_data.value, record_data.units,
))
else:
print (" * %s: %s" % (record_data.name, record_data.value))
print
now = datetime.datetime.now()
print
print ("End " + now.strftime("%H:%M:%S"))
print
else:
print ("Missing filname")
if __name__ == "__main__":
fileName = False
if len(sys.argv) >= 2:
fileName = sys.argv[1]
if len(sys.argv) >= 3:
fieldName = sys.argv[2]
if fieldName == 'all':
fieldName = None;
else:
fieldName = None
if len(sys.argv) >= 4:
asJson = sys.argv[3]
else:
asJson = False
if asJson:
print (importFitAndReturnJson(fileName, fieldName))
else:
print (importFit(fileName, fieldName))