model.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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():
  16. torn_api_key = _get_api_key()
  17. url = "https://api.torn.com/user/4?selections=basic&key="
  18. url = url+torn_api_key
  19. return url
  20. def _get_json():
  21. this_url = _get_url()
  22. response = requests.get(this_url)
  23. return response
  24. def _load_json():
  25. this_response = _get_json()
  26. this_json = json.loads(this_response.text)
  27. return this_json
  28. def _get_status():
  29. this_json = _load_json()
  30. this_status = this_json
  31. # pprint(this_status)
  32. status_return = None
  33. if this_status:
  34. status_return = this_status['status']
  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. else:
  49. print('Unknown Roman number: {}'.format(roman_number))
  50. return integer
  51. def get_loot_level():
  52. this_loot_level = 0
  53. this_status = _get_status()
  54. if this_status and len(this_status) > 1:
  55. if this_status[0] == 'Okay' and 'Loot level' in this_status[1]:
  56. this_loot_level = this_status[1][11:].strip()
  57. # print(this_loot_level)
  58. this_integer = _from_roman_to_integer(this_loot_level)
  59. return this_loot_level,this_integer,this_status