model.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. print('alert not in this_status')
  39. pprint(this_status)
  40. this_status['alert'] = {'15': False, '10': False, '5': False, '1': False}
  41. if this_status:
  42. status_return = this_status['status'], this_status['name'], this_status['timestamp'], this_status['alert']
  43. return status_return
  44. def _from_roman_to_integer(roman_number):
  45. integer = 0
  46. if roman_number == 'I':
  47. integer = 1
  48. elif roman_number == 'II':
  49. integer = 2
  50. elif roman_number == 'III':
  51. integer = 3
  52. elif roman_number == 'IV':
  53. integer = 4
  54. elif roman_number == 'V':
  55. integer = 5
  56. elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
  57. integer = 0
  58. else:
  59. print('Unknown Roman number: {}'.format(roman_number))
  60. return integer
  61. def get_loot_level(this_id):
  62. this_loot_level = 0
  63. this_status_status, this_status_name, this_status_timestamp, this_status_alert = _get_status(this_id)
  64. if this_status_status and len(this_status_status) > 1:
  65. if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
  66. this_loot_level = this_status_status[1][11:].strip()
  67. # print(this_loot_level)
  68. this_integer = _from_roman_to_integer(this_loot_level)
  69. this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
  70. "text": this_status_status, "timestamp": this_status_timestamp, "alert": this_status_alert}
  71. return this_status_dict
  72. def _read_json_file(this_player_id):
  73. return_status = -1, -1
  74. try:
  75. this_status_file = open(f'status-{this_player_id}.json', 'r')
  76. if this_status_file.mode == 'r':
  77. this_status_raw = this_status_file.read()
  78. if this_status_raw:
  79. temp_status_json = json.loads(this_status_raw)
  80. this_status = int(temp_status_json['integer'])
  81. return_status = temp_status_json, this_status
  82. this_status_file.close()
  83. except FileNotFoundError:
  84. new_status_file = open(f'status-{this_player_id}.json', 'w+')
  85. print('Created new file ... Exiting')
  86. new_status_file.close()
  87. return return_status
  88. def get_json_file(this_player_id):
  89. return _read_json_file(this_player_id)
  90. def extract_hospital_time(this_message_text, _debug=False):
  91. regex = re.compile(r'( (?P<hours>\d+) hrs)?( (?P<minutes>\d+) mins)?( (?P<seconds>\d+) secs)? ')
  92. hours = 0
  93. minutes = 0
  94. seconds = 0
  95. hours_plus = 0
  96. minutes_plus = 0
  97. seconds_plus = 0
  98. if 'hospital' in this_message_text[0]:
  99. time_text = this_message_text[0][15:]
  100. if _debug:
  101. print(time_text)
  102. m = re.match(regex, time_text)
  103. if m:
  104. if m.group('hours'):
  105. hours_plus = m.group('hours')
  106. if m.group('minutes'):
  107. minutes_plus = m.group('minutes')
  108. if m.group('seconds'):
  109. seconds_plus = m.group('seconds')
  110. if _debug:
  111. print(f'Extracted {hours_plus} hrs and {minutes_plus} mins and {seconds_plus} secs')
  112. try:
  113. hours += int(hours_plus)
  114. minutes += int(minutes_plus)
  115. seconds += int(seconds_plus)
  116. except ValueError as err:
  117. print(f'Cannot cast {hours_plus} or {minutes_plus} or {seconds_plus} into int: {err}')
  118. return hours * 60 * 60 + minutes * 60 + seconds
  119. def calculate_time_to_next_attack(this_player_id):
  120. this_status_json, this_status_integer = _read_json_file(this_player_id)
  121. if this_status_json == -1 or this_status_integer == -1:
  122. return -1
  123. seconds_till_level_iv = 0
  124. starting_time = int(this_status_json['timestamp'])
  125. if this_status_integer == 0:
  126. status_text = this_status_json['text']
  127. seconds_till_level_iv += extract_hospital_time(status_text, _debug=False)
  128. if this_status_integer <= 1:
  129. seconds_till_level_iv += 30 * 60
  130. if this_status_integer <= 2:
  131. seconds_till_level_iv += 60 * 60
  132. if this_status_integer <= 3:
  133. seconds_till_level_iv += 120 * 60
  134. apocalypse_time = starting_time + seconds_till_level_iv
  135. time_now = arrow.utcnow().timestamp
  136. diff = apocalypse_time - time_now
  137. # print(diff)
  138. # diff_in_minutes = int(diff//60)
  139. # print(
  140. # f'starting time {starting_time}, seconds_till_level_iv {seconds_till_level_iv}, ' +
  141. # f'apocalypse_time {apocalypse_time}')
  142. return diff, apocalypse_time