-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskinstats.py
106 lines (86 loc) · 2.9 KB
/
skinstats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from gevent import monkey
monkey.patch_all()
from gevent.pywsgi import WSGIServer
from csgo_worker import CSGOWorker, NoGcResponse, NoPaintwear
from flask import Flask, request, current_app
import re
import os
import sys
import logging
logging.basicConfig(
format="%(asctime)s | %(name)s | thread:%(thread)s | %(levelname)s | %(message)s",
level=logging.INFO,
)
LOG = logging.getLogger("CSGO GC API")
app = Flask("Flask Server")
@app.route("/")
def home():
return current_app.send_static_file("index.html")
@app.route("/api", methods=["GET"])
def item() -> str:
if "url" in request.args:
match = re.search("([SM])(\d+)A(\d+)D(\d+)$", request.args["url"])
if "S" == match.group(1):
s = int(match.group(2))
m = 0
else:
s = 0
m = int(match.group(2))
a = int(match.group(3))
d = int(match.group(4))
else:
s = int(request.args["s"])
a = int(request.args["a"])
d = int(request.args["d"])
m = int(request.args["m"])
try:
iteminfo = worker.get_item(s, a, d, m)
except NoGcResponse:
return "Steam failed to respond. Check you have the correct link for a skin, not a sticker. Email [email protected] if this persists."
except NoPaintwear:
return "Only skins are supported. If this seems like an error, email [email protected]."
except Exception as e:
LOG.error(f"Unhandled exception in worker: {e}")
return "Failed to retrieve info for an unknown reason."
return str(iteminfo)
@app.route("/ping", methods=["POST"])
def ping() -> str:
return "pong"
if __name__ == "__main__":
LOG.info("csgoSkinStatistics")
LOG.info("-" * 18)
LOG.info("Starting worker...")
worker = CSGOWorker()
try:
if os.environ.get("steam_user") and os.environ.get("steam_pass"):
try:
worker.start(
username=os.environ["steam_user"], password=os.environ["steam_pass"]
)
except:
LOG.error("Failed to sign in with environment variables")
raise
elif len(sys.argv) == 3:
try:
worker.start(username=sys.argv[1], password=sys.argv[2])
except:
LOG.error("Failed to sign in with args")
raise
else:
try:
worker.cli_login()
except:
LOG.error("Failed to with in through the CLI")
raise
except:
LOG.info("Exiting...")
worker.close()
sys.exit()
LOG.info("Starting HTTP server...")
wsgi_server_log = logging.getLogger("HTTP Server")
http_server = WSGIServer(("", 5000), app, log=wsgi_server_log, error_log=wsgi_server_log)
try:
http_server.serve_forever()
except KeyboardInterrupt:
LOG.info("Exit requested")
worker.close()