Last active 1486541279

A script to log into Steam and list game servers

Steven Smith revised this gist 1485268177. Go to revision

2 files changed, 7 insertions, 9 deletions

gameservers.py

@@ -7,8 +7,9 @@ try:
7 7 from bs4 import BeautifulSoup as Soup
8 8 from Crypto.PublicKey import RSA
9 9 from Crypto.Cipher import PKCS1_v1_5
10 + import yaml
10 11 except ImportError:
11 - print("Please run \"pip install requests beautifulsoup4 pycrypto\"", file=sys.stderr)
12 + print("Please run \"pip install requests beautifulsoup4 pycrypto pyyaml\"", file=sys.stderr)
12 13 sys.exit(1)
13 14
14 15 import time
@@ -19,10 +20,6 @@ import os
19 20 H = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}
20 21 session = requests.Session()
21 22 session.headers.update(H)
22 - yaml = """{3}:
23 - id: "{0}"
24 - token: "{1}"
25 - date: "{2}" """.strip()
26 23
27 24 def get_key(username):
28 25 return session.post("https://steamcommunity.com/login/getrsakey/",
@@ -81,10 +78,10 @@ if __name__ == "__main__":
81 78 for gs in get_gameservers():
82 79 if "(expired)" in gs[1]:
83 80 gs[1] = gs[1][:32]
84 - gs.append("true")
81 + gs.append(True)
85 82 else:
86 - gs.append("false")
87 - print(yaml.format(*gs))
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())
88 85 with open("/tmp/steam-{}.json".format(username), "w") as f:
89 86 f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
90 87 else:

requirements.txt

@@ -1,3 +1,4 @@
1 1 requests
2 2 beautifulsoup4
3 - pycrypto
3 + pycrypto
4 + pyyaml

Steven Smith revised this gist 1485267291. Go to revision

1 file changed, 3 insertions, 2 deletions

gameservers.py

@@ -81,9 +81,10 @@ if __name__ == "__main__":
81 81 for gs in get_gameservers():
82 82 if "(expired)" in gs[1]:
83 83 gs[1] = gs[1][:32]
84 - print(yaml.format(*gs) + "\n expired: true")
84 + gs.append("true")
85 85 else:
86 - print(yaml.format(*gs))
86 + gs.append("false")
87 + print(yaml.format(*gs))
87 88 with open("/tmp/steam-{}.json".format(username), "w") as f:
88 89 f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
89 90 else:

Steven Smith revised this gist 1485266907. Go to revision

1 file changed, 0 insertions, 0 deletions

steamlogin.py renamed to gameservers.py

File renamed without changes

Steven Smith revised this gist 1485266879. Go to revision

2 files changed, 6 insertions, 2 deletions

requirements.txt(file created)

@@ -0,0 +1,3 @@
1 + requests
2 + beautifulsoup4
3 + pycrypto

steamlogin.py

@@ -1,4 +1,5 @@
1 - #!/usr/bin/env python3
1 + #!/usr/bin/env python
2 + from __future__ import print_function
2 3 import sys
3 4
4 5 try:
@@ -7,7 +8,7 @@ try:
7 8 from Crypto.PublicKey import RSA
8 9 from Crypto.Cipher import PKCS1_v1_5
9 10 except ImportError:
10 - print("Please run \"pip3 install requests beautifulsoup4 pycrypto\"", file=sys.stderr)
11 + print("Please run \"pip install requests beautifulsoup4 pycrypto\"", file=sys.stderr)
11 12 sys.exit(1)
12 13
13 14 import time

Steven Smith revised this gist 1485266567. Go to revision

1 file changed, 1 insertion, 1 deletion

steamlogin.py

@@ -49,7 +49,7 @@ def login(username, password):
49 49 captcha_text="",
50 50 emailsteamid="",
51 51 rsatimestamp=key["timestamp"],
52 - remember_login=False,
52 + remember_login=True,
53 53 donotcache=time.time()*1000
54 54 ) ).json()
55 55

Steven Smith revised this gist 1485266431. Go to revision

1 file changed, 89 insertions

steamlogin.py(file created)

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