-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
158 lines (118 loc) · 4.98 KB
/
bot.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
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import signal
import subprocess
import datetime
import time
import requests
from config import load_config
from folder import create_folder
class Bot:
error = False
running = True
config = None
processes = []
logger = None
def __init__(self, logger):
self.logger = logger
# load config
self.reload_config()
# reg signals
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
def stop(self, signum, stack):
if self.running:
self.logger.info("Caught stop signal, stopping")
self.running = False
def reload_config(self):
# if config not loaded at all
if self.config is None:
self.config = load_config()
for idx, name in enumerate(self.config["streamers"]):
# add info
self.config["streamers"][idx] = [name, False]
return
# load new
new_config = load_config()
# remove all deleted streamers
for idx, streamer in enumerate(self.config["streamers"]):
if streamer[0] not in new_config["streamers"]:
self.logger.info("{} has been removed".format(streamer[0]))
del self.config["streamers"][idx]
# add all new streamers
for new_streamer in new_config["streamers"]:
# find streamer
found = False
for streamer in self.config["streamers"]:
if streamer[0] == new_streamer:
found = True
# add if not found
if not found:
self.config["streamers"].append([new_streamer, False])
def is_online(self, username):
url = "https://chaturbate.com/get_edge_hls_url_ajax/"
headers = {"X-Requested-With": "XMLHttpRequest"}
data = {"room_slug": username, "bandwidth": "high"}
try:
time.sleep(3) #added sleep to pass cloudflare
r = requests.post(url, headers=headers, data=data)
if r.json()["room_status"] == "public":
return True
return False
except Exception as e:
return None
def run(self):
while self.running:
# debug
try:
# reload config
if self.config["auto_reload_config"]:
self.reload_config()
# check current processes
for idx, rec in enumerate(self.processes):
# check if ended
if rec[1].poll() is not None:
self.logger.info("Stopped recording {}".format(rec[0]))
# set streamer recording to false
for loc, streamer in enumerate(self.config["streamers"]):
if streamer[0] == rec[0]:
self.config["streamers"][loc][1] = False
# remove from proc list
del self.processes[idx]
# check to start recording
for idx, streamer in enumerate(self.config["streamers"]):
# if already recording
if streamer[1]:
continue
# check if online
if self.is_online(streamer[0]):
self.logger.info("Started to record {}".format(streamer[0]))
# prep args
now = datetime.datetime.now()
date = now.strftime("%m-%d-%Y_%H-%M-%S")
create_folder("videos/{}".format(streamer[0]))
args = ["streamlink", # streamlink bin
"https://chaturbate.com/{}/".format(streamer[0]), # chaturbate url
"720p,540p,480p",
"-o",
"videos/{}/{}_{}.mp4".format(streamer[0], streamer[0], date)]
# append idx and process to processes list
self.processes.append([streamer[0], subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)])
# set to recording
self.config["streamers"][idx][1] = True
# check rate limit
if self.config["rate_limit"]:
time.sleep(self.config["rate_limit_time"])
# wait 1 min in 1 second intervals
for i in range(60):
if not self.running:
break
time.sleep(1)
except Exception:
self.logger.exception("loop error")
time.sleep(1)
# loop ended, stop all recording
for rec in self.processes:
# send sigint, wait for end
rec[1].send_signal(signal.SIGINT)
rec[1].wait()
self.logger.info("Successfully stopped")