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 this_status: status_return = this_status['status'], this_status['name'], this_status['timestamp'] 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 = _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} 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): regex = re.compile(r'( (?P\d+) hrs)?( (?P\d+) mins)?( (?P\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:] 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') 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) if this_status_integer >= 1: seconds_till_level_iv += 30 * 60 if this_status_integer >= 2: seconds_till_level_iv += 90 * 60 if this_status_integer >= 3: seconds_till_level_iv += 210 * 60 if this_status_integer == 4: seconds_till_level_iv = 0 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}, apocalypse_time {apocalypse_time}') return diff, apocalypse_time