diff --git a/recipes/1.Login/README.rst b/recipes/1.Login/README.rst new file mode 100644 index 0000000..6c5a0b8 --- /dev/null +++ b/recipes/1.Login/README.rst @@ -0,0 +1,13 @@ +one_off_login.py +---------------- + +Minimal example for login, support email and two factor codes. +After the login, the code will print some account info and exit. +No information is persisted to disk. + +persistent_login.py +------------------- + +In this example, after the login prompt, the client will remain connected until interrupted. +The client will attempt to reconnect after losing network connectivity or when steam is down. + diff --git a/recipes/1.Login/one_off_login.py b/recipes/1.Login/one_off_login.py new file mode 100644 index 0000000..c64ad5e --- /dev/null +++ b/recipes/1.Login/one_off_login.py @@ -0,0 +1,47 @@ +from __future__ import print_function +from getpass import getpass +import gevent +from steam import SteamClient + + +print("One-off login recipe") +print("-"*20) + +logon_details = { + 'username': raw_input("Steam user: "), + 'password': getpass("Password: "), +} + + +client = SteamClient() + +@client.on('error') +def error(result): + print("Logon result:", repr(result)) + +@client.on('auth_code_required') +def auth_code_prompt(is_2fa, mismatch): + if is_2fa: + code = raw_input("Enter 2FA Code: ") + client.login(two_factor_code=code, **logon_details) + else: + code = raw_input("Enter Email Code: ") + client.login(auth_code=code, **logon_details) + +print("-"*20) + +client.login(**logon_details) + +try: + client.wait_event('logged_on') +except: + raise SystemExit + +print("-"*20) +print("Logged on as:", client.user.name) +print("Community profile:", client.steam_id.community_url) +print("Last logon:", client.user.last_logon) +print("Last logoff:", client.user.last_logoff) + +gevent.idle() +client.logout() diff --git a/recipes/1.Login/persistent_login.py b/recipes/1.Login/persistent_login.py new file mode 100644 index 0000000..482fe17 --- /dev/null +++ b/recipes/1.Login/persistent_login.py @@ -0,0 +1,80 @@ +from __future__ import handle_function +import logging +import gevent +from getpass import getpass +from steam import SteamClient + +logging.basicConfig(format="%(asctime)s | %(message)s", level=logging.INFO) +LOG = logging.getLogger() + +LOG.info("Persistent logon recipe") +LOG.info("-"*30) + +logon_details = { + "username": raw_input("Steam user: "), + "password": getpass("Password: "), +} + + +client = SteamClient() +client.set_credential_location(".") + +@client.on("error") +def handle_error(result): + LOG.info("Logon result: %s", repr(result)) + +@client.on("channel_secured") +def send_login(): + if client.relogin_available: + client.relogin() + else: + client.login(**logon_details) + +@client.on("connected") +def handle_connected(): + LOG.info("Connected to %s", client.current_server_addr) + +@client.on("reconnect") +def handle_reconnect(delay): + LOG.info("Reconnect in %ds...", delay) + +@client.on("disconnected") +def handle_disconnect(): + LOG.info("Disconnected.") + + try: + client.wait_event("auth_code_required", timeout=2, raises=True) + except: + LOG.info("Reconnecting...") + client.reconnect(maxdelay=30) + +@client.on("auth_code_required") +def auth_code_prompt(is_2fa, mismatch): + if mismatch: + LOG.info("Previous code was incorrect") + + if is_2fa: + code = raw_input("Enter 2FA Code: ") + client.login(two_factor_code=code, **logon_details) + else: + code = raw_input("Enter Email Code: ") + client.login(auth_code=code, **logon_details) + +@client.on("logged_on") +def handle_after_logon(): + LOG.info("-"*30) + LOG.info("Logged on as: %s", client.user.name) + LOG.info("Community profile: %s", client.steam_id.community_url) + LOG.info("Last logon: %s", client.user.last_logon) + LOG.info("Last logoff: %s", client.user.last_logoff) + LOG.info("-"*30) + LOG.info("Press ^C to exit") + + +try: + client.connect() + client.run_forever() +except KeyboardInterrupt: + if client.connected: + LOG.info("Logout") + client.logout() diff --git a/recipes/README.rst b/recipes/README.rst new file mode 100644 index 0000000..66e051d --- /dev/null +++ b/recipes/README.rst @@ -0,0 +1,20 @@ +Recipes +------- + +Each directory contains recipes using the ``steam`` package. +See the individual ``README`` for details. + +All recipes assume the enviroment is already set up. +In order not to break any system packages we will use ``virtualenv``. + +.. code:: bash + + $ virtualenv env + $ source env/bin/activate + (env)$ pip install steam + +To deactivate + +.. code:: bash + + (env)$ deactivate