model.py 5.2 KB

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