Skip to content

Commit

Permalink
Refactor after updating python version (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustKappaMan authored and Michał Bukowski committed Nov 5, 2023
1 parent 092c1f8 commit d540b3d
Show file tree
Hide file tree
Showing 16 changed files with 558 additions and 468 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ Inventory items can be merged like in `SteamClient.get_my_inventory` method

**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`. Otherwise, it will return the value without a decimal point.
Check account balance of steam acccount. It converts money string to Decimal if `convert_to_decimal` is set to `True`,
otherwise, it will return the value string without a decimal point.
If the `on_hold` parameter is set to `True`, it will return the current on-hold balance value.

Example:
Expand Down
4 changes: 3 additions & 1 deletion examples/desktop_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# From your Steamguard file

from steampy.guard import generate_confirmation_key, generate_one_time_code


shared_secret = ''
identity_secret = ''

one_time_authentication_code = generate_one_time_code(shared_secret)
print(one_time_authentication_code)

confirmation_key = generate_confirmation_key(identity_secret, 'conf')
print(confirmation_key)
print(confirmation_key)
51 changes: 29 additions & 22 deletions examples/inventory.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
from json import dump
import json

from steampy.client import SteamClient, InvalidCredentials
from steampy.models import GameOptions

#Your steam username

# Your Steam username
username = ''

#Path to steam guard file
# Path to Steamguard file
steam_guard_path = ''

#Your steam password
# Your Steam password
password = ''

#Your steam api key (http://steamcommunity.com/dev/apikey)
# Your Steam api key (http://steamcommunity.com/dev/apikey)
steam_key = ''

#The game's app id. If not supplied, it will ask for input later
# The game's app id. If not supplied, it will ask for input later
app_id = ''

#The game's context id. If not supplied, it will ask for input later
# The game's context id. If not supplied, it will ask for input later
context_id = ''

#Log into steam. First, we create the SteamClient object, then we login
print("Logging into steam")

# Log in into Steam. First, we create the SteamClient object, then we log in.
print('Logging in into Steam...')
steam_client = SteamClient(steam_key)
try:
steam_client.login(username, password, steam_guard_path)
except (ValueError, InvalidCredentials):
print('Your login credentials are invalid')
print('Your login credentials are invalid!')
exit(1)
print("Finished! Logged into steam")
else:
print('Finished! Logged in into Steam')

#we will ask them for the game's app id and context id of the inventory

# We'll ask them for the game's app id and context id of the inventory
if not app_id:
app_id = input('What is the app id?\n')
if not context_id:
context_id = input('What is the context id of that game\'s inventory? (usually 2)\n')
context_id = input("What is the context id of that game's inventory? (usually 2)\n")


#get all the items in inventory, and save each name of item and the amount
# Get all the items in the inventory. Save name and amount for each item.
print('Obtaining inventory...')
item_amounts = {}
inventory = steam_client.get_my_inventory(GameOptions(app_id,context_id))
inventory = steam_client.get_my_inventory(GameOptions(app_id, context_id))
for item in inventory.values():
if item["market_name"] in item_amounts:
if item['market_name'] in item_amounts:
item_amounts[item['market_name']] += 1
else:
item_amounts[item['market_name']] = 1
print('Done reading inventory for game: {}'.format(app_id))
print(f'Done obtaining inventory for the game: {app_id}')


#dump all the information into inventory_(app_id)_(context_id).json file
print('Saving information....')
with open('inventory_{0}_{1}.json'.format(app_id, context_id), 'w') as file:
dump(item_amounts, file)
print('Done! Saved to file: inventory_{0}_{1}.json'.format(app_id, context_id))
# Dump all the info to inventory_(app_id)_(context_id).json file
print('Saving information...')
with open(f'inventory_{app_id}_{context_id}.json', 'w') as file:
json.dump(item_amounts, file)
print(f'Done! Saved to file: inventory_{app_id}_{context_id}.json')
24 changes: 15 additions & 9 deletions examples/storehouse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time

from steampy.client import SteamClient, TradeOfferState


# Set API key
api_key = ''
# Set path to SteamGuard file
Expand All @@ -13,35 +15,39 @@

def main():
print('This is the donation bot accepting items for free.')

if not are_credentials_filled():
print('You have to fill credentials in storehouse.py file to run the example')
print('Terminating bot')
print('Terminating bot...')
return

client = SteamClient(api_key)
client.login(username, password, steamguard_path)
print('Bot logged in successfully, fetching offers every 60 seconds')

while True:
offers = client.get_trade_offers()['response']['trade_offers_received']
for offer in offers:
if is_donation(offer):
offer_id = offer['tradeofferid']
num_accepted_items = len(offer['items_to_receive'])
client.accept_trade_offer(offer_id)
print('Accepted trade offer {}. Got {} items'.format(offer_id, num_accepted_items))
print(f'Accepted trade offer {offer_id}. Got {num_accepted_items} items')
time.sleep(60)


def are_credentials_filled() -> bool:
return api_key != '' and steamguard_path != '' and username != '' and password != ''
return all((api_key, steamguard_path, username, password))


def is_donation(offer: dict) -> bool:
return offer.get('items_to_receive') \
and not offer.get('items_to_give') \
and offer['trade_offer_state'] == TradeOfferState.Active \
and not offer['is_our_offer']
return (
offer.get('items_to_receive')
and not offer.get('items_to_give')
and offer['trade_offer_state'] == TradeOfferState.Active
and not offer['is_our_offer']
)


if __name__ == "__main__":
# execute only if run as a script
if __name__ == '__main__':
main()
Loading

0 comments on commit d540b3d

Please sign in to comment.