forked from watthub/HQSolver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
86 lines (63 loc) · 2.59 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
import json
import requests
import sys
from config import login_header, readFromConfig
from shutil import get_terminal_size
from autobahn.twisted.websocket import connectWS
from twisted.internet import reactor, ssl
from game import GameFactory
class Launcher(object):
def __init__(self):
self.api_shows = readFromConfig("API", "shows_now")
self.debug = readFromConfig("General", "debug_mode")
self.server_ip = readFromConfig("General", "server_ip")
def launchSolver(self):
print(" HQ Solver ".center(get_terminal_size()[0], "="))
if self.showAlive():
socketURL = self.getSocketURL()
proto = socketURL.split('://')[0]
server = socketURL.split('://')[1].split("/")[0]
server_dir = socketURL.replace(proto + '://' + server, "")
if proto == "http":
self.socketURL = "ws://" + server + ":80" + server_dir
elif proto == "https":
self.socketURL = "wss://" + server + ":443" + server_dir
else:
self.socketURL = socketURL
factory = GameFactory(self.socketURL, headers=login_header)
factory.setProtocolOptions(autoPingInterval=5, autoPingTimeout=5, autoPingSize=20)
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
connectWS(factory, contextFactory)
reactor.run()
else:
print(''.center(get_terminal_size()[0], '='))
sys.exit(0)
def showAlive(self):
""" Checks if show is live """
request = requests.get(self.server_ip + self.api_shows, headers=login_header)
response = request.content.decode()
try:
responseJSON = json.loads(response)
if responseJSON["active"]:
print("Show is now live!".center(get_terminal_size()[0]))
return True
else:
print("Show aren't live!".center(get_terminal_size()[0]))
return False
except:
print("Server returned unknown response!".center(get_terminal_size()[0]))
return False
def getSocketURL(self):
request = requests.get(self.server_ip + self.api_shows, headers=login_header)
response = request.content.decode()
try:
broadcastJSON = json.loads(response)["broadcast"]
socketURL = broadcastJSON["socketUrl"]
return socketURL
except:
return False
if __name__ == "__main__":
Launcher().launchSolver()