model.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = {}
  27. if this_response and this_response.text:
  28. this_json = json.loads(this_response.text)
  29. return this_json
  30. def _get_status(this_id):
  31. this_json = _load_json(this_id)
  32. this_status = this_json
  33. # pprint(this_status)
  34. status_return = None
  35. if this_status:
  36. status_return = this_status['status'], this_status['name'], this_status['timestamp']
  37. return status_return
  38. def _from_roman_to_integer(roman_number):
  39. integer = 0
  40. if roman_number == 'I':
  41. integer = 1
  42. elif roman_number == 'II':
  43. integer = 2
  44. elif roman_number == 'III':
  45. integer = 3
  46. elif roman_number == 'IV':
  47. integer = 4
  48. elif roman_number == 'V':
  49. integer = 5
  50. elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
  51. integer = 0
  52. else:
  53. print('Unknown Roman number: {}'.format(roman_number))
  54. return integer
  55. def get_loot_level(this_id):
  56. this_loot_level = 0
  57. this_status_status, this_status_name, this_status_timestamp = _get_status(this_id)
  58. if this_status_status and len(this_status_status) > 1:
  59. if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
  60. this_loot_level = this_status_status[1][11:].strip()
  61. # print(this_loot_level)
  62. this_integer = _from_roman_to_integer(this_loot_level)
  63. this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
  64. "text": this_status_status, "timestamp": this_status_timestamp}
  65. return this_status_dict