-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.py
79 lines (56 loc) · 2.27 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
import requests
import webbrowser
from lomond import WebSocket
import re
import json
import os
def show_active():
main_url = 'https://api-quiz.hype.space/shows/now'
response_data = requests.get(main_url).json()
return response_data['active']
def get_socket_url():
main_url = 'https://api-quiz.hype.space/shows/now'
response_data = requests.get(main_url).json()
socket_url = response_data['broadcast']['socketUrl'].replace('https', 'wss')
return socket_url
def connect_websocket(socket_url, auth_token):
headers = {"Authorization": f"Bearer {auth_token}",
"x-hq-client": "Android/1.3.0"}
websocket = WebSocket(socket_url)
for header, value in headers.items():
websocket.add_header(str.encode(header), str.encode(value))
for msg in websocket.connect(ping_rate=5):
if msg.name == "text":
message = msg.text
message = re.sub(r"[\x00-\x1f\x7f-\x9f]", "", message)
message_data = json.loads(message)
if message_data['type'] == 'question':
question = message_data['question']
cnt = message_data['questionCount']
print('{}. {}'.format(cnt, question))
open_browser(question)
def open_browser(question):
main_url = "https://www.google.co.in/search?q=" + question
webbrowser.open_new(main_url)
def get_auth_token():
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "token.txt"), "r") as conn_settings:
settings = conn_settings.read().splitlines()
settings = [line for line in settings if line != "" and line != " "]
try:
auth_token = settings[0].split("=")[1]
except IndexError:
print('No Key is given!')
return 'NONE'
return auth_token
if __name__ == '__main__':
input('Press enter to start ')
if show_active():
url = get_socket_url()
print('Connecting to Socket : {}'.format(url))
token = get_auth_token()
if token == 'NONE':
print('Please enter a valid auth token.')
else:
connect_websocket(url, token)
else:
print('Show is not active. Try again later!')