model.py 5.3 KB

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