-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketServer.py
232 lines (184 loc) · 5.61 KB
/
socketServer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import youtube_dl as ytdl
from urllib.parse import unquote
import eventlet
import socketio
import configparser
import time
import threading
import os
lastState = 0 # 0 - not init (0 sec and pause), 1 - pause, 2 - play
lastPlayed = 0
lastPaused = 0
pausedIndex = 0
sio = socketio.Server(cors_allowed_origins='*')
app = socketio.WSGIApp(sio, static_files={
'/': {'content_type': 'text/html', 'filename': 'index.html'}
})
@sio.event
def connect(sid, environ):
print('connect ', sid)
@sio.event
def video(sid):
if allowWatch == True:
if videoURL != "":
sio.emit("video", videoURL, room = sid)
sio.emit("audio", audioURL, room = sid)
if youtubeURL != "":
sio.emit("youtube", formats, room = sid)
sio.emit("youtubeFormat", defaultFormat, room = sid)
else:
sio.emit("message", "Please wait till the administrator starts the movie.", room=sid)
@sio.event
def state(sid):
global lastState
sio.emit("state", lastState, room=sid)
@sio.event
def progress(sid):
global lastPlayed, lastPaused, lastState
if lastPaused == 0:
progressNow = int(time.time() - lastPlayed)
if lastPaused != 0:
progressNow = int(lastPaused - lastPlayed)
if lastState == 0:
progressNow = 0
sio.emit("time", progressNow, room=sid)
@sio.event
def setState(sid, state):
global lastState, lastPlayed, lastPaused, pausedIndex
if lastState == 0 and state != 0:
lastPlayed = time.time()
lastState = state
if lastState == 2:
pausedIndex = 0
if lastPaused != 0:
pausedIndex = pausedIndex + (time.time() - lastPaused)
lastPlayed = lastPlayed + pausedIndex
lastPaused = 0
if lastState == 1:
lastPaused = time.time()
sio.emit("state", lastState, broadcast=True, include_self=False)
@sio.event
def buffering(sid, start):
global lastState, lastPlayed, lastPaused, pausedIndex
if int(start) == 1:
lastState = 1
sio.emit("message", "Buffering", broadcast=True, include_self=False)
if int(start) == 0:
lastState = 2
sio.emit("message", "Playing", broadcast=True, include_self=False)
if lastState == 2:
pausedIndex = 0
if lastPaused != 0:
pausedIndex = pausedIndex + (time.time() - lastPaused)
lastPlayed = lastPlayed + pausedIndex
lastPaused = 0
if lastState == 1:
lastPaused = time.time()
sio.emit("state", lastState, broadcast=True, include_self=False)
@sio.event
def setTime(sid, timeS):
global lastPlayed, lastPaused
if int(timeS) == 0:
lastPlayed = time.time()
sio.emit("time", 0, broadcast=True, include_self=False)
else:
if lastPaused == 0:
progressNow = int(time.time() - lastPlayed)
if lastPaused != 0:
progressNow = int(lastPaused - lastPlayed)
progressV = timeS - progressNow
lastPlayed = lastPlayed - progressV
sio.emit("time", timeS, broadcast=True, include_self=False)
@sio.event
def disconnect(sid):
print('disconnect ', sid)
def watchdog():
global initStamp, sio, videoURL, youtubeURL, allowWatch, lastState, lastPlayed, lastPaused, pausedIndex
while True:
if os.stat("settings.ini").st_mtime != initStamp:
oldVideo = videoURL
oldAllowed = allowWatch
oldYoutube = youtubeURL
readConfig()
if oldVideo != videoURL or oldAllowed != allowWatch or oldYoutube != youtubeURL:
lastPlayed = 0
lastState = 0
lastPaused = 0
pausedIndex = 0
sio.emit("newInfo", broadcast = True)
initStamp = os.stat("settings.ini").st_mtime
time.sleep(.5)
def parseYoutube(url):
global audioURL, formats, defaultFormat
defaultFormat = "1080p (FHD)"
formats = {
"720p (HD)": None,
"1080p (FHD)": None,
"1440p (2K)": None,
"2160p (4K)": None
}
yt = ytdl.YoutubeDL()
try:
_formats = yt.extract_info(url, download = False)['formats']
for i in _formats:
if i['acodec'] == "none":
if i['ext'] == "webm":
for x in formats.keys():
if i['format_note'].replace("60", "") in x:
formats[x] = unquote(i['url'])
else:
if i['vcodec'] == "none" and i['ext'] == "m4a":
audioURL = unquote(i['url'])
if formats[defaultFormat] == None:
print("drop default")
defaultFormat = "720p (HD)"
for i in formats.copy().keys():
if formats[i] == None:
del formats[i]
except Exception as e:
raise ValueError("invalid url")
def readConfig():
global audioURL, videoURL, youtubeURL, allowWatch, useSSL, certfile, privkey, port
config = configparser.ConfigParser()
config.read("settings.ini")
videoURL = config.get("general", "videoURL")
audioURL = config.get('general', 'audioURL')
youtubeURL = config.get('general', 'youtubeURL')
if youtubeURL != "":
videoURL = ""
audioURL = ""
parseYoutube(youtubeURL)
allowWatch = False
if config.get("general", "allowWatch") == "yes":
allowWatch = True
useSSL = True
if config.get("ssl", "enable") == "no":
useSSL = False
certfile = config.get("ssl", "certfile")
privkey = config.get("ssl", "privkey")
port = int(config.get('socket', 'port'))
if __name__ == '__main__':
defaultFormat = "1080p (FHD)"
formats = {
"720p (HD)": None,
"1080p (FHD)": None,
"1440p (2K)": None,
"2160p (4K)": None
}
initStamp = 0
videoURL = None
audioURL = None
youtubeURL = None
allowWatch = False
useSSL = False
certfile = None
privkey = None
port = 0
initStamp = os.stat("settings.ini").st_mtime
readConfig()
eventlet.monkey_patch()
eventlet.spawn(watchdog)
if useSSL == True:
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', port)), certfile=certfile, keyfile=privkey, server_side=True), app)
else:
eventlet.wsgi.server(eventlet.listen(('', port)), app)