import requests import json class SteamRelationships: 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.timeout = timeout def _getFriendsList(self, steamid64 = None) -> dict: # example url: http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamid=76561197960435530&relationship=friend if not steamid64: print("Requested id must not be blank") return # 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 if response.status_code == requests.codes.ok: return response.json() return None def parseFriendsList(self, friendslist = None) -> list: if not friendslist: return None final = [] for friend in friendslist['friendslist']['friends']: final.append(friend['steamid']) return final 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) #TODO: Find way to repeat this by recurse level return scans