Code Cleanup

This commit is contained in:
Robert 2020-01-08 02:26:36 +01:00
parent d2622e25e8
commit fa34f8510d
13 changed files with 98 additions and 50 deletions

View file

@ -1,19 +1,19 @@
'''
A module that sits between the API and the cog
and provides functions for easy communication
with the Steam API
'''
import requests
import json
from util import logging
from util import logging, config
# This module acts as an interface between the bot and the Steam API
# The responses of the API are formatted and returned in a standard format
with open("config.json", "r") as key_file:
json = json.load(key_file)
key = json["api_keys"]["steam"]
key = config.settings["api_keys"]["steam"]
# Returns a (hopefully) valid Steam API URL
def assemble_url(interface_name, method_name, version):
def assemble_url(interface_name: str, method_name: str, version: int) -> str:
return f"http://api.steampowered.com/{interface_name}/{method_name}/v{version}/?key={key}&format=json"
def get_json_request_response(url : str):
def get_json_request_response(url: str):
response = requests.get(url)
if not response.ok:
@ -24,7 +24,7 @@ def get_json_request_response(url : str):
return json
# Finds the SteamID of a user by their Vanity URL
def resolve_vanity_url(vanity_url : str) -> str:
def resolve_vanity_url(vanity_url: str) -> str:
url = assemble_url("ISteamUser", "ResolveVanityURL", 1) + f"&vanityurl={vanity_url}"
json = get_json_request_response(url)
@ -37,7 +37,7 @@ def resolve_vanity_url(vanity_url : str) -> str:
# Gets a users steam level
def get_steam_level(vanity_url : str) -> str:
def get_steam_level(vanity_url: str) -> str:
steamid = resolve_vanity_url(vanity_url)
if steamid is None:
logging.error("Could not resolve Vanity URL")
@ -52,7 +52,7 @@ def get_steam_level(vanity_url : str) -> str:
return json["response"]["player_level"]
# Gets the percentage of players who have a lower level
def get_steam_level_distribution(level : str) -> str:
def get_steam_level_distribution(level: str) -> str:
url = assemble_url("IPlayerService", "GetSteamLevelDistribution", 1) + f"&player_level={level}"
json = get_json_request_response(url)