main.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #! venv/bin/python
  2. import json
  3. # import sys
  4. import math
  5. from pprint import pprint
  6. import arrow
  7. import model
  8. import view
  9. if __name__ == '__main__':
  10. for player_id in [4, 15]:
  11. status = model.get_loot_level(player_id)
  12. # print(status)
  13. # seconds = extract_hospital_time(status['text'], status['integer'])
  14. write_file_next = False
  15. # Get data from stored json file
  16. old_status_json, old_status = model.get_json_file(player_id)
  17. if old_status_json == -1 or old_status == -1:
  18. write_file_next = True
  19. calculate_next_opportunity = False
  20. send_message = False
  21. # print('if old_status {} != status[\'integer\'] {}'.format(old_status, status['integer']))
  22. assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}"
  23. assert type(status['integer']) is int, \
  24. f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}"
  25. timestamp_when_okay = int(status['timestamp'])
  26. if status['integer'] == 0:
  27. seconds_in_hosp = model.extract_hospital_time(status['text'], _debug=True)
  28. timestamp_when_okay += seconds_in_hosp
  29. if old_status != status['integer']:
  30. calculate_next_opportunity = True
  31. write_file_next = True
  32. send_message = True
  33. # print('write_file_next: {}'.format(write_file_next))
  34. if calculate_next_opportunity:
  35. message_text = status['text']
  36. message_integer = status['integer']
  37. if write_file_next:
  38. # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]}
  39. pprint(status)
  40. summary_json = json.dumps(status, indent=2)
  41. print(summary_json)
  42. write_file = open(f'status-{player_id}.json', 'w')
  43. write_file.write(summary_json)
  44. write_file.close()
  45. write_file.close()
  46. seconds_to_go, attack_time = model.calculate_time_to_next_attack(player_id)
  47. player_name = status['name']
  48. attack_time_object = arrow.get(attack_time)
  49. attack_time_time = attack_time_object.format('hh:mm A')
  50. minutes_to_go = math.floor(seconds_to_go / 60)
  51. t = f'In < {minutes_to_go} mins ({attack_time_time}) {player_name} [{player_id}] reaches level IV'
  52. print(t)
  53. status['time_string'] = t
  54. if minutes_to_go < 20 and minutes_to_go % 5 == 0 and minutes_to_go != 0:
  55. name_alert = status['name']
  56. id_alert = status['id']
  57. url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}'
  58. alert = f'Alert: {minutes_to_go} mins to attack [{name_alert} \[{id_alert}\]]({url_alert}). ' + \
  59. f'Be there at {attack_time_time}!'
  60. print('Sending alert')
  61. view.send_alert(alert)
  62. if send_message:
  63. view.send_message(status)