summaryrefslogtreecommitdiff
path: root/steamrelationships/sr.py
blob: 3943770468422222241031b9c73bcc4465077dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
import json

class SteamRelationships:
    session = requests.Session()
    scanlist = []

    def __init__(self, webapikey, recursion=3, timeout=30) -> None:
        self.webapikey = webapikey
        self.recursion = recursion
        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
        response.raise_for_status()
        return response.json()

    def parseFriendsList(self, steamid64 = None) -> list:
        # Retrieve a user's friends list
        friendslist = self._getFriendsList(steamid64)

        final = []
        for friend in friendslist['friendslist']['friends']:
            final.append(friend['steamid'])

        return final

    def recurse(self, startid = None) -> list:
        self.scanlist = self.parseFriendsList(startid) # initialize scanlist
        templist = []

        for friend in self.scanlist:
            templist.append(self.parseFriendsList(friend))

        return templist