-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
129 lines (108 loc) · 3.66 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
import manager as m
import afkstreamer
import time
import listener
from multiprocessing.managers import BaseManager
import bootstrap
from bootstrap import Switch
import config
import logging
import threading
import os
class StatusUpdate(object):
__metaclass__ = bootstrap.Singleton
def __init__(self):
object.__init__(self)
# Start streamstatus updater
m.start_updater()
self.mode = None
self.status = m.Status()
self.status.add_handler(self)
self.streamer = afkstreamer.Streamer(config.icecast_attributes())
self.listener = None
self.switching = False
def switch_dj(self, force=False):
if (force):
self.switching = Switch(True)
else:
np = m.NP()
self.switching = Switch(True, (np.length - np.position) + 15)
# Call shutdown
self.streamer.shutdown(force)
def __call__(self, info):
"""
If info does not contain anything, then the call to the master server
failed, and hence there is no mountpoint or DJ active.
info is a dictionary from streamstatus.get_status(server_name)
"""
if (not info['Online']):
self.debug(
"No mountpoint for {server} found.".format(
server=config.master_server))
if not self.streamer.connected:
# no streamer up, and no mountpoint
self.debug("Streamer is not connected")
# only allow hanyuu to stream if she is set as DJ first!
if m.DJ().user == u"AFK" and (not self.switching):
logging.debug("Streaming trying to connect")
self.streamer.connect()
elif m.DJ().user != u"AFK":
logging.debug('Not allowed to connect')
elif (not self.streamer.connected):
self.debug(
"{server} is active and we aren't streaming; assume DJ".format(
server=config.master_server))
# No streamer is active, there is a DJ streaming
if (not self.listener):
self.debug("Listener isn't active, starting it")
# There is no listener active, create one
self.listener = listener.start()
elif (not self.listener.active):
# The listener died restart it
self.debug("Listener isn't active anymore, restarting it")
self.listener.shutdown()
self.listener = listener.start()
def debug(self, mode):
if mode != self.mode:
self.mode = mode
logging.debug(mode)
class StreamManager(BaseManager):
pass
StreamManager.register("stats", bootstrap.stats)
StreamManager.register("Stream", StatusUpdate)
def connect():
global manager, stream
manager = StreamManager(
address=config.manager_stream,
authkey=config.authkey)
manager.connect()
stream = manager.Stream()
return stream
def start():
s = StatusUpdate()
manager = StreamManager(
address=config.manager_stream,
authkey=config.authkey)
server = manager.get_server()
server.serve_forever()
def launch_server():
import os
try:
os.remove('/tmp/hanyuu_stream')
except:
pass
manager = StreamManager(
address=config.manager_stream,
authkey=config.authkey)
manager.start()
global _unrelated_
_unrelated_ = manager.Stream()
return manager
def main():
global manager
t = threading.Thread(target=start)
t.daemon = True
t.name = "Streamer Manager Thread"
t.start()
if __name__ == "__main__":
main()