-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_writer.py
54 lines (40 loc) · 1.49 KB
/
stream_writer.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
import requests
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
from datetime import datetime
class StreamWriter(object):
server_url = ''
file_size = 0.0
bitrate = 64.0
stream_length = 0.0
metadata = {}
def __init__(self, server_url, seconds, destination='./', filename='output.mp3'):
self.server_url = server_url
self.seconds = seconds
self.destination = destination
self.filename = filename
def record(self):
"""
This method records the file stream until the stream_length has been reached
"""
while True:
with open(self.destination+self.filename, 'w+b') as handle:
request = requests.get(self.server_url, stream=True)
self.metadata = request.headers
self.bitrate = float(self.metadata['icy-br'].split(',')[0])
for block in request.iter_content(1024):
self.file_size += 1024.0
if ((self.file_size/1024.0) * 8.0) / self.bitrate > self.seconds:
break_it_boy = True
break
if not block:
break
handle.write(block)
if break_it_boy:
break
if break_it_boy:
break
self.calc_length()
return True
def calc_length(self):
self.stream_length = ( ( self.file_size/1024.0 ) * 8.0 )/self.bitrate