-
Notifications
You must be signed in to change notification settings - Fork 78
/
CamsodaRecorder.py
87 lines (82 loc) · 3.95 KB
/
CamsodaRecorder.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
import sys, datetime, time, os, threading
import requests, json, random
from livestreamer import Livestreamer
#specify path to save to ie "/Users/Joe/camsoda"
save_directory = "/Users/Joe/camsoda"
#specify the path to the wishlist file ie "/Users/Joe/camsoda/wanted.txt"
wishlist = "/Users/Joe/camsoda/wanted.txt"
headers={
'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
'referer':'https://www.camsoda.com/'
}
r = requests.session()
r.get('http://www.camsoda.com')
def getOnlineModels():
num = random.randint(1000, 30000)
wanted = []
with open(wishlist) as f:
for line in f:
models = line.split()
for theModel in models:
wanted.append(theModel.lower())
f.close()
url = "https://www.camsoda.com/api/v1/browse/online"
with r.get(url, headers=headers) as url:
data = url.json()
for line in data['results']:
if line['username'].lower() not in recording and line['status'] != 'connected':
if line['display_name'].lower() in wanted or line['username'].lower() in wanted:
#if line['status'] == 'private':
#print("starting PRIVATE recording of ", line['display_name'])
#else:
#print("starting ", line['display_name'])
thread = threading.Thread(target=startRecording, args=(line, num))
thread.start()
def startRecording(modelData, num):
try:
recording.append(modelData['username'].lower())
videoapi = requests.get(
"https://www.camsoda.com/api/v1/video/vtoken/{model}?username=guest_{num}".format(model=modelData['username'], num=num), headers=headers)
data2 = json.loads(videoapi.text)
if str(modelData['status']) != 'limited':
if str(modelData['status']) == 'private':
server = data2['private_servers'][0]
elif modelData['status'] == 'online':
server = data2['edge_servers'][0]
else:
server = data2['mjpeg_server']
link = "hlsvariant://https://{server}/{app}/mp4:{stream_name}_mjpeg/playlist.m3u8?token={token}".format(server=server, app=data2['app'], stream_name=data2['stream_name'], token=data2['token'])
if not os.path.exists("{path}/{model}".format(path=save_directory, model=modelData['username'])):
os.makedirs("{path}/{model}".format(path=save_directory, model=modelData['username']))
session = Livestreamer()
streams = session.streams(link)
stream = streams["best"]
fd = stream.open()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S")
with open("{path}/{model}/{st}_{model}.mp4".format(path=save_directory, model=modelData['username'], st=st), 'wb') as f:
while True:
try:
data = fd.read(1024)
f.write(data)
except:
recording.remove(modelData['username'].lower())
#print("{} stream has ended".format(modelData['username']))
f.close()
return()
recording.remove(modelData['username'].lower())
#print("{} stream has ended".format(modelData['username']))
except:
recording.remove(modelData['username'].lower())
# print("{} stream has ended".format(modelData['username']))
if __name__ == '__main__':
recording = []
while True:
getOnlineModels()
for i in range(30, 0, -1):
sys.stdout.write("\033[K")
print("{} model(s) are being recorded. Next check in {} seconds".format(len(recording), i))
sys.stdout.write("\033[K")
print("the following models are being recorded: {}".format(recording), end="\r")
time.sleep(1)
sys.stdout.write("\033[F")