heroes.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import requests
  2. import logging
  3. import csv
  4. import os
  5. from datetime import datetime
  6. def get_heroes_info(params, headers, logging):
  7. def get_battlegrounds_list_heroes():
  8. url = "https://hsreplay.net/analytics/query/battlegrounds_list_heroes/"
  9. response = requests.get(url, params=params, headers=headers)
  10. if response.status_code == 200:
  11. json_response = response.json()
  12. data = json_response["series"]["data"]
  13. return data
  14. else:
  15. logging.error(f"请求失败,状态码: {response.status_code}")
  16. def get_cards():
  17. url = "https://api.hearthstonejson.com/v1/latest/zhCN/cards.json"
  18. response = requests.get(url, headers=headers)
  19. if response.status_code == 200:
  20. json_response = response.json()
  21. data = json_response
  22. return data
  23. else:
  24. logging.error(f"请求失败,状态码: {response.status_code}")
  25. def get_composition_affinity_by_hero():
  26. url = "https://hsreplay.net/analytics/query/battlegrounds_composition_affinity_by_hero/"
  27. response = requests.get(url, params=params, headers=headers)
  28. if response.status_code == 200:
  29. json_response = response.json()
  30. data = json_response["series"]["data"]
  31. return data
  32. else:
  33. logging.error(f"请求失败,状态码: {response.status_code}")
  34. def get_compositions():
  35. url = "https://hsreplay.net/api/v1/battlegrounds/compositions/"
  36. response = requests.get(url, headers=headers)
  37. if response.status_code == 200:
  38. json_response = response.json()
  39. data = json_response
  40. return data
  41. else:
  42. logging.error(f"请求失败,状态码: {response.status_code}")
  43. compositions = get_compositions()
  44. heroes_stats = get_battlegrounds_list_heroes()
  45. cards = get_cards()
  46. composition_affinity_by_hero = get_composition_affinity_by_hero()
  47. compositions_mapping = {}
  48. for entry in compositions:
  49. compositions_mapping[entry["id"]] = entry["name"]
  50. mapping = {}
  51. for entry in cards:
  52. mapping[entry["dbfId"]] = {}
  53. mapping[entry["dbfId"]]["name"] = entry["name"]
  54. if "heroPowerDbfId" in entry:
  55. mapping[entry["dbfId"]]["heroPowerDbfId"] = entry["heroPowerDbfId"]
  56. if "text" in entry:
  57. mapping[entry["dbfId"]]["text"] = entry["text"]
  58. modified_heroes = []
  59. for entry in heroes_stats[::-1]:
  60. hero_dbf_id = entry["hero_dbf_id"]
  61. name = mapping.get(hero_dbf_id)["name"]
  62. heroPowerDbfId = mapping.get(hero_dbf_id)["heroPowerDbfId"]
  63. composition_of_this_hero = composition_affinity_by_hero[str(
  64. hero_dbf_id)]
  65. modified_compositions = []
  66. temp_points = 0
  67. for entry_composition in composition_of_this_hero:
  68. temp_points += entry_composition["data_points"]
  69. if temp_points != 0:
  70. for entry_composition in composition_of_this_hero:
  71. new_entry_composition = {
  72. "Name":
  73. compositions_mapping[entry_composition["composition_id"]],
  74. "Popularity":
  75. "{:.2f}%".format(
  76. (entry_composition["data_points"] / temp_points) *
  77. 100),
  78. "Comp_Impact":
  79. entry_composition["comp_impact"]
  80. }
  81. modified_compositions.append(new_entry_composition)
  82. new_entry = {
  83. "ID": hero_dbf_id,
  84. "Name": name,
  85. "Text": mapping.get(heroPowerDbfId)["text"],
  86. "Pick_Rate": entry["pick_rate"],
  87. "Avg_Final_Placement": entry["avg_final_placement"],
  88. "Popularity": entry["popularity"],
  89. "Final_Placement_Distribution":
  90. entry["final_placement_distribution"],
  91. "Compositions": modified_compositions
  92. }
  93. modified_heroes.append(new_entry)
  94. output_directory = "output/heroes"
  95. current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
  96. csv_filename = f"{current_time}.csv"
  97. csv_output_file = os.path.join(output_directory, csv_filename)
  98. os.makedirs(output_directory, exist_ok=True)
  99. fieldnames = [
  100. "ID", "Name", "Text", "Pick_Rate", "Avg_Final_Placement", "Popularity",
  101. "Final_Placement_Distribution", "Compositions"
  102. ]
  103. with open(csv_output_file, mode="w", newline="",
  104. encoding="utf-8") as outfile:
  105. csv_writer = csv.DictWriter(outfile, fieldnames=fieldnames)
  106. csv_writer.writeheader()
  107. csv_writer.writerows(modified_heroes)