find_equip.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #! /usr/bin/env python
  2. # pip install country-converter fuzzywuzzy
  3. #
  4. # use to find and print entries from http://cdn.zwift.com/gameassets/GameDictionary.xml
  5. # scripts/find_equip.py -p emonda -s sworks -o blue -j zwift -e kask
  6. import urllib.request
  7. import json
  8. import country_converter as coco
  9. import argparse
  10. import os
  11. import sys
  12. import xml.etree.ElementTree as ET
  13. from fuzzywuzzy import process
  14. from fuzzywuzzy import fuzz
  15. cc = coco.CountryConverter()
  16. gd_file = 'GameDictionary.xml'
  17. if not os.path.isfile(gd_file):
  18. open(gd_file, 'wb').write(urllib.request.urlopen('http://cdn.zwift.com/gameassets/%s' % gd_file).read())
  19. tree = ET.parse(gd_file)
  20. def get_item(location, item, json_name):
  21. items = {}
  22. for x in tree.findall(location):
  23. items[x.get('name')] = int(x.get('signature'))
  24. best_match = process.extractOne(item, items.keys(), scorer=fuzz.token_set_ratio)
  25. equip = {}
  26. equip[json_name] = items[best_match[0]]
  27. equip[json_name+'_name'] = best_match[0]
  28. return equip
  29. def main(argv):
  30. global args
  31. parser = argparse.ArgumentParser(description='Print out json for bot.txt or ghost_profile.txt')
  32. megroup = parser.add_mutually_exclusive_group()
  33. parser.add_argument('-n', '--nation', help='Specify nation', default=False)
  34. parser.add_argument('-j', '--jersey', help='Get jersey', default=False)
  35. megroup.add_argument('-b', '--bike', help='Get bike', default=False)
  36. parser.add_argument('-w', '--wheels', help='Get wheels', default=False)
  37. parser.add_argument('-e', '--helmet', help='Get helmet', default=False)
  38. parser.add_argument('-s', '--shoes', help='Get shoes', default=False)
  39. parser.add_argument('-o', '--socks', help='Get socks', default=False)
  40. parser.add_argument('-g', '--glasses', help='Get glasses', default=False)
  41. megroup.add_argument('-p', '--paintjob', help='Get paintjob', default=False)
  42. args = parser.parse_args()
  43. total_data = {}
  44. if args.nation:
  45. total_data['country_code'] = cc.convert(names=args.nation, to='ISOnumeric')
  46. if args.jersey:
  47. total_data.update(get_item("./JERSEYS/JERSEY", args.jersey, 'ride_jersey'))
  48. if args.bike:
  49. total_data.update(get_item("./BIKEFRAMES/BIKEFRAME", args.bike, 'bike_frame'))
  50. if args.wheels:
  51. total_data.update(get_item("./BIKEFRONTWHEELS/BIKEFRONTWHEEL", args.wheels, 'bike_wheel_front'))
  52. total_data.update(get_item("./BIKEREARWHEELS/BIKEREARWHEEL", args.wheels, 'bike_wheel_rear'))
  53. if args.helmet:
  54. total_data.update(get_item("./HEADGEARS/HEADGEAR", args.helmet, 'ride_helmet_type'))
  55. if args.socks:
  56. total_data.update(get_item("./SOCKS/SOCK", args.socks, 'ride_socks_type'))
  57. if args.shoes:
  58. total_data.update(get_item("./BIKESHOES/BIKESHOE", args.shoes, 'ride_shoes_type'))
  59. if args.glasses:
  60. total_data.update(get_item("./GLASSES/GLASS", args.glasses, 'glass_type'))
  61. if args.paintjob:
  62. total_data.update(get_item("./PAINTJOBS/PAINTJOB", args.paintjob, 'bike_frame_colour'))
  63. total_data['bike_frame_colour'] <<= 32
  64. total_data.update(get_item("./BIKEFRAMES/BIKEFRAME", total_data['bike_frame_colour_name'].split('-')[0], 'bike_frame'))
  65. data = json.dumps(total_data, indent=2)
  66. print(data)
  67. open('ghost_profile.txt', 'w').write(data)
  68. if __name__ == '__main__':
  69. try:
  70. main(sys.argv)
  71. except KeyboardInterrupt:
  72. pass
  73. except SystemExit as se:
  74. print("ERROR:", se)