-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
sites.py
233 lines (181 loc) · 8.23 KB
/
sites.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import datetime
import json
import logging
import time
from dateutil.relativedelta import relativedelta
from ctvbot import utils
from ctvbot.instance import Instance
logger = logging.getLogger(__name__)
class Unknown(Instance):
name = "UNKNOWN"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def todo_every_loop(self):
self.page.keyboard.press("Tab")
def update_status(self):
pass
def todo_after_spawn(self):
self.goto_with_retry(self.target_url)
self.page.wait_for_timeout(1000)
class Youtube(Instance):
name = "YOUTUBE"
cookie_css = ".eom-button-row.style-scope.ytd-consent-bump-v2-lightbox > ytd-button-renderer:nth-child(1) button"
now_timestamp_ms = int(time.time() * 1000)
next_year_timestamp_ms = int((datetime.datetime.now() + relativedelta(years=1)).timestamp() * 1000)
local_storage = {
"yt-player-quality": r"""{{"data":"{{\\"quality\\":144,\\"previousQuality\\":144}}","expiration":{0},"creation":{1}}}""".format(
next_year_timestamp_ms, now_timestamp_ms
),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def todo_every_loop(self):
self.page.keyboard.press("Tab")
try:
self.page.click("button.ytp-ad-skip-button", timeout=100)
except:
pass
def update_status(self):
current_time = datetime.datetime.now()
if not self.status_info:
self.status_info = {
"last_active_resume_time": 0,
"last_active_timestamp": current_time - datetime.timedelta(seconds=10),
"last_stream_id": None,
}
# If the stream was active less than 10 seconds ago, it's still being watched
time_since_last_activity = current_time - self.status_info["last_active_timestamp"]
if time_since_last_activity < datetime.timedelta(seconds=15):
self.status = utils.InstanceStatus.WATCHING
return
# Fetch the current resume time for the stream
current_resume_time = int(
self.page.evaluate(
'''() => {
const element = document.querySelector(".ytp-progress-bar");
return element.getAttribute("aria-valuenow");
}'''
)
)
if current_resume_time:
# If the current resume time has advanced past the last active resume time, update and set status to
if current_resume_time > self.status_info["last_active_resume_time"]:
self.status_info["last_active_timestamp"] = current_time
self.status_info["last_active_resume_time"] = current_resume_time
self.status = utils.InstanceStatus.WATCHING
return
# If none of the above conditions are met, the stream is buffering
self.status = utils.InstanceStatus.BUFFERING
def todo_after_spawn(self):
self.goto_with_retry("https://www.youtube.com/")
self.page.wait_for_timeout(1000)
try:
self.page.click(self.cookie_css, timeout=10000)
except:
logger.warning("Cookie consent banner not found/clicked.")
for key, value in self.local_storage.items():
tosend = """window.localStorage.setItem('{key}','{value}');""".format(key=key, value=value)
self.page.evaluate(tosend)
self.goto_with_retry(self.target_url)
self.page.keyboard.press("t")
self.status = utils.InstanceStatus.INITIALIZED
class Kick(Instance):
name = "KICK"
local_storage = {
"agreed_to_mature_content": "true",
"kick_cookie_accepted": "true",
"kick_video_size": "160p",
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def todo_every_loop(self):
self.page.keyboard.press("Tab")
def update_status(self):
pass
def todo_after_spawn(self):
self.goto_with_retry("https://kick.com/auth/login")
self.page.wait_for_timeout(5000)
for key, value in self.local_storage.items():
tosend = """window.localStorage.setItem('{key}','{value}');""".format(key=key, value=value)
self.page.evaluate(tosend)
self.goto_with_retry(self.target_url)
self.page.wait_for_timeout(1000)
if 'cloudflare' in self.page.content().lower():
raise utils.CloudflareBlockException("Blocked by Cloudflare.")
class Twitch(Instance):
name = "TWITCH"
cookie_css = "button[data-a-target=consent-banner-accept]"
local_storage = {
"mature": "true",
"video-muted": '{"default": "false"}',
"volume": "0.5",
"video-quality": '{"default": "160p30"}',
"lowLatencyModeEnabled": "false",
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def todo_after_load(self):
self.page.wait_for_selector(".persistent-player", timeout=30000)
self.page.wait_for_timeout(1000)
self.page.keyboard.press("Alt+t")
def update_status(self):
current_time = datetime.datetime.now()
if not self.status_info:
self.status_info = {
"last_active_resume_time": 0,
"last_active_timestamp": current_time - datetime.timedelta(seconds=10),
"last_stream_id": None,
}
# If the stream was active less than 10 seconds ago, it's still being watched
time_since_last_activity = current_time - self.status_info["last_active_timestamp"]
if time_since_last_activity < datetime.timedelta(seconds=10):
self.status = utils.InstanceStatus.WATCHING
return
# Fetch the current resume time for the stream
fetched_resume_times = self.page.evaluate("window.localStorage.getItem('livestreamResumeTimes');")
if fetched_resume_times:
resume_times_dict = json.loads(fetched_resume_times)
current_stream_id = list(resume_times_dict.keys())[-1]
current_resume_time = list(resume_times_dict.values())[-1]
# If this is the first run, set the last stream id to current stream id
if not self.status_info["last_stream_id"]:
self.status_info["last_stream_id"] = current_stream_id
# If the stream has restarted, reset last_active_resume_time
if current_stream_id != self.status_info["last_stream_id"]:
self.status_info["last_stream_id"] = current_stream_id
self.status_info["last_active_resume_time"] = 0
# If the current resume time has advanced past the last active resume time, update and set status to
if current_resume_time > self.status_info["last_active_resume_time"]:
self.status_info["last_active_timestamp"] = current_time
self.status_info["last_active_resume_time"] = current_resume_time
self.status = utils.InstanceStatus.WATCHING
return
# If none of the above conditions are met, the stream is buffering
self.status = utils.InstanceStatus.BUFFERING
def todo_after_spawn(self):
self.goto_with_retry("https://www.twitch.tv/login")
try:
self.page.click(self.cookie_css, timeout=15000)
except:
logger.warning("Cookie consent banner not found/clicked.")
for key, value in self.local_storage.items():
tosend = """window.localStorage.setItem('{key}','{value}');""".format(key=key, value=value)
self.page.evaluate(tosend)
self.page.set_viewport_size(
{
"width": self.location_info["width"],
"height": self.location_info["height"],
}
)
self.goto_with_retry(self.target_url)
self.page.wait_for_timeout(1000)
self.page.wait_for_selector(".persistent-player", timeout=15000)
self.page.keyboard.press("Alt+t")
self.page.wait_for_timeout(1000)
try:
self.page.click(
"button[data-a-target=content-classification-gate-overlay-start-watching-button]", timeout=3000
)
except:
logger.info("Mature button not found/clicked.")
self.status = utils.InstanceStatus.INITIALIZED