model.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
  62. this_loot_level = this_status_status[1][11:].strip()
  63. # print(this_loot_level)
  64. this_integer = _from_roman_to_integer(this_loot_level)
  65. this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
  66. "text": this_status_status, "timestamp": this_status_timestamp}
  67. return this_status_dict
  68. def _read_json_file(this_player_id):
  69. return_status = -1, -1
  70. try:
  71. this_status_file = open(f'status-{this_player_id}.json', 'r')
  72. if this_status_file.mode == 'r':
  73. this_status_raw = this_status_file.read()
  74. if this_status_raw:
  75. temp_status_json = json.loads(this_status_raw)
  76. this_status = int(temp_status_json['integer'])
  77. return_status = temp_status_json, this_status
  78. this_status_file.close()
  79. except FileNotFoundError:
  80. new_status_file = open(f'status-{this_player_id}.json', 'w+')
  81. print('Created new file ... Exiting')
  82. new_status_file.close()
  83. return return_status
  84. def get_json_file(this_player_id):
  85. return _read_json_file(this_player_id)
  86. def extract_hospital_time(this_message_text):
  87. regex = re.compile(r'( (?P<hours>\d+) hrs)?( (?P<minutes>\d+) mins)?( (?P<seconds>\d+) secs)? ')
  88. hours = 0
  89. minutes = 0
  90. seconds = 0
  91. hours_plus = 0
  92. minutes_plus = 0
  93. seconds_plus = 0
  94. if 'hospital' in this_message_text[0]:
  95. time_text = this_message_text[0][15:]
  96. print(time_text)
  97. m = re.match(regex, time_text)
  98. if m:
  99. if m.group('hours'):
  100. hours_plus = m.group('hours')
  101. if m.group('minutes'):
  102. minutes_plus = m.group('minutes')
  103. if m.group('seconds'):
  104. seconds_plus = m.group('seconds')
  105. print(f'Extracted {hours_plus} hrs and {minutes_plus} mins and {seconds_plus} secs')
  106. try:
  107. hours += int(hours_plus)
  108. minutes += int(minutes_plus)
  109. seconds += int(seconds_plus)
  110. except ValueError as err:
  111. print(f'Cannot cast {hours_plus} or {minutes_plus} or {seconds_plus} into int: {err}')
  112. return hours * 60 * 60 + minutes * 60 + seconds
  113. def calculate_time_to_next_attack(this_player_id):
  114. this_status_json, this_status_integer = _read_json_file(this_player_id)
  115. if this_status_json == -1 or this_status_integer == -1:
  116. return -1
  117. seconds_till_level_iv = 0
  118. starting_time = int(this_status_json['timestamp'])
  119. if this_status_integer == 0:
  120. status_text = this_status_json['text']
  121. seconds_till_level_iv += extract_hospital_time(status_text)
  122. if this_status_integer <= 1:
  123. seconds_till_level_iv += 30 * 60
  124. if this_status_integer <= 2:
  125. seconds_till_level_iv += 90 * 60
  126. if this_status_integer <= 3:
  127. seconds_till_level_iv += 210 * 60
  128. apocalypse_time = starting_time + seconds_till_level_iv
  129. time_now = arrow.utcnow().timestamp
  130. diff = apocalypse_time - time_now
  131. print(diff)
  132. diff_in_minutes = int(diff//60)
  133. print(
  134. f'starting time {starting_time}, seconds_till_level_iv {seconds_till_level_iv}, apocalypse_time {apocalypse_time}')
  135. return diff, apocalypse_time