gameservers.py
· 3.2 KiB · Python
Raw
#!/usr/bin/env python
from __future__ import print_function
import sys
try:
import requests
from bs4 import BeautifulSoup as Soup
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import yaml
except ImportError:
print("Please run \"pip install requests beautifulsoup4 pycrypto pyyaml\"", file=sys.stderr)
sys.exit(1)
import time
import json
import base64
import os
H = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}
session = requests.Session()
session.headers.update(H)
def get_key(username):
return session.post("https://steamcommunity.com/login/getrsakey/",
data=dict(username=username, donotcache=time.time()*1000) ).json()
def encode_passwd(key_data, passwd):
mod = int(key_data["publickey_mod"], 16)
exp = int(key_data["publickey_exp"], 16)
rsa = RSA.construct((mod, exp))
cipher = PKCS1_v1_5.new(rsa)
if hasattr(passwd, "encode"):
passwd = passwd.encode("utf-8")
return base64.b64encode(cipher.encrypt(passwd))
def login(username, password):
key = get_key(username)
encrypted_password = encode_passwd(key, password)
return session.post("https://steamcommunity.com/login/dologin/",
data=dict(
username=username,
password=encrypted_password,
emailauth="",
loginfriendlyname="",
captchagid="-1",
captcha_text="",
emailsteamid="",
rsatimestamp=key["timestamp"],
remember_login=True,
donotcache=time.time()*1000
) ).json()
def transfer(url, login_data):
session.post(url,
data=login_data["transfer_parameters"]
)
def get_gameservers():
"""Yields lists containing four items: steam game id, token, date last used and memo"""
page = Soup(session.get("https://steamcommunity.com/dev/managegameservers").text, "html.parser")
response = []
for row in page.findAll("tr")[1:]:
response.append([ i.text.replace("\\", "\\\\").replace('"', '\\"') for i in row.findAll("td")[:4] ])
return response
if __name__ == "__main__":
username = os.environ.get("STEAM_USERNAME")
password = os.environ.get("STEAM_PASSWORD")
if username and password:
if os.path.isfile("/tmp/steam-{}.json".format(username)):
with open("/tmp/steam-{}.json".format(username)) as f:
session.cookies = requests.utils.cookiejar_from_dict(json.loads(f.read()))
else:
login_data = login(username, password)
for url in login_data["transfer_urls"]:
transfer(url, login_data)
for gs in get_gameservers():
if "(expired)" in gs[1]:
gs[1] = gs[1][:32]
gs.append(True)
else:
gs.append(False)
print(yaml.safe_dump( {gs[3]: dict(id=gs[0], token=gs[1], date=gs[2], expired=gs[4])}, default_flow_style=False ).strip())
with open("/tmp/steam-{}.json".format(username), "w") as f:
f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
else:
print("Please specify environment variables STEAM_USERNAME and STEAM_PASSWORD", file=sys.stderr)
| 1 | #!/usr/bin/env python |
| 2 | from __future__ import print_function |
| 3 | import sys |
| 4 | |
| 5 | try: |
| 6 | import requests |
| 7 | from bs4 import BeautifulSoup as Soup |
| 8 | from Crypto.PublicKey import RSA |
| 9 | from Crypto.Cipher import PKCS1_v1_5 |
| 10 | import yaml |
| 11 | except ImportError: |
| 12 | print("Please run \"pip install requests beautifulsoup4 pycrypto pyyaml\"", file=sys.stderr) |
| 13 | sys.exit(1) |
| 14 | |
| 15 | import time |
| 16 | import json |
| 17 | import base64 |
| 18 | import os |
| 19 | |
| 20 | H = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"} |
| 21 | session = requests.Session() |
| 22 | session.headers.update(H) |
| 23 | |
| 24 | def get_key(username): |
| 25 | return session.post("https://steamcommunity.com/login/getrsakey/", |
| 26 | data=dict(username=username, donotcache=time.time()*1000) ).json() |
| 27 | |
| 28 | def encode_passwd(key_data, passwd): |
| 29 | mod = int(key_data["publickey_mod"], 16) |
| 30 | exp = int(key_data["publickey_exp"], 16) |
| 31 | rsa = RSA.construct((mod, exp)) |
| 32 | cipher = PKCS1_v1_5.new(rsa) |
| 33 | if hasattr(passwd, "encode"): |
| 34 | passwd = passwd.encode("utf-8") |
| 35 | return base64.b64encode(cipher.encrypt(passwd)) |
| 36 | |
| 37 | def login(username, password): |
| 38 | key = get_key(username) |
| 39 | encrypted_password = encode_passwd(key, password) |
| 40 | return session.post("https://steamcommunity.com/login/dologin/", |
| 41 | data=dict( |
| 42 | username=username, |
| 43 | password=encrypted_password, |
| 44 | emailauth="", |
| 45 | loginfriendlyname="", |
| 46 | captchagid="-1", |
| 47 | captcha_text="", |
| 48 | emailsteamid="", |
| 49 | rsatimestamp=key["timestamp"], |
| 50 | remember_login=True, |
| 51 | donotcache=time.time()*1000 |
| 52 | ) ).json() |
| 53 | |
| 54 | def transfer(url, login_data): |
| 55 | session.post(url, |
| 56 | data=login_data["transfer_parameters"] |
| 57 | ) |
| 58 | |
| 59 | def get_gameservers(): |
| 60 | """Yields lists containing four items: steam game id, token, date last used and memo""" |
| 61 | page = Soup(session.get("https://steamcommunity.com/dev/managegameservers").text, "html.parser") |
| 62 | response = [] |
| 63 | for row in page.findAll("tr")[1:]: |
| 64 | response.append([ i.text.replace("\\", "\\\\").replace('"', '\\"') for i in row.findAll("td")[:4] ]) |
| 65 | return response |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | username = os.environ.get("STEAM_USERNAME") |
| 69 | password = os.environ.get("STEAM_PASSWORD") |
| 70 | if username and password: |
| 71 | if os.path.isfile("/tmp/steam-{}.json".format(username)): |
| 72 | with open("/tmp/steam-{}.json".format(username)) as f: |
| 73 | session.cookies = requests.utils.cookiejar_from_dict(json.loads(f.read())) |
| 74 | else: |
| 75 | login_data = login(username, password) |
| 76 | for url in login_data["transfer_urls"]: |
| 77 | transfer(url, login_data) |
| 78 | for gs in get_gameservers(): |
| 79 | if "(expired)" in gs[1]: |
| 80 | gs[1] = gs[1][:32] |
| 81 | gs.append(True) |
| 82 | else: |
| 83 | gs.append(False) |
| 84 | print(yaml.safe_dump( {gs[3]: dict(id=gs[0], token=gs[1], date=gs[2], expired=gs[4])}, default_flow_style=False ).strip()) |
| 85 | with open("/tmp/steam-{}.json".format(username), "w") as f: |
| 86 | f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) |
| 87 | else: |
| 88 | print("Please specify environment variables STEAM_USERNAME and STEAM_PASSWORD", file=sys.stderr) |
| 1 | requests |
| 2 | beautifulsoup4 |
| 3 | pycrypto |
| 4 | pyyaml |