13
13
14
14
from constants import TMPL_LIVE_STREAM_EMBED_URL , COLORS , TMPL_LIVE_STREAM_URL , VERSION_CHECK_URL , PATH_DEBUG , \
15
15
CURRENT_VERSION , VERSION_ENVIRON , DEBUG_ENVIRON , PATH_CONFIG , UPDATE_DOWNLOAD_URL , NOWAIT_ENVIRON , \
16
- DEFAULT_CHROMIUM_FLAGS , PATH_STATS , SCHEDULE_URL
16
+ DEFAULT_CHROMIUM_FLAGS , PATH_STATS , SCHEDULE_URL , FAKE_USER_AGENT
17
17
18
18
19
19
def get_version (version : str ) -> tuple :
@@ -36,6 +36,7 @@ def get_default_config() -> dict:
36
36
'shut_down' : False ,
37
37
'debug' : False ,
38
38
'time_delta' : False ,
39
+ 'schedule' : False ,
39
40
'chromium_binary' : None ,
40
41
'chromium_flags' : DEFAULT_CHROMIUM_FLAGS ,
41
42
}
@@ -57,6 +58,9 @@ def load_config() -> dict:
57
58
58
59
# v2.0.5
59
60
'shut_down' ,
61
+
62
+ # v2.0.6
63
+ 'schedule' ,
60
64
):
61
65
if new_flag not in content :
62
66
update_config = True
@@ -144,29 +148,69 @@ def kill_headless_chromes(binary_path: str | None = None):
144
148
log_debug (src , f'Failed killing Chrome process(es): { str (e )} ' )
145
149
146
150
151
+ def make_get_request (url : str ):
152
+ return requests .get (url , headers = {'User-Agent' : FAKE_USER_AGENT }, timeout = 10 )
153
+
154
+
147
155
def get_active_stream (channel_id : str ) -> str | None :
148
156
"""Returns stream url if a channel with specified channel_id has active stream"""
149
157
src = 'LiveCheck'
150
158
151
159
try :
152
- response = requests .get ('https://www.youtube.com/channel/%s' % channel_id , timeout = 10 ).text
153
- if "hqdefault_live.jpg" in response :
160
+ response = make_get_request ('https://www.youtube.com/channel/%s' % channel_id ).text
161
+ with open ('response_sample.html' , 'w+' , encoding = 'utf-8' ) as f :
162
+ f .write (response )
163
+ f .close ()
164
+ if 'hqdefault_live.jpg' in response :
154
165
video_id = re .search (r'vi/(.*?)/hqdefault_live.jpg' , response ).group (1 )
155
166
return TMPL_LIVE_STREAM_URL % video_id
156
- except requests . RequestException as e :
167
+ except Exception as e :
157
168
log_error (src , f'&rLive stream check failed: { str (e )} ' )
158
- tb = traceback .format_exc ()
159
- make_debug_file ('failed-getting-active-stream' , tb )
169
+ make_debug_file ('failed-getting-active-stream' , traceback .format_exc ())
160
170
return
161
171
162
- def get_relative_time ():
163
- resSCH = requests .get (SCHEDULE_URL , timeout = 10 ).text
164
- JSONSCH = json .loads (re .search (r'statusText":"Watch Online","date":(.*?),"competitors":' , resSCH ).group (1 ))
165
- unix_time = (JSONSCH ['startDate' ])
166
- dt = datetime .fromtimestamp (unix_time / 1000 , timezone .utc )
167
- now = datetime .now (timezone .utc )
168
- delta_time = dt - now
169
- return str (delta_time - timedelta (microseconds = delta_time .microseconds ))
172
+
173
+ def get_seconds_till_next_match () -> float | None :
174
+ src = 'Schedule'
175
+
176
+ try :
177
+ # getting page json
178
+ schedule_html = requests .get (SCHEDULE_URL , timeout = 10 ).text
179
+ next_data = (
180
+ schedule_html
181
+ .split ('<script id="__NEXT_DATA__" type="application/json">' )[1 ]
182
+ .split ('</script>' )[0 ].strip ()
183
+ )
184
+ schedule_json = json .loads (next_data )
185
+
186
+ # getting matches
187
+ blocks = schedule_json ['props' ]['pageProps' ]['blocks' ]
188
+ matches : list = []
189
+ for block in blocks :
190
+ if 'owlHeader' in block .keys ():
191
+ matches = block ['owlHeader' ]['scoreStripList' ]['scoreStrip' ]['matches' ]
192
+ break
193
+
194
+ if not matches :
195
+ raise Exception ('Could not get matches.' )
196
+
197
+ pending_matches = list (filter (lambda match : match .get ('status' ) == 'PENDING' , matches ))
198
+ pending_matches .sort (key = lambda match : match ['date' ]['startDate' ])
199
+
200
+ if not pending_matches :
201
+ raise Exception (f"No matches with status 'PENDING'." )
202
+
203
+ timestamp_ms = pending_matches [0 ]['date' ]['startDate' ]
204
+ delta = datetime .fromtimestamp (timestamp_ms / 1000 , timezone .utc ) - datetime .now (timezone .utc )
205
+ total_seconds = delta .total_seconds ()
206
+
207
+ log_info (src , f'Closest match will be played in { delta } .' )
208
+
209
+ return total_seconds
210
+ except Exception as e :
211
+ log_error (src , f'&rSchedule check failed: { str (e )} . Falling back to regular checks.' )
212
+ make_debug_file ('failed-getting-schedule' , traceback .format_exc ())
213
+
170
214
171
215
def check_for_new_version ():
172
216
log_src = 'Version'
0 commit comments