Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Getting Started

Nate the great edited this page Jan 8, 2018 · 1 revision

Getting Started

Setting up

First we must import 3 required modules to run this program:

import asyncio
from pytrade.client import TradeManager
from pytrade.login import AsyncClient

Then, we create the manager, and client objects:

manager = TradeManager('name', 'pass', shared_secret='secret')
client = AsyncClient('steam 64 id', key='apikey', identity_secret='your other secret')

Why AsyncClient separate from TradeManager?

Those are 2 separate classes, because later, I plan on adding more steam related libraries, and will make AsyncClient it's own module, and all the other libraries will accept that class an input.

Hooking up events

steam-trade has an array of events, we're going to be using 3 in our donation program.

@manager.on('logged_on')
async def logged_on():
    print("Logged in!")

@manager.on("new_trade")
async def new_trade(trade):
    print(f"New Trade! {trade.tradeofferid}")
    if trade.items_to_give == []:
        await trade.accept()
    else:
        await trade.decline()

@manager.on("trade_accepted")
async def trade_accepted(trade):
    print(f"Trade Accepted! {trade.tradeofferid}")

The function logged_on will run when the program has logged onto steam, and tested the connection. When this happens, it will print Logged in!. The function new_trade will run on a new trade, and check to see if we're giving anything. If we are, it will accept the trade, else wise it will decline the trade. The function trade_accepted will run when a trade status has changed to accepted.

What is this async def and await

If you don't know, please check out a tutorial on asyncio / asynchronous programming! async def is syntax added to python 3.6 (The minimum required version for this library) that creates a coroutine out of the function. await awaits the coroutine.

Add the loop and running

There are 3 lines of code now required to run this program. Keep in mind, any code after this will not run.

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.ensure_future(manager.login(steam_cli)))
manager.run_forever()

The first line gets the main loop, which manages all asynchronous stuff. The second line logs in, and blocks everything else from running. We need to stop it from continuing, because otherwise it will log in while scanning for trades, which would result in an error. The last line does what it says. It starts polling, can runs forever.

How can I stop it?

At the moment, there is no clean way implemented into steam-trade, to stop it nicely. Simply doing exit() will suffice.

And your done! This is the final code

import asyncio
from pytrade.client import TradeManager
from pytrade.login import AsyncClient

manager = TradeManager('name', 'pass', shared_secret='secret')
client = AsyncClient('steam 64 id', key='apikey', identity_secret='your other secret')

@manager.on('logged_on')
async def logged_on():
    print("Logged in!")

@manager.on("new_trade")
async def new_trade(trade):
    print(f"New Trade! {trade.tradeofferid}")
    if trade.items_to_give == []:
        await trade.accept()
    else:
        await trade.decline()

@manager.on("trade_accepted")
async def trade_accepted(trade):
    print(f"Trade Accepted! {trade.tradeofferid}")

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.ensure_future(manager.login(steam_cli)))
manager.run_forever()