DeepBlue/api/steam.py

62 lines
1.9 KiB
Python
Raw Normal View History

2020-01-08 01:26:36 +00:00
'''
A module that sits between the API and the cog
and provides functions for easy communication
with the Steam API
'''
2020-01-07 01:41:08 +00:00
import requests
import json
2020-01-08 01:26:36 +00:00
from util import logging, config
2020-01-07 01:41:08 +00:00
2020-01-08 01:26:36 +00:00
key = config.settings["api_keys"]["steam"]
2020-01-07 01:41:08 +00:00
# Returns a (hopefully) valid Steam API URL
2020-01-08 01:26:36 +00:00
def assemble_url(interface_name: str, method_name: str, version: int) -> str:
2020-01-07 01:41:08 +00:00
return f"http://api.steampowered.com/{interface_name}/{method_name}/v{version}/?key={key}&format=json"
2020-01-08 01:26:36 +00:00
def get_json_request_response(url: str):
2020-01-07 01:41:08 +00:00
response = requests.get(url)
if not response.ok:
logging.error(f"Steam API response not OK: {response.status_code} [{url}]")
return None
json = response.json()
return json
# Finds the SteamID of a user by their Vanity URL
2020-01-08 01:26:36 +00:00
def resolve_vanity_url(vanity_url: str) -> str:
2020-01-07 01:41:08 +00:00
url = assemble_url("ISteamUser", "ResolveVanityURL", 1) + f"&vanityurl={vanity_url}"
json = get_json_request_response(url)
success = json["response"]["success"]
if success != 1:
logging.error(f"Something went wrong while resolving Vanity URL: {success}")
return None
return json["response"]["steamid"]
# Gets a users steam level
2020-01-08 01:26:36 +00:00
def get_steam_level(vanity_url: str) -> str:
2020-01-07 01:41:08 +00:00
steamid = resolve_vanity_url(vanity_url)
if steamid is None:
logging.error("Could not resolve Vanity URL")
return None
url = assemble_url("IPlayerService", "GetSteamLevel", 1) + f"&steamid={steamid}"
json = get_json_request_response(url)
if len(json["response"]) == 0:
return None
return json["response"]["player_level"]
# Gets the percentage of players who have a lower level
2020-01-08 01:26:36 +00:00
def get_steam_level_distribution(level: str) -> str:
2020-01-07 01:41:08 +00:00
url = assemble_url("IPlayerService", "GetSteamLevelDistribution", 1) + f"&player_level={level}"
json = get_json_request_response(url)
if len(json["response"]) == 0:
return None
return json["response"]["player_level_percentile"]