-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
129 lines (96 loc) · 3.5 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from express.express import Express
from express.options import Options, GlobalOptions
from express.utils import (
ExpressFormatter,
ExpressFileFormatter,
get_version,
get_config,
)
from threading import Thread
import logging
import sys
import os
from tf2_utils import PricesTFSocket
from tf2_utils import __version__ as tf2_utils_version
from tf2_data import __version__ as tf2_data_version
from express import __version__ as tf2_express_version
from tf2_sku import __version__ as tf2_sku_version
bots: list[Express] = []
packages = [
(tf2_express_version, "tf2-express", "express"),
(tf2_utils_version, "tf2-utils", "src/tf2_utils"),
(tf2_data_version, "tf2-data", "src/tf2_data"),
(tf2_sku_version, "tf2-sku", "src/tf2_sku"),
]
formatter = ExpressFormatter()
stream_handler = logging.StreamHandler(sys.stdout)
LOG_PATH = os.getcwd() + "/logs/"
LOG_FILE = LOG_PATH + "express.log"
# create folder and empty log file
if not os.path.isfile(LOG_FILE):
os.makedirs(LOG_PATH)
open(LOG_FILE, "x")
file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
logging.basicConfig(
level=logging.DEBUG,
handlers=[
stream_handler,
file_handler,
],
)
# only want to see info and above in console
stream_handler.setLevel(logging.INFO)
# stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(ExpressFormatter())
# want to have everything in the log file
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(ExpressFileFormatter())
def create_bot_instance(bot: dict) -> Express:
options = Options(**bot["options"])
logging.debug(f"using options {options=}")
client = Express(bot, options)
logging.info(f"Created instance for {bot['username']}")
return client
# callback to tf2-utils' pricestf
def on_price_change(data: dict) -> None:
if data.get("type") != "PRICE_CHANGED":
return
if not data.get("data", {}).get("sku"):
return
# already easy to find in the logs
# no need to log again, takes time
# logging.debug(f"Got new price change from Prices.tf {data=}")
for bot in bots:
bot.append_new_price(data.get("data", {}))
if __name__ == "__main__":
logging.info("Started tf2-express")
config = get_config()
options = GlobalOptions(**config)
if options.check_versions_on_startup:
for i in packages:
current_version, repo, folder = i
latest_version = get_version(repo, folder)
if current_version == latest_version:
continue
logging.warning(
"{} is at version {} while {} is available".format(
repo, current_version, latest_version
)
)
if options.listen_to_pricer:
prices_tf_socket = PricesTFSocket(on_price_change)
prices_thread = Thread(target=prices_tf_socket.listen, daemon=True)
prices_thread.start()
logging.info("Listening to Prices.tf for price updates")
bot_threads: list[Thread] = []
for bot in options.bots:
bot_instance = create_bot_instance(bot)
bot_instance.login()
bot_thread = Thread(target=bot_instance.run, daemon=True)
bot_thread.start()
bots.append(bot_instance)
bot_threads.append(bot_thread)
for thread in bot_threads:
thread.join()
if options.listen_to_pricer:
prices_thread.join()