convert.py 897 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/python
  2. import xml.etree.ElementTree as ET
  3. import json
  4. import fileinput
  5. import sys
  6. if len(sys.argv) == 2 and sys.argv[1] == '-h':
  7. print(sys.argv[0], ': Convert Practice Timer 1 data into Praceice Timer 2 data')
  8. print(sys.argv[0], ': usage: ', sys.argv[0], ' <input>')
  9. print(sys.argv[0], ': output will be on stdout. Input can also be read from stdin')
  10. exit(0)
  11. xmlData = ""
  12. for f in fileinput.input():
  13. xmlData += f
  14. root = ET.fromstring(xmlData)
  15. headers = root[1]
  16. jo = []
  17. for header in headers:
  18. ho = {
  19. "date": int(round(int(header.get('date')) / 1000)),
  20. "tasks": []
  21. }
  22. for task in header:
  23. to = {
  24. "name": task.get('name'),
  25. "length": int(task.get('length')),
  26. "start": int(round(int(task.get('start')) / 1000))
  27. }
  28. ho['tasks'].append(to)
  29. jo.append(ho)
  30. print(json.dumps(jo))