forked from infoforcefeed/InsomniaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pipe.py
68 lines (49 loc) · 1.74 KB
/
test_pipe.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
import re
from socketproxy import Pipe, PlumbingServer, main
import os
import time
class TestProxy(PlumbingServer):
def __init__(self, *args, **kwargs):
PlumbingServer.__init__(self, *args, **kwargs)
self.pipes.append(RateLimitPipe())
self.pipes.append(AudioPipe('horn', open('airhorn.mp3')))
self.pipes.append(AudioPipe('trumpet', open('trumpet.mp3')))
class AudioPipe(Pipe):
def __init__(self, name, mp3):
self.mp3 = mp3.read()
self.name = name
self.buff = ''
def to_client(self, data):
# Add mp3 to buffer
if (os.path.isfile(self.name)):
os.remove(self.name)
self.buff += self.mp3
if self.buff:
if len(self.buff) >= len(data):
data = self.buff[:len(data)]
self.buff = self.buff[len(data):]
else:
data = self.buff + data[:-len(self.buff)]
self.buff = ''
return data
class RateLimitPipe(Pipe):
def __init__(self):
self.bitrate = 0
self.time_of_next_send = 0
def to_client(self, data):
sleep_time = self.time_of_next_send - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
if data.find('icy-br') != -1:
regex = re.compile('icy-br:(?P<bitrate>[0-9]*)')
match = regex.search(data)
if match:
bitrate = int(match.groupdict()['bitrate'])
self.bitrate = bitrate
if self.bitrate:
bits = len(data) * 8
secs_til_next_send = float(bits) / (self.bitrate * 1024)
self.time_of_next_send = time.time() + secs_til_next_send
return data
if __name__ == '__main__':
main(TestProxy)