diff options
| author | NW/RL <NWRL@dabikers.online> | 2024-04-18 12:53:25 -0500 |
|---|---|---|
| committer | NW/RL <NWRL@dabikers.online> | 2024-04-18 12:53:25 -0500 |
| commit | bb9145d9976ed4a0c25387edd82d6b6e3796e889 (patch) | |
| tree | 5a133b582341b2ecd1582ebc75a471b31b944e20 /steamrelationships/sr.py | |
| parent | 2a6da69df4344c4867508ae4ebc576d2059f7780 (diff) | |
Diffstat (limited to 'steamrelationships/sr.py')
| -rw-r--r-- | steamrelationships/sr.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py index 8185ebb..c36bb56 100644 --- a/steamrelationships/sr.py +++ b/steamrelationships/sr.py | |||
| @@ -5,11 +5,13 @@ import random | |||
| 5 | import sys | 5 | import sys |
| 6 | 6 | ||
| 7 | from steamrelationships.constants import _Const | 7 | from steamrelationships.constants import _Const |
| 8 | CONST = _Const() | ||
| 9 | 8 | ||
| 10 | class SteamRelationships: | 9 | class SteamRelationships: |
| 11 | """A class that handles the querring of Steam's web api to request a user's friends list""" | 10 | """A class that handles the querring of Steam's web api to request a user's friends list""" |
| 12 | 11 | ||
| 12 | # If you're changing a constant, you probably want to change it for every instance of the class | ||
| 13 | CONST = _Const() | ||
| 14 | |||
| 13 | 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: | 15 | 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: |
| 14 | """ | 16 | """ |
| 15 | (str) webapikey - A Steam "web api key" for grabbing friends lists (get one at https://steamcommunity.com/dev/apikey) | 17 | (str) webapikey - A Steam "web api key" for grabbing friends lists (get one at https://steamcommunity.com/dev/apikey) |
| @@ -41,7 +43,7 @@ class SteamRelationships: | |||
| 41 | 43 | ||
| 42 | 44 | ||
| 43 | def __str__(self) -> str: | 45 | def __str__(self) -> str: |
| 44 | 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}" | 46 | 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 * self.CONST.STEAMAPI_MAXREQ} out of {self.CONST.STEAMAPI_MAXREQ} max requests)\n\tRequests Log Filepath: \"{self.reqjson}\"\n\n\tMost Recent Scan: {self.scanlist}" |
| 45 | 47 | ||
| 46 | def __readjsonfile(self, filepath: str = "") -> dict: | 48 | def __readjsonfile(self, filepath: str = "") -> dict: |
| 47 | """ | 49 | """ |
| @@ -69,7 +71,7 @@ class SteamRelationships: | |||
| 69 | print(f"[__readjsonfile] File {filepath} does not exist. Generating empty json file...") | 71 | print(f"[__readjsonfile] File {filepath} does not exist. Generating empty json file...") |
| 70 | try: | 72 | try: |
| 71 | with open(filepath, "w+") as newfile: | 73 | with open(filepath, "w+") as newfile: |
| 72 | json.dump({time.time_ns(): [0, []]}, newfile, indent=CONST.JSON_INDENT) | 74 | json.dump({time.time_ns(): [0, []]}, newfile, indent=self.CONST.JSON_INDENT) |
| 73 | newfile.close() | 75 | newfile.close() |
| 74 | 76 | ||
| 75 | return self.__readjsonfile(filepath) | 77 | return self.__readjsonfile(filepath) |
| @@ -108,26 +110,26 @@ class SteamRelationships: | |||
| 108 | 110 | ||
| 109 | # 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)) ] | 111 | # 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)) ] |
| 110 | checktime = time.time_ns() | 112 | checktime = time.time_ns() |
| 111 | if (checktime - int(list(jsoncontents.keys())[-1])) > CONST.DAY_IN_NANO: | 113 | if (checktime - int(list(jsoncontents.keys())[-1])) > self.CONST.DAY_IN_NANO: |
| 112 | jsoncontents[checktime] = [0, []] | 114 | jsoncontents[checktime] = [0, []] |
| 113 | 115 | ||
| 114 | else: | 116 | else: |
| 115 | # This bullshit brought to you by: ordered dictionaries | 117 | # This bullshit brought to you by: ordered dictionaries |
| 116 | currentreqs = jsoncontents[list(jsoncontents.keys())[-1]][0] | 118 | currentreqs = jsoncontents[list(jsoncontents.keys())[-1]][0] |
| 117 | if currentreqs < (CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer) and currentreqs < CONST.STEAMAPI_MAXREQ: | 119 | if currentreqs < (self.CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer) and currentreqs < self.CONST.STEAMAPI_MAXREQ: |
| 118 | if increment == True: | 120 | if increment == True: |
| 119 | jsoncontents[list(jsoncontents.keys())[-1]][0] += 1 | 121 | jsoncontents[list(jsoncontents.keys())[-1]][0] += 1 |
| 120 | jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime) | 122 | jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime) |
| 121 | 123 | ||
| 122 | else: | 124 | else: |
| 123 | print(f"[__checkrequests] Daily request limit reached ({currentreqs}/{CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer}). Please try again tomorrow, or increase \"reqsafetybuffer\" (currently: {self.reqsafetybuffer})") | 125 | print(f"[__checkrequests] Daily request limit reached ({currentreqs}/{self.CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer}). Please try again tomorrow, or increase \"reqsafetybuffer\" (currently: {self.reqsafetybuffer})") |
| 124 | return 0 | 126 | return 0 |
| 125 | 127 | ||
| 126 | # Update the json file | 128 | # Update the json file |
| 127 | if increment == True: | 129 | if increment == True: |
| 128 | try: | 130 | try: |
| 129 | with open(filename, "w+t") as jsonfile: | 131 | with open(filename, "w+t") as jsonfile: |
| 130 | json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) | 132 | json.dump(jsoncontents, jsonfile, indent=self.CONST.JSON_INDENT) |
| 131 | jsonfile.close() | 133 | jsonfile.close() |
| 132 | 134 | ||
| 133 | except Exception as e: | 135 | except Exception as e: |
