From ab4bd819ff8a918c7892b94884aabbbcd65f7a42 Mon Sep 17 00:00:00 2001 From: NW/RL Date: Fri, 29 Mar 2024 13:30:56 -0500 Subject: Implement basic scan --- .gitignore | 3 ++- steamrelationships/sr.py | 48 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 8d69a6f..f192137 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -main.py \ No newline at end of file +main.py +*.json \ No newline at end of file diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py index 3943770..a9d8119 100644 --- a/steamrelationships/sr.py +++ b/steamrelationships/sr.py @@ -2,12 +2,15 @@ import requests import json class SteamRelationships: - session = requests.Session() - scanlist = [] - - def __init__(self, webapikey, recursion=3, timeout=30) -> None: + session = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection + scanlist = [] # To be populated by recurse() + + def __init__(self, webapikey, timeout=30) -> None: + ''' + (str/int) webapikey - Steam dev api key required to use Steam's ISteamUser/GetFriendList interface + (int/float) timeout (Default: 30) - Seconds to wait before timing out a request. + ''' self.webapikey = webapikey - self.recursion = recursion self.timeout = timeout def _getFriendsList(self, steamid64 = None) -> dict: @@ -19,15 +22,18 @@ class SteamRelationships: # Format url and make a request url = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" options = {"key": self.webapikey, "steamid": steamid64, "relationship": "friend"} + response = self.session.get(url, params=options, timeout=self.timeout) # GET should be as secure as POST because ssl is being used # TODO: Implement proper error checking so that this doesn't just break if someone has a private friends list - response.raise_for_status() - return response.json() + if response.status_code == requests.codes.ok: + return response.json() + + return None - def parseFriendsList(self, steamid64 = None) -> list: - # Retrieve a user's friends list - friendslist = self._getFriendsList(steamid64) + def parseFriendsList(self, friendslist = None) -> list: + if not friendslist: + return None final = [] for friend in friendslist['friendslist']['friends']: @@ -35,11 +41,21 @@ class SteamRelationships: return final - def recurse(self, startid = None) -> list: - self.scanlist = self.parseFriendsList(startid) # initialize scanlist - templist = [] + def recursive_scan(self, startid = None, recurselevel = 2) -> list: + # Scan an initial id, then populate a list with the scans of each friend + scans = {} + alreadyscanned = [] + + # Start the scan and collect the first user's friend list + scans[startid] = self.parseFriendsList(self._getFriendsList(startid)) + alreadyscanned.append(startid) + + # Scan the current scanid's friends and append them to the list + for friend in scans[startid]: + if friend not in alreadyscanned: + scans[friend] = self.parseFriendsList(self._getFriendsList(friend)) + alreadyscanned.append(friend) - for friend in self.scanlist: - templist.append(self.parseFriendsList(friend)) + #TODO: Find way to repeat this by recurse level - return templist \ No newline at end of file + return scans \ No newline at end of file -- cgit v1.2.3