forked from Balgden/ChaturbateRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChaturbateRecorder.py
145 lines (137 loc) · 6.31 KB
/
ChaturbateRecorder.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
import time, datetime, os, sys, requests, configparser, re, subprocess
if os.name == 'nt':
import ctypes
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
from queue import Queue
from livestreamer import Livestreamer
from threading import Thread
Config = configparser.ConfigParser()
Config.read(sys.path[0] + "/config.conf")
save_directory = Config.get('paths', 'save_directory')
wishlist = Config.get('paths', 'wishlist')
interval = int(Config.get('settings', 'checkInterval'))
genders = re.sub(' ', '', Config.get('settings', 'genders')).split(",")
directory_structure = Config.get('paths', 'directory_structure').lower()
postProcessingCommand = Config.get('settings', 'postProcessingCommand')
try:
postProcessingThreads = int(Config.get('settings', 'postProcessingThreads'))
except ValueError:
pass
completed_directory = Config.get('paths', 'completed_directory').lower()
def now():
return '[' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ']'
recording = []
wanted = []
def startRecording(model):
global postProcessingCommand
global processingQueue
try:
result = requests.get('https://chaturbate.com/api/chatvideocontext/{}/'.format(model)).json()
session = Livestreamer()
session.set_option('http-headers', "referer=https://www.chaturbate.com/{}".format(model))
streams = session.streams("hlsvariant://{}".format(result['hls_source'].rsplit('?')[0]))
stream = streams["best"]
fd = stream.open()
now = datetime.datetime.now()
filePath = directory_structure.format(path=save_directory, model=model, gender=result['broadcaster_gender'],
seconds=now.strftime("%S"),
minutes=now.strftime("%M"), hour=now.strftime("%H"),
day=now.strftime("%d"),
month=now.strftime("%m"), year=now.strftime("%Y"))
directory = filePath.rsplit('/', 1)[0]+'/'
if not os.path.exists(directory):
os.makedirs(directory)
if model in recording: return
with open(filePath, 'wb') as f:
recording.append(model)
while model in wanted:
try:
data = fd.read(1024)
f.write(data)
except:
f.close()
break
if postProcessingCommand:
processingQueue.put({'model':model, 'path':filePath, 'gender':gender})
elif completed_directory:
finishedDir = completed_directory.format(path=save_directory, model=model,
gender=gender, seconds=now.strftime("%S"),
minutes=now.strftime("%M"),hour=now.strftime("%H"), day=now.strftime("%d"),
month=now.strftime("%m"), year=now.strftime("%Y"))
if not os.path.exists(finishedDir):
os.makedirs(finishedDir)
os.rename(filePath, finishedDir+'/'+filePath.rsplit['/',1][0])
except: pass
finally:
if model in recording:recording.remove(model)
def postProcess():
global processingQueue
global postProcessingCommand
while True:
while processingQueue.empty():
time.sleep(1)
parameters = processingQueue.get()
model = parameters['model']
path = parameters['path']
filename = path.rsplit('/', 1)[1]
gender = parameters['gender']
directory = path.rsplit('/', 1)[0]+'/'
subprocess.run(postProcessingCommand.split() + [path, filename, directory, model, gender])
def getOnlineModels():
online = []
global wanted
for gender in genders:
try:
data = {'categories': gender, 'num': 127}
result = requests.post("https://roomlister.stream.highwebmedia.com/session/start/", data=data).json()
length = len(result['rooms'])
online.extend([m['username'].lower() for m in result['rooms']])
data['key'] = result['key']
while length == 127:
result = requests.post("https://roomlister.stream.highwebmedia.com/session/next/", data=data).json()
length = len(result['rooms'])
data['key'] = result['key']
online.extend([m['username'].lower() for m in result['rooms']])
except:
break
f = open(wishlist, 'r')
wanted = list(set(f.readlines()))
wanted = [m.strip('\n').split('chaturbate.com/')[-1].lower().strip().replace('/', '') for m in wanted]
#wantedModels = list(set(wanted).intersection(online).difference(recording))
'''new method for building list - testing issue #19 yet again'''
wantedModels = [m for m in (list(set(wanted))) if m in online and m not in recording]
for theModel in wantedModels:
thread = Thread(target=startRecording, args=(theModel,))
thread.start()
f.close()
if __name__ == '__main__':
AllowedGenders = ['female', 'male', 'trans', 'couple']
for gender in genders:
if gender.lower() not in AllowedGenders:
print(gender, "is not an acceptable gender, options are: female, male, trans, and couple - please correct your config file")
exit()
genders = [a.lower()[0] for a in genders]
print()
if postProcessingCommand != "":
processingQueue = Queue()
postprocessingWorkers = []
for i in range(0, postProcessingThreads):
t = Thread(target=postProcess)
postprocessingWorkers.append(t)
t.start()
sys.stdout.write("\033[F")
while True:
sys.stdout.write("\033[K")
print( now(),"{} model(s) are being recorded. Getting list of online models now".format(len(recording)))
sys.stdout.write("\033[K")
print("the following models are being recorded: {}".format(recording), end="\r")
getOnlineModels()
sys.stdout.write("\033[F")
for i in range(interval, 0, -1):
sys.stdout.write("\033[K")
print(now(), "{} 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")