Skip to content

Commit

Permalink
Client- Add retrieving the on-hold balance value and remove parse_pri…
Browse files Browse the repository at this point in the history
…ce function (#306)
  • Loading branch information
wolfovik authored and Michał Bukowski committed Nov 5, 2023
1 parent fe0433c commit 092c1f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,21 @@ Inventory items can be merged like in `SteamClient.get_my_inventory` method

`Count` parameter is default max number of items, that can be fetched.

**get_wallet_balance(convert_to_float: bool = True) -> Union[str, float]**
**get_wallet_balance(convert_to_decimal: bool = True, on_hold: bool = False) -> Union[str, float]**

Check account balance of steam acccount. It uses `parse_price` method from utils
to covnert money string to Decimal if `convert_to_decimal` is set to `True`.
to covnert money string to Decimal if `convert_to_decimal` is set to `True`. Otherwise, it will return the value without a decimal point.
If the `on_hold` parameter is set to `True`, it will return the current on-hold balance value.

Example:
```python
from steampy.client import SteamClient
from decimal import Decimal
with SteamClient('MY_API_KEY', 'MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE') as client:
wallet_balance = client.get_wallet_balance()
on_hold_wallet_balance = client.get_wallet_balance(on_hold = True)
assert type(wallet_balance) == Decimal
assert type(on_hold_wallet_balance) == Decimal
```

market methods
Expand Down
22 changes: 14 additions & 8 deletions steampy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import bs4
import urllib.parse as urlparse
from typing import List, Union
from decimal import Decimal

import json
import requests
from steampy import guard
from steampy.confirmation import ConfirmationExecutor
from steampy.exceptions import SevenDaysHoldException, ApiException
from steampy.exceptions import SevenDaysHoldException, ApiException, LoginRequired
from steampy.login import LoginExecutor, InvalidCredentials
from steampy.market import SteamMarket
from steampy.models import Asset, TradeOfferState, SteamUrl, GameOptions
Expand Down Expand Up @@ -353,12 +354,17 @@ def _get_trade_offer_url(trade_offer_id: str) -> str:
return SteamUrl.COMMUNITY_URL + '/tradeoffer/' + trade_offer_id

@login_required
def get_wallet_balance(self, convert_to_decimal: bool = True) -> Union[str, decimal.Decimal]:
url = SteamUrl.STORE_URL + '/account/history/'
response = self._session.get(url)
response_soup = bs4.BeautifulSoup(response.text, "html.parser")
balance = response_soup.find(id='header_wallet_balance').string
# If convert_to_decimal = False, the price will be returned WITHOUT a decimal point.
def get_wallet_balance(self, convert_to_decimal: bool = True, on_hold: bool = False) -> Union[str, decimal.Decimal]:
response = self._session.get("%s/market" % SteamUrl.COMMUNITY_URL)
wallet_info_match = re.search(r'var g_rgWalletInfo = (.*?);', response.text)
if wallet_info_match:
balance_dict_str = wallet_info_match.group(1)
balance_dict = json.loads(balance_dict_str)
else:
raise Exception("Unable to get wallet balance string match")
balance_dict_key = 'wallet_delayed_balance' if on_hold else 'wallet_balance'
if convert_to_decimal:
return parse_price(balance)
return Decimal(balance_dict[balance_dict_key]) / 100
else:
return balance
return balance_dict[balance_dict_key]
7 changes: 0 additions & 7 deletions steampy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ def steam_id_to_account_id(steam_id: str) -> str:
return str(struct.unpack('>L', int(steam_id).to_bytes(8, byteorder = 'big')[4:])[0])


def parse_price(price: str) -> Decimal:
pattern = '\D?(\\d*)(\\.|,)?(\\d*)'
tokens = re.search(pattern, price, re.UNICODE)
decimal_str = tokens.group(1) + '.' + tokens.group(3)
return Decimal(decimal_str)


def calculate_gross_price(price_net: Decimal, publisher_fee: Decimal, steam_fee: Decimal = Decimal('0.05')) -> Decimal:
"""Calculate the price including the publisher's fee and the Steam fee.
Expand Down

0 comments on commit 092c1f8

Please sign in to comment.