From a35f66c387471af438930b96a9f1cfd9b3e18f77 Mon Sep 17 00:00:00 2001 From: insert Date: Sat, 31 May 2025 20:17:11 -0400 Subject: [PATCH] Add option to ban videos with specific words & general code clean up --- .env.example | 3 ++- newbot.py | 51 +++++++++++++++++---------------------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/.env.example b/.env.example index 600de66..4f69757 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,5 @@ MAX_QUEUE="5" ALLOW_SKIP="TRUE" PERMANENT_MAX_QUEUE="FALSE" LOCK_SHUFFLE="FALSE" -USE_PROXY="TRUE MEANING YOU HAVE TO EDIT THE PROXIES LIST IN THE FILE OR FALSE" \ No newline at end of file +USE_PROXY="TRUE MEANING YOU HAVE TO EDIT THE PROXIES LIST IN THE FILE OR FALSE" +BLOCK_REGEX="" \ No newline at end of file diff --git a/newbot.py b/newbot.py index 2318cbd..0ebf467 100644 --- a/newbot.py +++ b/newbot.py @@ -184,13 +184,9 @@ def video_check(info, *, incomplete): vid_details["channel"] = info.get("channel") print(info.get("channel")) print(info.get("title")) - if duration and duration >= ((float(os.getenv("MAX_MIN")) * 60) + 480): - queue.pop(0) - propagate_queue(1) - skip_list.clear() - if queue: - download_video(0,True) - return "video too long... :(" + banned_video = re.compile(os.getenv("BLOCK_REGEX",""), re.IGNORECASE).findall(f"{vid_details['title']} {vid_details['channel']}") + if (duration and duration >= ((float(os.getenv("MAX_MIN")) * 60) + 480)) or banned_video: + return "video too long" #TODO why is this an issue if its the second video played obs = obsws_python.ReqClient() @@ -221,10 +217,9 @@ async def on_ready(): await loop.run_in_executor(None, cold_run) if len(queue) > 1: download_video(1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") downloading = False -def download_video(index,bypass=False): +def download_video(index,bypass=False,renameoffset=1): global retries global downloading if bypass: @@ -240,9 +235,10 @@ def download_video(index,bypass=False): try: downloading = True ydl.download(queue[index]) - retries = 0 sleep(2) #allow ytdlp to fully cleanup + os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+renameoffset}.mp4") downloading = False + retries = 0 return except Exception as e: print("handling youtube exception") @@ -253,11 +249,15 @@ def download_video(index,bypass=False): download_video(index,True) failures = 0 return - if retries % 2 == 1: + if "no such file or directory" in str(e).lower(): #Thanks for the silent fail ytdlp queue.pop(index) propagate_queue(1) - failures = failures + 1 - retries = retries + 1 + else: + if retries % 2 == 1: + queue.pop(index) + propagate_queue(1) + failures = failures + 1 + retries = retries + 1 if len(queue) >= index+1: download_video(index,True) else: @@ -270,7 +270,6 @@ def wait_for_next_video(): return stat = obs.get_scene_item_id("youtube", "notice") print(stat.scene_item_id) - #if not os.path.isfile(f"{vid_dir}/{vidcounter+1}.mp4"): print("Attempting to enable") obs.set_scene_item_enabled("youtube", stat.scene_item_id, True) while not os.path.isfile(f"{vid_dir}/{vidcounter+1}.mp4"): @@ -279,7 +278,6 @@ def wait_for_next_video(): if counter == 120 and len(os.listdir(full_dl_dir)) == 0: print("failsafe activated") download_video(0,True) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") obs.set_scene_item_enabled("youtube", stat.scene_item_id, False) return @@ -287,8 +285,9 @@ def cold_run(): scene = obs.get_current_program_scene() if scene.scene_name != "waiting": return - download_video(0) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter}.mp4") + download_video(0,renameoffset=0) + if not os.path.isfile(f"{vid_dir}/{vidcounter}.mp4"): + return obs.set_current_program_scene("youtube") nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") stat = obs.get_scene_item_id("youtube", "notice") @@ -297,9 +296,6 @@ def cold_run(): obs.set_input_settings("nowplaying", {'text': f'{vid_details["title"]}\nBy {vid_details["channel"]}'}, True) obs.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True) obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART") - #if len(queue) > 1: - # download_video(1) - # os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") def on_media_input_playback_ended(data): global vidcounter @@ -316,7 +312,6 @@ def on_media_input_playback_ended(data): print("alternative download triggered") propagate_queue(2) download_video(0) #will be noticeably slow but this should not happen - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") else: obs.set_current_program_scene("waiting") os.remove(f"{vid_dir}/{vidcounter}.mp4") @@ -324,10 +319,8 @@ def on_media_input_playback_ended(data): return wait_for_next_video() print(queue) - #obs.set_current_program_scene("youtube2") os.remove(f"{vid_dir}/{vidcounter}.mp4") - vidcounter = vidcounter + 1 - #os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter}.mp4") + vidcounter = vidcounter + 1 print("changing obs settigs") scene = obs.get_current_program_scene() if scene.scene_name != "youtube": @@ -342,11 +335,9 @@ def on_media_input_playback_ended(data): obs.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True) #same as above if len(queue) > 1: download_video(1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") elif sqllen() >= 1 and len(queue) == 1: propagate_queue(1) download_video(1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") @bot.slash_command( name="stats", @@ -393,7 +384,6 @@ async def play(inter: disnake.AppCmdInter, link: str): scene = obs.get_current_program_scene() if (not os.path.isfile(f"{vid_dir}/{vidcounter}.mp4")) and scene.scene_name == "waiting" and sqllen() >= 1 and len(queue) == 0: loop = asyncio.get_running_loop() - #queue.clear() #safety propagate_queue(1) await loop.run_in_executor(None, cold_run) return @@ -401,7 +391,6 @@ async def play(inter: disnake.AppCmdInter, link: str): loop = asyncio.get_running_loop() propagate_queue(2) await loop.run_in_executor(None, download_video, 1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") return return else: @@ -558,7 +547,6 @@ async def skip(inter: disnake.AppCmdInter): return await loop.run_in_executor(None, wait_for_next_video) print("stopping video") - #obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE") os.remove(f"{vid_dir}/{vidcounter}.mp4") vidcounter = vidcounter + 1 nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") @@ -569,7 +557,6 @@ async def skip(inter: disnake.AppCmdInter): await inter.edit_original_response("skipped as sufficient votes were reached") if len(queue) > 1: await loop.run_in_executor(None, download_video, 1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") @bot.slash_command( @@ -626,8 +613,6 @@ async def voteskip(inter: disnake.AppCmdInter): await inter.edit_original_response("skipped as sufficient votes were reached") if len(queue) > 1: await loop.run_in_executor(None, download_video, 1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") - else: await inter.edit_original_response(f"**{inter.user.display_name}** has voted to skip the video, {len(skip_list)}/{math.floor(len(vc)/2)}") @@ -658,7 +643,6 @@ async def videotimer(): return await loop.run_in_executor(None, wait_for_next_video) print("stopping video") - #obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE") os.remove(f"{vid_dir}/{vidcounter}.mp4") vidcounter = vidcounter + 1 nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") @@ -668,7 +652,6 @@ async def videotimer(): obs.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True) if len(queue) > 1: await loop.run_in_executor(None, download_video, 1) - os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4") except Exception: pass return