minion.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import requests
  2. import logging
  3. import csv
  4. import os
  5. from datetime import datetime
  6. def get_minion_info(params, headers, logging):
  7. def get_battlegrounds_minion_list():
  8. url = "https://hsreplay.net/analytics/query/battlegrounds_minion_list/"
  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_aggregates_data(aggregates):
  26. win_rates = []
  27. for entry in aggregates:
  28. total_wins = entry["total_wins"]
  29. total_losses = entry["total_losses"]
  30. at_least_one = entry["at_least_one"]
  31. if total_wins != None and total_losses != None:
  32. rate = "{:.2f}%".format(total_wins /
  33. (total_wins + total_losses) * 100)
  34. else:
  35. rate = "None"
  36. if at_least_one != None:
  37. popularity = "{:.2f}%".format(at_least_one)
  38. else:
  39. popularity = "None"
  40. win_rate = {
  41. "combat_round": entry["combat_round"],
  42. "win_rate": rate,
  43. "popularity": popularity
  44. }
  45. win_rates.append(win_rate)
  46. return win_rates
  47. battlegrounds_minion_list = get_battlegrounds_minion_list()
  48. cards = get_cards()
  49. mapping = {}
  50. for entry in cards:
  51. mapping[entry["dbfId"]] = {}
  52. mapping[entry["dbfId"]]["name"] = entry["name"]
  53. if "text" in entry:
  54. mapping[entry["dbfId"]]["text"] = entry["text"]
  55. modified_minion = []
  56. for entry in battlegrounds_minion_list:
  57. minion_dbf_id = entry["minion_dbf_id"]
  58. name = mapping.get(minion_dbf_id)["name"]
  59. if "text" not in mapping.get(minion_dbf_id):
  60. text = ""
  61. else:
  62. text = mapping.get(minion_dbf_id)["text"]
  63. normal_aggregates = entry["normal_aggregates"]
  64. premium_aggregates = entry["premium_aggregates"]
  65. new_entry = {
  66. "ID": minion_dbf_id,
  67. "Name": name,
  68. "Text": text,
  69. "Minion_Tier": entry["minion_tier"],
  70. "Normal": get_aggregates_data(normal_aggregates),
  71. "Premium": get_aggregates_data(premium_aggregates),
  72. }
  73. modified_minion.append(new_entry)
  74. output_directory = "output/minion"
  75. current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
  76. csv_filename = f"{current_time}.csv"
  77. csv_output_file = os.path.join(output_directory, csv_filename)
  78. os.makedirs(output_directory, exist_ok=True)
  79. fieldnames = ["ID", "Name", "Text", "Minion_Tier", "Normal", "Premium"]
  80. with open(csv_output_file, mode="w", newline="",
  81. encoding="utf-8") as outfile:
  82. csv_writer = csv.DictWriter(outfile, fieldnames=fieldnames)
  83. csv_writer.writeheader()
  84. csv_writer.writerows(modified_minion)