| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import json
- # from pprint import pprint
- import re
- import arrow
- from dotenv import load_dotenv
- import os
- import random
- import requests
- # Develop only
- load_dotenv(verbose=True)
- def _get_api_key():
- torn_api_keys = json.loads(os.environ['TORN_API_KEY'])
- # pprint(torn_api_keys)
- torn_api_key = random.choice(torn_api_keys)
- # print(torn_api_key)
- return torn_api_key
- def _get_url(this_id):
- torn_api_key = _get_api_key()
- url = f"https://api.torn.com/user/{this_id}?selections=basic,timestamp&key="
- url = url+torn_api_key
- return url
- def _get_json(this_id):
- this_url = _get_url(this_id)
- response = requests.get(this_url)
- return response
- def _load_json(this_id):
- this_response = _get_json(this_id)
- this_json = {}
- if this_response and this_response.text:
- this_json = json.loads(this_response.text)
- return this_json
- def _get_status(this_id):
- this_json = _load_json(this_id)
- this_status = this_json
- # pprint(this_status)
- status_return = None
- if 'alert' not in this_status:
- this_status['alert'] = {'15': False, '10': False, '5': False, '1': False}
- if this_status:
- status_return = this_status['status'], this_status['name'], this_status['timestamp'], this_status['alert']
- return status_return
- def _from_roman_to_integer(roman_number):
- integer = 0
- if roman_number == 'I':
- integer = 1
- elif roman_number == 'II':
- integer = 2
- elif roman_number == 'III':
- integer = 3
- elif roman_number == 'IV':
- integer = 4
- elif roman_number == 'V':
- integer = 5
- elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
- integer = 0
- else:
- print('Unknown Roman number: {}'.format(roman_number))
- return integer
- def get_loot_level(this_id):
- this_loot_level = 0
- this_status_status, this_status_name, this_status_timestamp, this_status_alert = _get_status(this_id)
- if this_status_status and len(this_status_status) > 1:
- if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
- this_loot_level = this_status_status[1][11:].strip()
- # print(this_loot_level)
- this_integer = _from_roman_to_integer(this_loot_level)
- this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
- "text": this_status_status, "timestamp": this_status_timestamp, "alert": this_status_alert}
- return this_status_dict
- def _read_json_file(this_player_id):
- return_status = -1, -1
- try:
- this_status_file = open(f'status-{this_player_id}.json', 'r')
- if this_status_file.mode == 'r':
- this_status_raw = this_status_file.read()
- if this_status_raw:
- temp_status_json = json.loads(this_status_raw)
- this_status = int(temp_status_json['integer'])
- return_status = temp_status_json, this_status
- this_status_file.close()
- except FileNotFoundError:
- new_status_file = open(f'status-{this_player_id}.json', 'w+')
- print('Created new file ... Exiting')
- new_status_file.close()
- return return_status
- def get_json_file(this_player_id):
- return _read_json_file(this_player_id)
- def extract_hospital_time(this_message_text, _debug=False):
- regex = re.compile(r'( (?P<hours>\d+) hrs)?( (?P<minutes>\d+) mins)?( (?P<seconds>\d+) secs)? ')
- hours = 0
- minutes = 0
- seconds = 0
- hours_plus = 0
- minutes_plus = 0
- seconds_plus = 0
- if 'hospital' in this_message_text[0]:
- time_text = this_message_text[0][15:]
- if _debug:
- print(time_text)
- m = re.match(regex, time_text)
- if m:
- if m.group('hours'):
- hours_plus = m.group('hours')
- if m.group('minutes'):
- minutes_plus = m.group('minutes')
- if m.group('seconds'):
- seconds_plus = m.group('seconds')
- if _debug:
- print(f'Extracted {hours_plus} hrs and {minutes_plus} mins and {seconds_plus} secs')
- try:
- hours += int(hours_plus)
- minutes += int(minutes_plus)
- seconds += int(seconds_plus)
- except ValueError as err:
- print(f'Cannot cast {hours_plus} or {minutes_plus} or {seconds_plus} into int: {err}')
- return hours * 60 * 60 + minutes * 60 + seconds
- def calculate_time_to_next_attack(this_player_id):
- this_status_json, this_status_integer = _read_json_file(this_player_id)
- if this_status_json == -1 or this_status_integer == -1:
- return -1
- seconds_till_level_iv = 0
- starting_time = int(this_status_json['timestamp'])
- if this_status_integer == 0:
- status_text = this_status_json['text']
- seconds_till_level_iv += extract_hospital_time(status_text, _debug=False)
- if this_status_integer <= 1:
- seconds_till_level_iv += 30 * 60
- if this_status_integer <= 2:
- seconds_till_level_iv += 60 * 60
- if this_status_integer <= 3:
- seconds_till_level_iv += 120 * 60
- apocalypse_time = starting_time + seconds_till_level_iv
- time_now = arrow.utcnow().timestamp
- diff = apocalypse_time - time_now
- # print(diff)
- # diff_in_minutes = int(diff//60)
- # print(
- # f'starting time {starting_time}, seconds_till_level_iv {seconds_till_level_iv}, ' +
- # f'apocalypse_time {apocalypse_time}')
- return diff, apocalypse_time
|