diff options
| author | NW/RL <NWRL@dabikers.online> | 2024-04-02 00:59:24 -0500 |
|---|---|---|
| committer | NW/RL <NWRL@dabikers.online> | 2024-04-02 00:59:24 -0500 |
| commit | 56bcf6615a51989dfbe4c7dbbb2d03b4af5f32fb (patch) | |
| tree | 044b63a8e4c2a0f1d13cda700c0c1250277eb7e6 /steamrelationships/sr.py | |
| parent | e69456f7d063157c8926ca3c2cd485b8573f6bf3 (diff) | |
Get a basic recursive scan working
Diffstat (limited to 'steamrelationships/sr.py')
| -rw-r--r-- | steamrelationships/sr.py | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py index d5b63e1..4bad716 100644 --- a/steamrelationships/sr.py +++ b/steamrelationships/sr.py | |||
| @@ -7,7 +7,7 @@ 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: dict = {} # To be populated by recursivescan() |
| 11 | reqjson: str = "requests.json" # Path to the JSON file that handles requests | 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: |
| @@ -146,7 +146,7 @@ class SteamRelationships: | |||
| 146 | 146 | ||
| 147 | # Error out on request error | 147 | # Error out on request error |
| 148 | if result.status_code != requests.codes.ok: | 148 | if result.status_code != requests.codes.ok: |
| 149 | print("[_getFriendsList] Got bad status code") | 149 | print(f"[_getFriendsList] Got bad status code (Requested id: {steamid64}, status: {result.status_code})") |
| 150 | return {} | 150 | return {} |
| 151 | 151 | ||
| 152 | # Get the json contents from the response | 152 | # Get the json contents from the response |
| @@ -182,4 +182,61 @@ class SteamRelationships: | |||
| 182 | return people | 182 | return people |
| 183 | 183 | ||
| 184 | def basicscan(self, steamid64: str) -> dict: | 184 | def basicscan(self, steamid64: str) -> dict: |
| 185 | return {steamid64: self._parseFriendsList(self._getFriendsList(steamid64))} \ No newline at end of file | 185 | ''' |
| 186 | basicscan - do a basic scan of someone's steam friends | ||
| 187 | |||
| 188 | PARAMS: | ||
| 189 | (str) steamid64 - The 64 bit steam id of the user you want to scan | ||
| 190 | |||
| 191 | RETURN VALUES: | ||
| 192 | (dict) EMPTY - There was an error scanning the user's friends list | ||
| 193 | (dict) {steamid64: [friendID1, friendID2, ...]} - A dict with a single key, that of the scanned user, which maps to a list of the users's friends | ||
| 194 | |||
| 195 | ''' | ||
| 196 | return {steamid64: self._parseFriendsList(self._getFriendsList(steamid64))} | ||
| 197 | |||
| 198 | def recursivescan(self, steamid64: str, recurselevel: int = 2) -> dict: | ||
| 199 | ''' | ||
| 200 | recursivescan - Scan a user's friends list, then scan their friends as well | ||
| 201 | |||
| 202 | PARAMS: | ||
| 203 | (str) steamid64 - The starting user to scan | ||
| 204 | (int) recurselevel - The number of recursive scans to complete | ||
| 205 | |||
| 206 | RETURN VALUES: | ||
| 207 | (dict) EMPTY - Some catastrophic error has occured and no scan could be started | ||
| 208 | (dict) { | ||
| 209 | steamid64: | ||
| 210 | [friend1id, friend2id, friend3id, ...], | ||
| 211 | friend1id: | ||
| 212 | [other_friend, other_friend, ...], | ||
| 213 | friend2id: | ||
| 214 | [other_friend, other_friend, ...], | ||
| 215 | ... | ||
| 216 | } | ||
| 217 | |||
| 218 | - A dict containing the starting steamid, then the friends contained in the original scan with the results of their scan | ||
| 219 | |||
| 220 | NOTE: | ||
| 221 | Please do not use a value greater than 3. While theoretically any value works, due to the exponential | ||
| 222 | nature of friendship relations, you will very quickly spam Steam with tens of thousands of requests. | ||
| 223 | If this concept is unfamiliar to you, please take a quick glance at the Wikipedia page for | ||
| 224 | "six degress of separation": https://en.wikipedia.org/wiki/Six_degrees_of_separation | ||
| 225 | ''' | ||
| 226 | |||
| 227 | testlist: dict = self.basicscan(steamid64) | ||
| 228 | alreadyscanned: list = [steamid64] | ||
| 229 | |||
| 230 | for i in range(recurselevel): | ||
| 231 | tempdict: dict = {} | ||
| 232 | for person in testlist: | ||
| 233 | for friend in testlist[person]: | ||
| 234 | if friend not in alreadyscanned: | ||
| 235 | tempdict.update(self.basicscan(friend)) | ||
| 236 | alreadyscanned.append(friend) | ||
| 237 | |||
| 238 | testlist.update(tempdict) | ||
| 239 | tempdict.clear() | ||
| 240 | |||
| 241 | self.scanlist.update(testlist) | ||
| 242 | return testlist \ No newline at end of file | ||
