From 2a6da69df4344c4867508ae4ebc576d2059f7780 Mon Sep 17 00:00:00 2001 From: NW/RL Date: Thu, 18 Apr 2024 10:18:17 -0500 Subject: Move variables into a better spot, fix requests file location --- steamrelationships/sr.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'steamrelationships') diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py index 63122b4..8185ebb 100644 --- a/steamrelationships/sr.py +++ b/steamrelationships/sr.py @@ -2,6 +2,7 @@ import requests import json import time import random +import sys from steamrelationships.constants import _Const CONST = _Const() @@ -9,10 +10,7 @@ CONST = _Const() class SteamRelationships: """A class that handles the querring of Steam's web api to request a user's friends list""" - session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection - scanlist: dict = {} # To be populated by scan functions - - def __init__(self, webapikey: str, timeout: int = 30, timeout_retries: int = 5, reqdelay: float = 0.5, delayrand: int = 25, reqsafetybuffer: float = 0.9, reqjson: str = "requests.json") -> None: + def __init__(self, webapikey: str, timeout: int = 30, timeout_retries: int = 5, reqdelay: float = 0.5, delayrand: int = 25, reqsafetybuffer: float = 0.9, reqjson: str = f"{sys.path[0]}/requests.json") -> None: """ (str) webapikey - A Steam "web api key" for grabbing friends lists (get one at https://steamcommunity.com/dev/apikey) @@ -33,10 +31,15 @@ class SteamRelationships: self.timeout: int = int(abs(timeout)) self.timeout_retries: int = int(abs(timeout_retries)) self.reqdelay: float = float(abs(reqdelay)) - self.delayrand: int = int(abs(delayrand) % 100) + self.delayrand: int = int(abs(delayrand) % 100) # clamped to 0-99 self.reqsafetybuffer: float = float(abs(reqsafetybuffer)) self.reqjson: str = str(reqjson) + # Not specified by user, but necessary regardless + self.session: requests.Session = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection + self.scanlist: dict = {} # To be populated by scan functions + + def __str__(self) -> str: return f"Vals:\n\twebapikey: {len(self.webapikey) > 0} (Actual value ommitted for security)\n\n\tTimeout: {self.timeout}\n\tTimeout Retries: {self.timeout_retries}\n\tRequest Delay: {self.reqdelay}\n\tRequest Delay Randomness Factor: +/- {self.delayrand}%\n\tRequest Safety Buffer: {self.reqsafetybuffer} ({self.reqsafetybuffer * CONST.STEAMAPI_MAXREQ} out of {CONST.STEAMAPI_MAXREQ} max requests)\n\tRequests Log Filepath: \"{self.reqjson}\"\n\n\tMost Recent Scan: {self.scanlist}" @@ -81,10 +84,12 @@ class SteamRelationships: return final - def __checkrequests(self, filename: str = "") -> int: + def __checkrequests(self, filename: str = "", increment: bool = True) -> int: """ - __checkrequests(self, filename: str = "") -> int: Check the requests log to make sure Steam's request limit hasn't been passed + __checkrequests(self, filename: str = "", increment: bool = True) -> int: Check the requests log to make sure Steam's request limit hasn't been passed filename: filepath to requests log + increment: Whether to increment the number of requests or not + int (return): The number of requests in the last 24 hours. -1 on error, 0 on too many requests, and >1 if a valid number of requests __checkrequests will create a file at filepath if it doesn't exist. It will also never go over 100,000 requests, regardless of what reqsafetybuffer is @@ -110,22 +115,24 @@ class SteamRelationships: # This bullshit brought to you by: ordered dictionaries currentreqs = jsoncontents[list(jsoncontents.keys())[-1]][0] if currentreqs < (CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer) and currentreqs < CONST.STEAMAPI_MAXREQ: - jsoncontents[list(jsoncontents.keys())[-1]][0] += 1 - jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime) + if increment == True: + jsoncontents[list(jsoncontents.keys())[-1]][0] += 1 + jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime) else: print(f"[__checkrequests] Daily request limit reached ({currentreqs}/{CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer}). Please try again tomorrow, or increase \"reqsafetybuffer\" (currently: {self.reqsafetybuffer})") return 0 # Update the json file - try: - with open(filename, "w+t") as jsonfile: - json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) - jsonfile.close() + if increment == True: + try: + with open(filename, "w+t") as jsonfile: + json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) + jsonfile.close() - except Exception as e: - print(f"[__checkrequests] Could not update json file ({e})") - return -1 + except Exception as e: + print(f"[__checkrequests] Could not update json file ({e})") + return -1 return jsoncontents[list(jsoncontents.keys())[-1]][0] -- cgit v1.2.3