import json # from pprint import pprint from dotenv import load_dotenv import os import random import requests # Develop only load_dotenv(verbose=True) def _get_api_key(): torn_api_keys = json.loads(os.environ['TORN_API_KEY']) # pprint(torn_api_keys) torn_api_key = random.choice(torn_api_keys) # print(torn_api_key) return torn_api_key def _get_url(this_id): torn_api_key = _get_api_key() url = f"https://api.torn.com/user/{this_id}?selections=basic,timestamp&key=" url = url+torn_api_key return url def _get_json(this_id): this_url = _get_url(this_id) response = requests.get(this_url) return response def _load_json(this_id): this_response = _get_json(this_id) this_json = json.loads(this_response.text) return this_json def _get_status(this_id): this_json = _load_json(this_id) this_status = this_json # pprint(this_status) status_return = None if this_status: status_return = this_status['status'], this_status['name'], this_status['timestamp'] return status_return def _from_roman_to_integer(roman_number): integer = 0 if roman_number == 'I': integer = 1 elif roman_number == 'II': integer = 2 elif roman_number == 'III': integer = 3 elif roman_number == 'IV': integer = 4 elif roman_number == 'V': integer = 5 elif roman_number == '0' or roman_number == 0 or roman_number == 'N': integer = 0 else: print('Unknown Roman number: {}'.format(roman_number)) return integer def get_loot_level(this_id): this_loot_level = 0 this_status_status, this_status_name, this_status_timestamp = _get_status(this_id) if this_status_status and len(this_status_status) > 1: if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]: this_loot_level = this_status_status[1][11:].strip() # print(this_loot_level) this_integer = _from_roman_to_integer(this_loot_level) this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level, "text": this_status_status, "timestamp": this_status_timestamp} return this_status_dict