From e69456f7d063157c8926ca3c2cd485b8573f6bf3 Mon Sep 17 00:00:00 2001 From: NW/RL Date: Mon, 1 Apr 2024 23:31:19 -0500 Subject: Reimplement (a non-robust) basic scan --- steamrelationships/constants.py | 10 +++++++++- steamrelationships/sr.py | 36 ++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/steamrelationships/constants.py b/steamrelationships/constants.py index af35733..cf52151 100644 --- a/steamrelationships/constants.py +++ b/steamrelationships/constants.py @@ -9,4 +9,12 @@ def constant(f): class _Const(object): @constant def STEAMAPI_MAXREQ() -> int: - return 100000 \ No newline at end of file + return 100000 + + @constant + def DAY_IN_NANO() -> int: + return (8.64 * (10 ** 13)) + + @constant + def JSON_INDENT() -> int: + return 4 \ No newline at end of file diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py index 22e941e..d5b63e1 100644 --- a/steamrelationships/sr.py +++ b/steamrelationships/sr.py @@ -6,9 +6,9 @@ from steamrelationships.constants import _Const CONST = _Const() class SteamRelationships: - session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection - scanlist: list = [] # To be populated by recurse() - reqjson: str = "requests.json" + session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection + scanlist: list = [] # To be populated by recurse() + reqjson: str = "requests.json" # Path to the JSON file that handles requests def __init__(self, webapikey: str, timeout: int = 30, timeout_retries: int = 5, reqdelay: float = 0.5, delayrand: float = 1.25, reqsafetybuffer: float = 0.9, reqjson: str = "requests.json") -> None: """ @@ -55,7 +55,7 @@ class SteamRelationships: print(f"[_readjsonfile] File {filepath} does not exist. Generating empty json file...") try: with open(filepath, "w+") as newfile: - json.dump({time.time_ns(): [0, []]}, newfile, indent=4) + json.dump({time.time_ns(): [0, []]}, newfile, indent=CONST.JSON_INDENT) newfile.close() return self._readjsonfile(filepath) @@ -85,7 +85,7 @@ class SteamRelationships: # Check the current date. If over 1 day since last entry, add a new entry. Otherwise, edit the current day's entry [note, 1 day in nanoseconds = (8.64 * (10 ** 13)) ] checktime = time.time_ns() - if (checktime - int(list(jsoncontents.keys())[-1])) > (8.64 * (10 ** 13)): + if (checktime - int(list(jsoncontents.keys())[-1])) > CONST.DAY_IN_NANO: jsoncontents[checktime] = [0, []] else: @@ -102,7 +102,7 @@ class SteamRelationships: # Update the json file try: with open(filename, "w+t") as jsonfile: - json.dump(jsoncontents, jsonfile, indent=4) + json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) jsonfile.close() except Exception as e: @@ -118,11 +118,16 @@ class SteamRelationships: print("[_getFriendsList] No steamid64 given") return {} + # Make sure we haven't gone over steam's daily max + if self._checkrequests() == 0: + print("[_getFriendsList] Max requests reached, refusing to contact steam") + return {} + url: str = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" options: dict = {"key": self.webapikey, "steamid": steamid64, "format": "json"} result: object = None - - # Get the result + + # Contact steam try: result = self.session.get(url, params=options, timeout=self.timeout) @@ -164,14 +169,17 @@ class SteamRelationships: print("[_parseFriendsList] Empty friends dict given") return [] - - friends: list = [] + + people: list = [] try: - for friend in friendsdict['friendslist']['friends']: - friends.append(f"{friend}") + for friend in friendsdict["friendslist"]["friends"]: + people.append(f'{friend["steamid"]}') except Exception as e: print("[_parseFriendsList] Error parsing friendsdict ({e})") - friends.clear() + people.clear() + + return people - return friends \ No newline at end of file + def basicscan(self, steamid64: str) -> dict: + return {steamid64: self._parseFriendsList(self._getFriendsList(steamid64))} \ No newline at end of file -- cgit v1.2.3