From 7490392ce69b7615bfa62be2980e2eaa0156e8cd Mon Sep 17 00:00:00 2001 From: hodakin Date: Thu, 15 Feb 2024 15:29:07 +0200 Subject: [PATCH 1/2] methods for adding, accepting and deleting friends --- README.md | 24 ++++++++++++++++++++++++ steampy/client.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/README.md b/README.md index 9178bf6..3f13a06 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,30 @@ with SteamClient('MY_API_KEY', 'MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD assert type(on_hold_wallet_balance) == Decimal ``` + +**add_friend(steam_id: str) -> bool** + +**accept_friend(steam_id: str) -> bool** + +**remove_friend(steam_id: str) -> bool** + +Using `SteamClient.login` method is required before usage. +These methods perform the operations of adding, accepting and deleting friends using the provided `steam_id` of the friend. +If the request was successful, True is returned, otherwise False. +`remove_friend` method returns True even if `steam_id` has never been a friend + +Example: +```python +from steampy.client import SteamClient + + +steam_client = SteamClient('MY_API_KEY') +steam_client.login('MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE') + +# if response True - was success +response = steam_client.add_friend("FRIEND_STEAM_ID") +``` + market methods ============== diff --git a/steampy/client.py b/steampy/client.py index ece1057..702176d 100644 --- a/steampy/client.py +++ b/steampy/client.py @@ -416,3 +416,39 @@ def get_wallet_balance(self, convert_to_decimal: bool = True, on_hold: bool = Fa return Decimal(balance_dict[balance_dict_key]) / 100 else: return balance_dict[balance_dict_key] + + @login_required + def add_friend(self, steam_id: str) -> bool: + response = self._friend_ajax_request(steam_id) + return True if response else False + + @login_required + def accept_friend(self, steam_id: str) -> bool: + return self._friend_ajax_request(steam_id, accept=1) + + _headers = { + "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" + } + + def _friend_ajax_request(self, steam_id: str, accept: int = 0) -> dict: + url = f'{SteamUrl.COMMUNITY_URL}/actions/AddFriendAjax' + data = { + "sessionID": self._get_session_id(), + "steamid": steam_id, + "accept_invite": accept + } + # returns True or json object if request was succes, otherwise False + return self._session.post(url, data=data, headers=self._headers).json() + + @login_required + def remove_friend(self, steam_id: str) -> bool: + url = f'{SteamUrl.COMMUNITY_URL}/actions/RemoveFriendAjax' + data = { + "sessionID": self._get_session_id(), + "steamid": steam_id + } + # returns True if request was succes, otherwise False + # returns True even if `steam_id` has never been a friend + return self._session.post( + url, data=data, headers=self._headers).json() + From 0452e2af32b5240add2df4d0a86e413aa704df54 Mon Sep 17 00:00:00 2001 From: hodakin Date: Sun, 25 Feb 2024 13:55:54 +0200 Subject: [PATCH 2/2] update pull request with resolved remarks --- steampy/client.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/steampy/client.py b/steampy/client.py index 702176d..ebfb300 100644 --- a/steampy/client.py +++ b/steampy/client.py @@ -427,28 +427,39 @@ def accept_friend(self, steam_id: str) -> bool: return self._friend_ajax_request(steam_id, accept=1) _headers = { - "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } def _friend_ajax_request(self, steam_id: str, accept: int = 0) -> dict: url = f'{SteamUrl.COMMUNITY_URL}/actions/AddFriendAjax' data = { - "sessionID": self._get_session_id(), - "steamid": steam_id, - "accept_invite": accept + 'sessionID': self._get_session_id(), + 'steamid': steam_id, + 'accept_invite': accept } + response = self._session.post(url, data=data, headers=self._headers) + + if response.status_code != 200: + raise ApiException( + f'Request failed with status code {response.status_code}') + # returns True or json object if request was succes, otherwise False - return self._session.post(url, data=data, headers=self._headers).json() + return response.json() @login_required def remove_friend(self, steam_id: str) -> bool: url = f'{SteamUrl.COMMUNITY_URL}/actions/RemoveFriendAjax' data = { - "sessionID": self._get_session_id(), - "steamid": steam_id + 'sessionID': self._get_session_id(), + 'steamid': steam_id } + response = self._session.post(url, data=data, headers=self._headers) + + if response.status_code != 200: + raise ApiException( + f'Request failed with status code {response.status_code}') + # returns True if request was succes, otherwise False # returns True even if `steam_id` has never been a friend - return self._session.post( - url, data=data, headers=self._headers).json() + return response.json()