get_events.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os
  2. import xml.etree.ElementTree as ET
  3. import re
  4. import json
  5. import subprocess
  6. worlds = 'C:\\Program Files (x86)\\Zwift\\assets\\Worlds'
  7. world_to_course = {
  8. '1': (6, 'Watopia'),
  9. '2': (2, 'Richmond'),
  10. '3': (7, 'London'),
  11. '4': (8, 'New York'),
  12. '5': (9, 'Innsbruck'),
  13. '6': (10, 'Bologna'),
  14. '7': (11, 'Yorkshire'),
  15. '8': (12, 'Crit City'),
  16. '9': (13, 'Makuri Islands'),
  17. '10': (14, 'France'),
  18. '11': (15, 'Paris'),
  19. '12': (16, 'Gravel Mountain'),
  20. '13': (17, 'Scotland')
  21. }
  22. data = []
  23. for directory in os.listdir(worlds):
  24. world = directory[5:]
  25. if os.path.isdir(os.path.join(worlds, directory)) and world in world_to_course:
  26. subprocess.run(['wad_unpack.exe', os.path.join(worlds, directory, 'data_1.wad')])
  27. routes = os.path.join('Worlds', directory, 'routes')
  28. for file in os.listdir(routes):
  29. with open(os.path.join(routes, file)) as f:
  30. xml = f.read()
  31. tree = ET.fromstring(re.sub(r"(<\?xml[^>]+\?>)", r"\1<root>", xml) + "</root>")
  32. route = tree.find('route')
  33. if route.get('eventOnly') == '1':
  34. wname = world_to_course[world][1]
  35. name = route.get('name').strip()
  36. if not name.startswith(wname):
  37. name = '%s - %s' % (wname, name)
  38. event = {
  39. 'name': name,
  40. 'route': int(route.get('nameHash')),
  41. 'distance': round(float(route.get('distanceInMeters')) + float(route.get('leadinDistanceInMeters')), 1),
  42. 'course': world_to_course[world][0],
  43. 'sport': 1 if route.get('sportType') == '2' else 0
  44. }
  45. data.append(event)
  46. with open('../data/events.txt', 'w') as f:
  47. json.dump(sorted(data, key=lambda row: row['name']), f, indent=2)