Add support for premieres

untested as usual
This commit is contained in:
blank X 2021-04-04 19:42:02 +07:00
parent bf9c83f3df
commit 7783ee32e4
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 11 additions and 7 deletions

View File

@ -29,7 +29,7 @@ notify_chat = config['config']['notify_chat']
wait_seconds = config['config']['wait_seconds'] wait_seconds = config['config']['wait_seconds']
channels = config['config']['channels'] channels = config['config']['channels']
live_regex = re.compile(r'error: (?:ytnotifier:([0-9]+) )?this live event will begin in (.+)', re.I) live_regex = re.compile(r'error: (?:ytnotifier:([0-9]+) )?(this live event will begin|premieres) in (.+)', re.I)
strip_date = re.compile(r' \d{4}-\d{2}-\d{2} \d{2}:\d{2}$') strip_date = re.compile(r' \d{4}-\d{2}-\d{2} \d{2}:\d{2}$')
ytdl = YoutubeDL({'skip_download': True, 'no_color': True}) ytdl = YoutubeDL({'skip_download': True, 'no_color': True})
ytdl.add_default_info_extractors() ytdl.add_default_info_extractors()
@ -59,7 +59,7 @@ def traverse_dict(src):
def try_get(src, getter, expected_type=None): def try_get(src, getter, expected_type=None):
if reason := src.get('reason'): if reason := src.get('reason'):
if isinstance(reason, str) and reason.startswith('This live event will begin in '): if isinstance(reason, str) and (reason.startswith('This live event will begin in ') or reason.startswith('Premieres in ')):
t = _try_get(src, traverse_dict, str) t = _try_get(src, traverse_dict, str)
if t: if t:
src['reason'] = f'ytnotifier:{t} {reason}' src['reason'] = f'ytnotifier:{t} {reason}'
@ -81,18 +81,22 @@ async def _handle_video(video_id, video_title):
wait_time = too_many_attempts_count * 60 * 60 wait_time = too_many_attempts_count * 60 * 60
too_many_attempts_count += 1 too_many_attempts_count += 1
elif match := live_regex.match(message.rstrip('.')): elif match := live_regex.match(message.rstrip('.')):
notify_text = 'Live event started'
end_schedule_time = match.group(1) or 0 end_schedule_time = match.group(1) or 0
human_end_schedule_time = match.group(2) is_premiere = match.group(2).lower().startswith('premieres in ')
if is_premiere:
notify_text = 'Premiere started'
else:
notify_text = 'Live event started'
human_end_schedule_time = match.group(3)
if end_schedule_time := int(end_schedule_time): if end_schedule_time := int(end_schedule_time):
tmp_wait_time = end_schedule_time - time.time() tmp_wait_time = end_schedule_time - time.time()
if tmp_wait_time > wait_time: if tmp_wait_time > wait_time:
wait_time = tmp_wait_time wait_time = tmp_wait_time
await client.send_message(notify_chat, f'<b>Live event starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>') await client.send_message(notify_chat, f'<b>{"Premiere" if is_premiere else "Live event"} starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>')
elif not last_was_few_moments: elif not last_was_few_moments:
await client.send_message(notify_chat, f'<b>Live event starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>') await client.send_message(notify_chat, f'<b>{"Premiere" if is_premiere else "Live event"} starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>')
elif not last_was_few_moments: elif not last_was_few_moments:
await client.send_message(notify_chat, f'<b>Live event starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>') await client.send_message(notify_chat, f'<b>{"Premiere" if is_premeire else "Live event"} starting in {human_end_schedule_time}:</b> <a href="{video_url}">{html.escape(video_title)}</a>')
last_was_few_moments = 'moment' in human_end_schedule_time.lower() last_was_few_moments = 'moment' in human_end_schedule_time.lower()
await asyncio.sleep(wait_time) await asyncio.sleep(wait_time)
else: else: