get_game_dictionary.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import xml.etree.ElementTree as ET
  2. import json
  3. from urllib3 import PoolManager
  4. from fuzzywuzzy import process, fuzz
  5. frame_exceptions = [270803031, 1409258486, 1444415023, 2029842509, 3079625256, 3814159195, 4150853780, 4243692575]
  6. frontwheel_exceptions = [69023253, 1344753875, 1361038541, 1547965258, 2004537892, 2365488570, 2907165694, 3787145210, 3849702821, 4221174482, 998391700, 4249063997]
  7. rearwheel_exceptions = [345690674, 413430806, 1547965258, 1796445915, 1965395406, 2602078812, 2740373137, 4088741326, 4111310185, 4151822963, 961116451, 4097663513]
  8. route_exceptions = {
  9. '2018 UCI WORLDS SHORT LAP': '2018 WORLDS SHORT LAP',
  10. '2015 UCI WORLDS COURSE': 'RICHMOND UCI WORLDS',
  11. 'HILLY ROUTE': 'WATOPIA HILLY ROUTE',
  12. '2019 UCI WORLDS HARROGATE CIRCUIT': '2019 WORLDS HARROGATE CIRCUIT',
  13. 'THE PRETZEL': 'THE LONDON PRETZEL',
  14. 'MOUNTAIN ROUTE': 'WATOPIA MOUNTAIN ROUTE',
  15. 'MOUNTAIN 8': 'WATOPIA MOUNTAIN 8',
  16. 'FIGURE 8': 'WATOPIA FIGURE 8',
  17. 'FIGURE 8 REVERSE': 'WATOPIA FIGURE 8 REVERSE',
  18. 'FLAT ROUTE': 'WATOPIA FLAT ROUTE',
  19. 'THE PRL HALF': 'LONDON PRL HALF',
  20. 'THE PRL FULL': 'LONDON PRL FULL',
  21. 'CASSE PATTES': 'CASSE-PATTES',
  22. 'TIRE BOUCHON': 'TIRE-BOUCHON',
  23. 'HANDFUL OF GRAVEL (CYCLING)': 'HANDFUL OF GRAVEL',
  24. 'HANDFUL OF GRAVEL (RUNNING)': 'HANDFUL OF GRAVEL RUN',
  25. 'WATOPIAS WAISTBAND': 'WATOPIA\'S WAISTBAND',
  26. 'RICHMOND UCI REVERSE': 'RICHMOND 2015 WORLDS REVERSE',
  27. 'CASTLE CRIT (RUNNING)': 'CASTLE CRIT RUN',
  28. 'TRIPLE TWISTS': 'TRIPLE TWIST'
  29. }
  30. with open('../data/game_dictionary.txt') as f:
  31. gd = json.load(f, object_hook=lambda d: {int(k) if k.lstrip('-').isdigit() else k: v for k, v in d.items()})
  32. base_url = 'http://cdn.zwift.com/gameassets/'
  33. filename = 'GameDictionary.xml'
  34. open(filename, 'wb').write(PoolManager().request('GET', base_url + filename).data)
  35. tree = ET.parse(filename)
  36. root = tree.getroot()
  37. gd['headgears'] = [int(x.get('signature')) for x in root.findall("./HEADGEARS/HEADGEAR")]
  38. gd['glasses'] = [int(x.get('signature')) for x in root.findall("./GLASSES/GLASS")]
  39. gd['bikeshoes'] = [int(x.get('signature')) for x in root.findall("./BIKESHOES/BIKESHOE")]
  40. gd['socks'] = [int(x.get('signature')) for x in root.findall("./SOCKS/SOCK")]
  41. gd['jerseys'] = [int(x.get('signature')) for x in root.findall("./JERSEYS/JERSEY")]
  42. frontwheels = {}
  43. for x in root.findall("./BIKEFRONTWHEELS/BIKEFRONTWHEEL"):
  44. signature = int(x.get('signature'))
  45. if not signature in frontwheel_exceptions:
  46. frontwheels[x.get('name')] = signature
  47. rearwheels = {}
  48. for x in root.findall("./BIKEREARWHEELS/BIKEREARWHEEL"):
  49. signature = int(x.get('signature'))
  50. if not signature in rearwheel_exceptions:
  51. rearwheels[x.get('name')] = signature
  52. gd['wheels'] = [(rearwheels[x], frontwheels[x]) for x in rearwheels if x in frontwheels]
  53. gd['runshirts'] = [int(x.get('signature')) for x in root.findall("./RUNSHIRTS/RUNSHIRT")]
  54. gd['runshorts'] = [int(x.get('signature')) for x in root.findall("./RUNSHORTS/RUNSHORT")]
  55. gd['runshoes'] = [int(x.get('signature')) for x in root.findall("./RUNSHOES/RUNSHOE")]
  56. bikeframes = {}
  57. for x in root.findall("./BIKEFRAMES/BIKEFRAME"):
  58. signature = int(x.get('signature'))
  59. if not signature in frame_exceptions:
  60. bikeframes[signature] = x.get('name')
  61. gd['bikeframes'] = bikeframes
  62. routes = {}
  63. for x in root.findall("./ROUTES/ROUTE"):
  64. routes[x.get('name').upper()] = int(x.get('signature'))
  65. achievements = {}
  66. for x in root.findall("./ACHIEVEMENTS/ACHIEVEMENT"):
  67. if x.get('imageName') == "RouteComplete":
  68. name = x.get('name')
  69. signature = int(x.get('signature'))
  70. if name in routes:
  71. achievements[signature] = routes[name]
  72. elif name in route_exceptions:
  73. achievements[signature] = routes[route_exceptions[name]]
  74. else:
  75. best_match = process.extractOne(name, routes.keys(), scorer=fuzz.token_set_ratio)
  76. print("'%s': '%s'," % (name, best_match[0]))
  77. gd['achievements'] = achievements
  78. with open('../data/game_dictionary.txt', 'w') as f:
  79. json.dump(gd, f, indent=2)