model.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import json
  2. # from pprint import pprint
  3. from dotenv import load_dotenv
  4. import os
  5. import random
  6. import requests
  7. # Develop only
  8. load_dotenv(verbose=True)
  9. def _get_api_key():
  10. torn_api_keys = json.loads(os.environ['TORN_API_KEY'])
  11. # pprint(torn_api_keys)
  12. torn_api_key = random.choice(torn_api_keys)
  13. # print(torn_api_key)
  14. return torn_api_key
  15. def _get_url(this_id):
  16. torn_api_key = _get_api_key()
  17. url = f"https://api.torn.com/user/{this_id}?selections=basic,timestamp&key="
  18. url = url+torn_api_key
  19. return url
  20. def _get_json(this_id):
  21. this_url = _get_url(this_id)
  22. response = requests.get(this_url)
  23. return response
  24. def _load_json(this_id):
  25. this_response = _get_json(this_id)
  26. this_json = json.loads(this_response.text)
  27. return this_json
  28. def _get_status(this_id):
  29. this_json = _load_json(this_id)
  30. this_status = this_json
  31. # pprint(this_status)
  32. status_return = None
  33. if this_status:
  34. status_return = this_status['status'], this_status['name'], this_status['timestamp']
  35. return status_return
  36. def _from_roman_to_integer(roman_number):
  37. integer = 0
  38. if roman_number == 'I':
  39. integer = 1
  40. elif roman_number == 'II':
  41. integer = 2
  42. elif roman_number == 'III':
  43. integer = 3
  44. elif roman_number == 'IV':
  45. integer = 4
  46. elif roman_number == 'V':
  47. integer = 5
  48. elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
  49. integer = 0
  50. else:
  51. print('Unknown Roman number: {}'.format(roman_number))
  52. return integer
  53. def get_loot_level(this_id):
  54. this_loot_level = 0
  55. this_status_status, this_status_name, this_status_timestamp = _get_status(this_id)
  56. if this_status_status and len(this_status_status) > 1:
  57. if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
  58. this_loot_level = this_status_status[1][11:].strip()
  59. # print(this_loot_level)
  60. this_integer = _from_roman_to_integer(this_loot_level)
  61. this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
  62. "text": this_status_status, "timestamp": this_status_timestamp}
  63. return this_status_dict