model.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. # from pprint import pprint
  3. import re
  4. from dotenv import load_dotenv
  5. import os
  6. import random
  7. import requests
  8. # Develop only
  9. load_dotenv(verbose=True)
  10. def _get_api_key():
  11. torn_api_keys = json.loads(os.environ['TORN_API_KEY'])
  12. # pprint(torn_api_keys)
  13. torn_api_key = random.choice(torn_api_keys)
  14. # print(torn_api_key)
  15. return torn_api_key
  16. def _get_url(this_id):
  17. torn_api_key = _get_api_key()
  18. url = f"https://api.torn.com/user/{this_id}?selections=basic,timestamp&key="
  19. url = url+torn_api_key
  20. return url
  21. def _get_json(this_id):
  22. this_url = _get_url(this_id)
  23. response = requests.get(this_url)
  24. return response
  25. def _load_json(this_id):
  26. this_response = _get_json(this_id)
  27. this_json = {}
  28. if this_response and this_response.text:
  29. this_json = json.loads(this_response.text)
  30. return this_json
  31. def _get_status(this_id):
  32. this_json = _load_json(this_id)
  33. this_status = this_json
  34. # pprint(this_status)
  35. status_return = None
  36. if this_status:
  37. status_return = this_status['status'], this_status['name'], this_status['timestamp']
  38. return status_return
  39. def _from_roman_to_integer(roman_number):
  40. integer = 0
  41. if roman_number == 'I':
  42. integer = 1
  43. elif roman_number == 'II':
  44. integer = 2
  45. elif roman_number == 'III':
  46. integer = 3
  47. elif roman_number == 'IV':
  48. integer = 4
  49. elif roman_number == 'V':
  50. integer = 5
  51. elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
  52. integer = 0
  53. else:
  54. print('Unknown Roman number: {}'.format(roman_number))
  55. return integer
  56. def get_loot_level(this_id):
  57. this_loot_level = 0
  58. this_status_status, this_status_name, this_status_timestamp = _get_status(this_id)
  59. if this_status_status and len(this_status_status) > 1:
  60. if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
  61. this_loot_level = this_status_status[1][11:].strip()
  62. # print(this_loot_level)
  63. this_integer = _from_roman_to_integer(this_loot_level)
  64. this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
  65. "text": this_status_status, "timestamp": this_status_timestamp}
  66. return this_status_dict
  67. def _read_json_file(this_player_id):
  68. return_status = -1, -1
  69. try:
  70. this_status_file = open(f'status-{this_player_id}.json', 'r')
  71. if this_status_file.mode == 'r':
  72. this_status_raw = this_status_file.read()
  73. if this_status_raw:
  74. temp_status_json = json.loads(this_status_raw)
  75. this_status = int(temp_status_json['integer'])
  76. return_status = temp_status_json, this_status
  77. this_status_file.close()
  78. except FileNotFoundError:
  79. new_status_file = open(f'status-{this_player_id}.json', 'w+')
  80. print('Created new file ... Exiting')
  81. new_status_file.close()
  82. return return_status
  83. def get_json_file(this_player_id):
  84. return _read_json_file(this_player_id)
  85. def extract_hospital_time(this_message_text):
  86. regex = re.compile(r'( (?P<hours>\d+) hrs)?( (?P<minutes>\d+) mins)?( (?P<seconds>\d+) secs)? ')
  87. hours = 0
  88. minutes = 0
  89. seconds = 0
  90. hours_plus = 0
  91. minutes_plus = 0
  92. seconds_plus = 0
  93. if 'hospital' in this_message_text[0]:
  94. time_text = this_message_text[0][15:]
  95. print(time_text)
  96. m = re.match(regex, time_text)
  97. if m:
  98. if m.group('hours'):
  99. hours_plus = m.group('hours')
  100. if m.group('minutes'):
  101. minutes_plus = m.group('minutes')
  102. if m.group('seconds'):
  103. seconds_plus = m.group('seconds')
  104. print(f'Extracted {hours_plus} hrs and {minutes_plus} mins and {seconds_plus} secs')
  105. try:
  106. hours += int(hours_plus)
  107. minutes += int(minutes_plus)
  108. seconds += int(seconds_plus)
  109. except ValueError as err:
  110. print(f'Cannot cast {hours_plus} or {minutes_plus} or {seconds_plus} into int: {err}')
  111. return hours * 60 * 60 + minutes * 60 + seconds
  112. """
  113. def calculate_time_to_next_attack(this_player_id):
  114. pass
  115. # this_status_json, this_status = _read_json_file(this_player_id)
  116. """