forked from Fisab/bongacams-record-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (79 loc) · 2.72 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
import requests, json, time, os, datetime
from threading import Thread
from sys import stdout
from livestreamer import Livestreamer
global resolution # In what resolution should stream be recorded (will choose closest under this parameter if specified is not present)
resolution = 1080 # 1080p
model = input(f'INPUT | Enter model name or link: ')
if 'bongacams' in model:
model = os.path.basename(os.path.normpath(model))
def sizeConvert(size):
in_KB = size/1024
in_MB = size/1024/1024
in_GB = size/1024/1024/1024
if in_GB > 0.8:
return f'{round(in_GB, 2)} GB'
elif in_MB > 0.8:
return f'{round(in_MB, 2)} MB'
else:
return f'{round(in_KB, 2)} KB'
global sizePrintStop
sizePrintStop = False
def print_file_details(path):
startTime = time.perf_counter()
while not sizePrintStop:
time.sleep(1)
timeNow = time.perf_counter()
stdout.write('\r'+f'INFO | Recording... {datetime.timedelta(seconds=(round(timeNow - startTime, 0)))} {sizeConvert(os.path.getsize(path))}'.ljust(100))
stdout.flush()
def get_data(model):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
data = [('method', 'getRoomData'), ('args[]', model)]
r = requests.post('https://bongacams.com/tools/amf.php', headers=headers, data=data)
return json.loads(r.text)
def stream(videoServerUrl, model):
session = Livestreamer()
session.set_option('http-headers', 'referer=https://bongacams.com/%s' % model)
url = 'hlsvariant://https:%s/hls/stream_%s/playlist.m3u8' % (videoServerUrl, model)
streams = session.streams(url)
resolutions = []
for x in streams:
try:
x = int(x[:-1])
if x <= resolution:
resolutions.append(x)
except Exception as e:
pass
# print(f'{resolutions[-1:][0]}p')
if not resolutions:
stream = streams[f'worst']
else:
stream = streams[f'{resolutions[-1:][0]}p']
fd = stream.open()
now = datetime.datetime.now()
filePath = '%s/%s.mp4' % (model, model+now.strftime('%Y-%m-%d-%H-%M'))
print(f'INFO | Stream recording started {filePath}')
if not os.path.exists(model):
os.makedirs(model)
sizePrint = Thread(target=print_file_details, args=(filePath,))
sizePrint.start()
with open(filePath, 'wb') as f:
while True:
try:
data = fd.read(1024)
f.write(data)
except:
sizePrintStop = True
print(f'ERROR | Unable to write to a file {filePath}')
f.close()
return
if __name__ == '__main__':
data = get_data(model)
if 'videoServerUrl' in data['localData']:
stream(data['localData']['videoServerUrl'], model)
else:
sizePrintStop = True
print('ERROR | This model is not online')