-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstart.py
131 lines (111 loc) · 4.08 KB
/
start.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
130
131
#! /usr/bin/env python3.7
from itertools import chain
import logging
import os
import threading
import time
from modules import saltybot
from modules import balancer
from modules import configuration
from modules import setup_env
from modules.module_errors import NewBotException
from modules.apis import kraken
from modules.apis import newbs
from modules.apis import osu
from modules.apis import splits_io
from modules.apis import sr_com
from modules.apis import srl
from modules.apis import youtube
RUNNING = True
# All apis use the environment keys for the required access
GLOBAL_APIS = {}
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
def twitch_update_thread(balancer_obj):
sleep_timer = 2
while RUNNING:
with balancer_obj.lock:
ids = list(balancer_obj.bot_lookup.values())
channels = list(balancer_obj.bot_lookup.keys())
success, response = GLOBAL_APIS["kraken"].get_streams(ids)
if not success:
time.sleep(sleep_timer)
sleep_timer = sleep_timer ** 2
continue
new_info = {x: {"game": "", "title": "", "stream_start": "", "is_live": False} for x in channels}
try:
for i in response["data"]:
new_info[i["user_login"]] = {
"game": i["game_name"],
"title": i["title"],
"is_live": True,
"stream_start": i["started_at"]
}
balancer_obj.update_twitch(new_info)
except TypeError:
pass
sleep_timer = 2
time.sleep(60)
return
def listen_thread(config_obj, balancer_obj, server_obj):
while RUNNING:
retrieve_value = server_obj.listen_for_updates()
new_config = config_obj.fetch_one(retrieve_value)
if retrieve_value:
new_config = {"": new_config}
for i in list(new_config.values()):
try:
balancer_obj.update_bot(i)
except NewBotException:
balancer_obj.add_bot(saltybot.SaltyBot(i))
return
def main():
logging.basicConfig(
filename="debug.log",
filemode="w",
level=logging.INFO,
format="[%(levelname)s %(asctime)s] %(message)s",
datefmt="%m-%d %H:%M:%S"
)
logging.getLogger("requests").setLevel(logging.WARNING)
salty_environment = setup_env.set_environment_variables()
GLOBAL_APIS["kraken"] = kraken.Kraken()
GLOBAL_APIS["newbs"] = newbs.NewbsAPI()
GLOBAL_APIS["osu"] = osu.OsuAPI()
GLOBAL_APIS["splits_io"] = splits_io.SplitsIOAPI()
GLOBAL_APIS["sr_com"] = sr_com.SRcomAPI()
GLOBAL_APIS["srl"] = srl.SRLAPI()
GLOBAL_APIS["youtube"] = youtube.YoutubeAPI()
config_type = os.environ["DB_TYPE"]
db_url = os.environ["DB_URL"]
if config_type == "json":
config_obj = configuration.JSONConfig(salty_environment, db_url)
server_obj = configuration.ConfigServer("JSON", **{"filename": os.environ["DB_URL"]})
else:
config_obj = configuration.DBConfig(salty_environment, db_url)
server_obj = configuration.ConfigServer("db", **{
"web_ip": os.environ["WEB_LISTEN_IP"],
"web_port": os.environ["WEB_LISTEN_PORT"],
"web_secret": os.environ["WEB_SECRET_KEY"]
})
balancer_obj = balancer.Balancer()
initial_configs = config_obj.initial_retrieve()
for k, v in initial_configs.items():
bot_inst = saltybot.SaltyBot(v)
balancer_obj.add_bot(bot_inst)
tu_thread = threading.Thread(name="update-thread", target=twitch_update_thread, args=(balancer_obj,))
tu_thread.daemon = True
tu_thread.start()
l_thread = threading.Thread(name="listen-thread", target=listen_thread, args=(config_obj, balancer_obj, server_obj))
l_thread.daemon = True
l_thread.start()
try:
global RUNNING
while RUNNING:
eval(input("> "))
except KeyboardInterrupt:
print("Time to shut down!")
RUNNING = False
balancer_obj.shutdown()
if __name__ == "__main__":
main()