#! venv/bin/python import json import re import sys from pprint import pprint import model import view def extract_hospital_time(this_message_text, this_message_integer): in_hospital = False time_text = '' hours = 0 minutes = 0 if this_message_integer == 0: if 'hospital' in this_message_text[0]: in_hospital = True time_text = this_message_text[0][15:] print(time_text) m = re.match(regex, time_text) if m: if m.group('hours'): hours = m.group('hours') if m.group('minutes'): minutes = m.group('minutes') print(f'Extracted {hours} hrs and {minutes} mins') if __name__ == '__main__': regex = re.compile(r'( (?P\d+) hrs)? (?P\d+) mins ') for player_id in [4, 15]: status = model.get_loot_level(player_id) # print(status) extract_hospital_time(status['text'], status['integer']) old_status = 0 write_file_next = False try: old_status_file = open(f'status-{player_id}.json', 'r') if old_status_file.mode == 'r': old_status_raw = old_status_file.read() if old_status_raw: old_status_json = json.loads(old_status_raw) old_status = int(old_status_json['integer']) else: write_file_next = True old_status_file.close() except FileNotFoundError: empty_status_file = open(f'status-{player_id}.json', 'w+') print('Created new file ... Exiting') empty_status_file.close() sys.exit(1) # print(old_status) calculate_next_opportunity = False send_message = False # print('if old_status {} != status[1] {}'.format(old_status, status[1])) assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}" assert type(status['integer']) is int, \ f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}" if old_status != status['integer']: calculate_next_opportunity = True write_file_next = True send_message = True # print('write_file_next: {}'.format(write_file_next)) if calculate_next_opportunity: message_text = status['text'] message_integer = status['integer'] if write_file_next: # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]} pprint(status) summary_json = json.dumps(status, indent=2) print(summary_json) write_file = open(f'status-{player_id}.json', 'w') write_file.write(summary_json) write_file.close() write_file.close() if send_message: view.send_message(status)