diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | steamrelationships/sr.py | 48 |
2 files changed, 34 insertions, 17 deletions
| @@ -1,2 +1,3 @@ | |||
| 1 | __pycache__ | 1 | __pycache__ |
| 2 | main.py \ No newline at end of file | 2 | main.py |
| 3 | *.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 | |||
| 2 | import json | 2 | import json |
| 3 | 3 | ||
| 4 | class SteamRelationships: | 4 | class SteamRelationships: |
| 5 | session = requests.Session() | 5 | session = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection |
| 6 | scanlist = [] | 6 | scanlist = [] # To be populated by recurse() |
| 7 | 7 | ||
| 8 | def __init__(self, webapikey, recursion=3, timeout=30) -> None: | 8 | def __init__(self, webapikey, timeout=30) -> None: |
| 9 | ''' | ||
| 10 | (str/int) webapikey - Steam dev api key required to use Steam's ISteamUser/GetFriendList interface | ||
| 11 | (int/float) timeout (Default: 30) - Seconds to wait before timing out a request. | ||
| 12 | ''' | ||
| 9 | self.webapikey = webapikey | 13 | self.webapikey = webapikey |
| 10 | self.recursion = recursion | ||
| 11 | self.timeout = timeout | 14 | self.timeout = timeout |
| 12 | 15 | ||
| 13 | def _getFriendsList(self, steamid64 = None) -> dict: | 16 | def _getFriendsList(self, steamid64 = None) -> dict: |
| @@ -19,15 +22,18 @@ class SteamRelationships: | |||
| 19 | # Format url and make a request | 22 | # Format url and make a request |
| 20 | url = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" | 23 | url = "https://api.steampowered.com/ISteamUser/GetFriendList/v0001/" |
| 21 | options = {"key": self.webapikey, "steamid": steamid64, "relationship": "friend"} | 24 | options = {"key": self.webapikey, "steamid": steamid64, "relationship": "friend"} |
| 25 | |||
| 22 | response = self.session.get(url, params=options, timeout=self.timeout) # GET should be as secure as POST because ssl is being used | 26 | response = self.session.get(url, params=options, timeout=self.timeout) # GET should be as secure as POST because ssl is being used |
| 23 | 27 | ||
| 24 | # TODO: Implement proper error checking so that this doesn't just break if someone has a private friends list | 28 | # TODO: Implement proper error checking so that this doesn't just break if someone has a private friends list |
| 25 | response.raise_for_status() | 29 | if response.status_code == requests.codes.ok: |
| 26 | return response.json() | 30 | return response.json() |
| 31 | |||
| 32 | return None | ||
| 27 | 33 | ||
| 28 | def parseFriendsList(self, steamid64 = None) -> list: | 34 | def parseFriendsList(self, friendslist = None) -> list: |
| 29 | # Retrieve a user's friends list | 35 | if not friendslist: |
| 30 | friendslist = self._getFriendsList(steamid64) | 36 | return None |
| 31 | 37 | ||
| 32 | final = [] | 38 | final = [] |
| 33 | for friend in friendslist['friendslist']['friends']: | 39 | for friend in friendslist['friendslist']['friends']: |
| @@ -35,11 +41,21 @@ class SteamRelationships: | |||
| 35 | 41 | ||
| 36 | return final | 42 | return final |
| 37 | 43 | ||
| 38 | def recurse(self, startid = None) -> list: | 44 | def recursive_scan(self, startid = None, recurselevel = 2) -> list: |
| 39 | self.scanlist = self.parseFriendsList(startid) # initialize scanlist | 45 | # Scan an initial id, then populate a list with the scans of each friend |
| 40 | templist = [] | 46 | scans = {} |
| 47 | alreadyscanned = [] | ||
| 48 | |||
| 49 | # Start the scan and collect the first user's friend list | ||
| 50 | scans[startid] = self.parseFriendsList(self._getFriendsList(startid)) | ||
| 51 | alreadyscanned.append(startid) | ||
| 52 | |||
| 53 | # Scan the current scanid's friends and append them to the list | ||
| 54 | for friend in scans[startid]: | ||
| 55 | if friend not in alreadyscanned: | ||
| 56 | scans[friend] = self.parseFriendsList(self._getFriendsList(friend)) | ||
| 57 | alreadyscanned.append(friend) | ||
| 41 | 58 | ||
| 42 | for friend in self.scanlist: | 59 | #TODO: Find way to repeat this by recurse level |
| 43 | templist.append(self.parseFriendsList(friend)) | ||
| 44 | 60 | ||
| 45 | return templist \ No newline at end of file | 61 | return scans \ No newline at end of file |
