diff options
Diffstat (limited to 'steamrelationships')
| -rw-r--r-- | steamrelationships/constants.py | 10 | ||||
| -rw-r--r-- | 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): | |||
| 9 | class _Const(object): | 9 | class _Const(object): |
| 10 | @constant | 10 | @constant |
| 11 | def STEAMAPI_MAXREQ() -> int: | 11 | def STEAMAPI_MAXREQ() -> int: |
| 12 | return 100000 \ No newline at end of file | 12 | return 100000 |
| 13 | |||
| 14 | @constant | ||
| 15 | def DAY_IN_NANO() -> int: | ||
| 16 | return (8.64 * (10 ** 13)) | ||
| 17 | |||
| 18 | @constant | ||
| 19 | def JSON_INDENT() -> int: | ||
| 20 | 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 | |||
| 6 | CONST = _Const() | 6 | CONST = _Const() |
| 7 | 7 | ||
| 8 | class SteamRelationships: | 8 | class SteamRelationships: |
| 9 | session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection | 9 | session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection |
| 10 | scanlist: list = [] # To be populated by recurse() | 10 | scanlist: list = [] # To be populated by recurse() |
| 11 | reqjson: str = "requests.json" | 11 | reqjson: str = "requests.json" # Path to the JSON file that handles requests |
| 12 | 12 | ||
| 13 | 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: | 13 | 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: |
| 14 | """ | 14 | """ |
| @@ -55,7 +55,7 @@ class SteamRelationships: | |||
| 55 | print(f"[_readjsonfile] File {filepath} does not exist. Generating empty json file...") | 55 | print(f"[_readjsonfile] File {filepath} does not exist. Generating empty json file...") |
| 56 | try: | 56 | try: |
| 57 | with open(filepath, "w+") as newfile: | 57 | with open(filepath, "w+") as newfile: |
| 58 | json.dump({time.time_ns(): [0, []]}, newfile, indent=4) | 58 | json.dump({time.time_ns(): [0, []]}, newfile, indent=CONST.JSON_INDENT) |
| 59 | newfile.close() | 59 | newfile.close() |
| 60 | 60 | ||
| 61 | return self._readjsonfile(filepath) | 61 | return self._readjsonfile(filepath) |
| @@ -85,7 +85,7 @@ class SteamRelationships: | |||
| 85 | 85 | ||
| 86 | # 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)) ] | 86 | # 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)) ] |
| 87 | checktime = time.time_ns() | 87 | checktime = time.time_ns() |
| 88 | if (checktime - int(list(jsoncontents.keys())[-1])) > (8.64 * (10 ** 13)): | 88 | if (checktime - int(list(jsoncontents.keys())[-1])) > CONST.DAY_IN_NANO: |
| 89 | jsoncontents[checktime] = [0, []] | 89 | jsoncontents[checktime] = [0, []] |
| 90 | 90 | ||
| 91 | else: | 91 | else: |
| @@ -102,7 +102,7 @@ class SteamRelationships: | |||
| 102 | # Update the json file | 102 | # Update the json file |
| 103 | try: | 103 | try: |
| 104 | with open(filename, "w+t") as jsonfile: | 104 | with open(filename, "w+t") as jsonfile: |
| 105 | json.dump(jsoncontents, jsonfile, indent=4) | 105 | json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) |
| 106 | jsonfile.close() | 106 | jsonfile.close() |
| 107 | 107 | ||
| 108 | except Exception as e: | 108 | except Exception as e: |
| @@ -118,11 +118,16 @@ class SteamRelationships: | |||
| 118 | print("[_getFriendsList] No steamid64 given") | 118 | print("[_getFriendsList] No steamid64 given") |
| 119 | return {} | 119 | return {} |
| 120 | 120 | ||
| 121 | # Make sure we haven't gone over steam's daily max | ||
| 122 | if self._checkrequests() == 0: | ||
| 123 | print("[_getFriendsList] Max requests reached, refusing to contact steam") | ||
| 124 | return {} | ||
| 125 | |||
| 121 | url: str = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" | 126 | url: str = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" |
| 122 | options: dict = {"key": self.webapikey, "steamid": steamid64, "format": "json"} | 127 | options: dict = {"key": self.webapikey, "steamid": steamid64, "format": "json"} |
| 123 | result: object = None | 128 | result: object = None |
| 124 | 129 | ||
| 125 | # Get the result | 130 | # Contact steam |
| 126 | try: | 131 | try: |
| 127 | result = self.session.get(url, params=options, timeout=self.timeout) | 132 | result = self.session.get(url, params=options, timeout=self.timeout) |
| 128 | 133 | ||
| @@ -164,14 +169,17 @@ class SteamRelationships: | |||
| 164 | print("[_parseFriendsList] Empty friends dict given") | 169 | print("[_parseFriendsList] Empty friends dict given") |
| 165 | return [] | 170 | return [] |
| 166 | 171 | ||
| 167 | 172 | ||
| 168 | friends: list = [] | 173 | people: list = [] |
| 169 | try: | 174 | try: |
| 170 | for friend in friendsdict['friendslist']['friends']: | 175 | for friend in friendsdict["friendslist"]["friends"]: |
| 171 | friends.append(f"{friend}") | 176 | people.append(f'{friend["steamid"]}') |
| 172 | 177 | ||
| 173 | except Exception as e: | 178 | except Exception as e: |
| 174 | print("[_parseFriendsList] Error parsing friendsdict ({e})") | 179 | print("[_parseFriendsList] Error parsing friendsdict ({e})") |
| 175 | friends.clear() | 180 | people.clear() |
| 181 | |||
| 182 | return people | ||
| 176 | 183 | ||
| 177 | return friends \ No newline at end of file | 184 | def basicscan(self, steamid64: str) -> dict: |
| 185 | return {steamid64: self._parseFriendsList(self._getFriendsList(steamid64))} \ No newline at end of file | ||
