Add option to ban videos with specific words & general code clean up

This commit is contained in:
insert 2025-05-31 20:17:11 -04:00
parent 771dd20259
commit a35f66c387
Signed by: insert
GPG key ID: A70775C389ACF105
2 changed files with 19 additions and 35 deletions

View file

@ -8,4 +8,5 @@ MAX_QUEUE="5"
ALLOW_SKIP="TRUE" ALLOW_SKIP="TRUE"
PERMANENT_MAX_QUEUE="FALSE" PERMANENT_MAX_QUEUE="FALSE"
LOCK_SHUFFLE="FALSE" LOCK_SHUFFLE="FALSE"
USE_PROXY="TRUE MEANING YOU HAVE TO EDIT THE PROXIES LIST IN THE FILE OR FALSE" USE_PROXY="TRUE MEANING YOU HAVE TO EDIT THE PROXIES LIST IN THE FILE OR FALSE"
BLOCK_REGEX=""

View file

@ -184,13 +184,9 @@ def video_check(info, *, incomplete):
vid_details["channel"] = info.get("channel") vid_details["channel"] = info.get("channel")
print(info.get("channel")) print(info.get("channel"))
print(info.get("title")) print(info.get("title"))
if duration and duration >= ((float(os.getenv("MAX_MIN")) * 60) + 480): banned_video = re.compile(os.getenv("BLOCK_REGEX",""), re.IGNORECASE).findall(f"{vid_details['title']} {vid_details['channel']}")
queue.pop(0) if (duration and duration >= ((float(os.getenv("MAX_MIN")) * 60) + 480)) or banned_video:
propagate_queue(1) return "video too long" #TODO why is this an issue if its the second video played
skip_list.clear()
if queue:
download_video(0,True)
return "video too long... :("
obs = obsws_python.ReqClient() obs = obsws_python.ReqClient()
@ -221,10 +217,9 @@ async def on_ready():
await loop.run_in_executor(None, cold_run) await loop.run_in_executor(None, cold_run)
if len(queue) > 1: if len(queue) > 1:
download_video(1) download_video(1)
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4")
downloading = False downloading = False
def download_video(index,bypass=False): def download_video(index,bypass=False,renameoffset=1):
global retries global retries
global downloading global downloading
if bypass: if bypass:
@ -240,9 +235,10 @@ def download_video(index,bypass=False):
try: try:
downloading = True downloading = True
ydl.download(queue[index]) ydl.download(queue[index])
retries = 0
sleep(2) #allow ytdlp to fully cleanup sleep(2) #allow ytdlp to fully cleanup
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+renameoffset}.mp4")
downloading = False downloading = False
retries = 0
return return
except Exception as e: except Exception as e:
print("handling youtube exception") print("handling youtube exception")
@ -253,11 +249,15 @@ def download_video(index,bypass=False):
download_video(index,True) download_video(index,True)
failures = 0 failures = 0
return return
if retries % 2 == 1: if "no such file or directory" in str(e).lower(): #Thanks for the silent fail ytdlp
queue.pop(index) queue.pop(index)
propagate_queue(1) propagate_queue(1)
failures = failures + 1 else:
retries = retries + 1 if retries % 2 == 1:
queue.pop(index)
propagate_queue(1)
failures = failures + 1
retries = retries + 1
if len(queue) >= index+1: if len(queue) >= index+1:
download_video(index,True) download_video(index,True)
else: else:
@ -270,7 +270,6 @@ def wait_for_next_video():
return return
stat = obs.get_scene_item_id("youtube", "notice") stat = obs.get_scene_item_id("youtube", "notice")
print(stat.scene_item_id) print(stat.scene_item_id)
#if not os.path.isfile(f"{vid_dir}/{vidcounter+1}.mp4"):
print("Attempting to enable") print("Attempting to enable")
obs.set_scene_item_enabled("youtube", stat.scene_item_id, True) obs.set_scene_item_enabled("youtube", stat.scene_item_id, True)
while not os.path.isfile(f"{vid_dir}/{vidcounter+1}.mp4"): 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: if counter == 120 and len(os.listdir(full_dl_dir)) == 0:
print("failsafe activated") print("failsafe activated")
download_video(0,True) 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) obs.set_scene_item_enabled("youtube", stat.scene_item_id, False)
return return
@ -287,8 +285,9 @@ def cold_run():
scene = obs.get_current_program_scene() scene = obs.get_current_program_scene()
if scene.scene_name != "waiting": if scene.scene_name != "waiting":
return return
download_video(0) download_video(0,renameoffset=0)
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter}.mp4") if not os.path.isfile(f"{vid_dir}/{vidcounter}.mp4"):
return
obs.set_current_program_scene("youtube") obs.set_current_program_scene("youtube")
nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") nowplayingid = obs.get_scene_item_id("youtube", "nowplaying")
stat = obs.get_scene_item_id("youtube", "notice") 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_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.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True)
obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART") 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): def on_media_input_playback_ended(data):
global vidcounter global vidcounter
@ -316,7 +312,6 @@ def on_media_input_playback_ended(data):
print("alternative download triggered") print("alternative download triggered")
propagate_queue(2) propagate_queue(2)
download_video(0) #will be noticeably slow but this should not happen 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: else:
obs.set_current_program_scene("waiting") obs.set_current_program_scene("waiting")
os.remove(f"{vid_dir}/{vidcounter}.mp4") os.remove(f"{vid_dir}/{vidcounter}.mp4")
@ -324,10 +319,8 @@ def on_media_input_playback_ended(data):
return return
wait_for_next_video() wait_for_next_video()
print(queue) print(queue)
#obs.set_current_program_scene("youtube2")
os.remove(f"{vid_dir}/{vidcounter}.mp4") os.remove(f"{vid_dir}/{vidcounter}.mp4")
vidcounter = vidcounter + 1 vidcounter = vidcounter + 1
#os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter}.mp4")
print("changing obs settigs") print("changing obs settigs")
scene = obs.get_current_program_scene() scene = obs.get_current_program_scene()
if scene.scene_name != "youtube": 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 obs.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True) #same as above
if len(queue) > 1: if len(queue) > 1:
download_video(1) download_video(1)
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4")
elif sqllen() >= 1 and len(queue) == 1: elif sqllen() >= 1 and len(queue) == 1:
propagate_queue(1) propagate_queue(1)
download_video(1) download_video(1)
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4")
@bot.slash_command( @bot.slash_command(
name="stats", name="stats",
@ -393,7 +384,6 @@ async def play(inter: disnake.AppCmdInter, link: str):
scene = obs.get_current_program_scene() 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: 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() loop = asyncio.get_running_loop()
#queue.clear() #safety
propagate_queue(1) propagate_queue(1)
await loop.run_in_executor(None, cold_run) await loop.run_in_executor(None, cold_run)
return return
@ -401,7 +391,6 @@ async def play(inter: disnake.AppCmdInter, link: str):
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
propagate_queue(2) propagate_queue(2)
await loop.run_in_executor(None, download_video, 1) await loop.run_in_executor(None, download_video, 1)
os.rename(f"{vid_dir}/999zznext.mp4", f"{vid_dir}/{vidcounter+1}.mp4")
return return
return return
else: else:
@ -558,7 +547,6 @@ async def skip(inter: disnake.AppCmdInter):
return return
await loop.run_in_executor(None, wait_for_next_video) await loop.run_in_executor(None, wait_for_next_video)
print("stopping video") print("stopping video")
#obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE")
os.remove(f"{vid_dir}/{vidcounter}.mp4") os.remove(f"{vid_dir}/{vidcounter}.mp4")
vidcounter = vidcounter + 1 vidcounter = vidcounter + 1
nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") 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") await inter.edit_original_response("skipped as sufficient votes were reached")
if len(queue) > 1: if len(queue) > 1:
await loop.run_in_executor(None, download_video, 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( @bot.slash_command(
@ -626,8 +613,6 @@ async def voteskip(inter: disnake.AppCmdInter):
await inter.edit_original_response("skipped as sufficient votes were reached") await inter.edit_original_response("skipped as sufficient votes were reached")
if len(queue) > 1: if len(queue) > 1:
await loop.run_in_executor(None, download_video, 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)}") 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 return
await loop.run_in_executor(None, wait_for_next_video) await loop.run_in_executor(None, wait_for_next_video)
print("stopping video") print("stopping video")
#obs.trigger_media_input_action("player", "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE")
os.remove(f"{vid_dir}/{vidcounter}.mp4") os.remove(f"{vid_dir}/{vidcounter}.mp4")
vidcounter = vidcounter + 1 vidcounter = vidcounter + 1
nowplayingid = obs.get_scene_item_id("youtube", "nowplaying") 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) obs.set_scene_item_enabled("youtube", nowplayingid.scene_item_id, True)
if len(queue) > 1: if len(queue) > 1:
await loop.run_in_executor(None, download_video, 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: except Exception:
pass pass
return return