feat: add sponsorblock integration

Add the -S option to the fast forward command.
This commit is contained in:
Ryan 2025-01-22 14:46:58 -05:00
parent 3ca58a4c19
commit 640e750e3d
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 53 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import disnake_paginator
import arguments import arguments
import commands import commands
import sponsorblock
import utils import utils
from constants import EMBED_COLOR from constants import EMBED_COLOR
from state import players from state import players
@ -89,11 +90,19 @@ async def pause(message):
async def fast_forward(message): async def fast_forward(message):
tokens = commands.tokenize(message.content) tokens = commands.tokenize(message.content)
parser = arguments.ArgumentParser(tokens[0], "fast forward audio playback") parser = arguments.ArgumentParser(tokens[0], "fast forward audio playback")
parser.add_argument( group = parser.add_mutually_exclusive_group(required=True)
"seconds", group.add_argument(
"-s",
"--seconds",
type=lambda v: arguments.range_type(v, min=0, max=300), type=lambda v: arguments.range_type(v, min=0, max=300),
help="the amount of seconds to fast forward", help="the amount of seconds to fast forward",
) )
group.add_argument(
"-S",
"--sponsorblock",
action="store_true",
help="go to the end of the current sponsorblock segment",
)
if not (args := await parser.parse_args(message, tokens)): if not (args := await parser.parse_args(message, tokens)):
return return
@ -104,8 +113,26 @@ async def fast_forward(message):
await utils.reply(message, "nothing is playing!") await utils.reply(message, "nothing is playing!")
return return
seconds = args.seconds
if args.sponsorblock:
video = sponsorblock.get_segments(players[message.guild.id].current.player.id)
if not video:
await utils.reply(
message, "no sponsorblock segments were found for this video!"
)
return
progress = message.guild.voice_client.source.original.progress
for segment in video["segments"]:
begin, end = map(float, segment["segment"])
if progress >= begin and progress < end:
seconds = end - message.guild.voice_client.source.original.progress
if not seconds:
await utils.reply(message, "no sponsorblock segment is currently playing!")
return
message.guild.voice_client.pause() message.guild.voice_client.pause()
message.guild.voice_client.source.original.fast_forward(args.seconds) message.guild.voice_client.source.original.fast_forward(seconds)
message.guild.voice_client.resume() message.guild.voice_client.resume()
await utils.add_check_reaction(message) await utils.add_check_reaction(message)

View File

@ -36,6 +36,7 @@ RELOADABLE_MODULES = [
"events", "events",
"extra", "extra",
"fun", "fun",
"sponsorblock",
"tasks", "tasks",
"utils", "utils",
"voice", "voice",

21
sponsorblock.py Normal file
View File

@ -0,0 +1,21 @@
import hashlib
import requests
from state import sponsorblock_cache
def get_segments(videoId: str):
if videoId in sponsorblock_cache:
return sponsorblock_cache[videoId]
hashPrefix = hashlib.sha256(videoId.encode()).hexdigest()[:4]
response = requests.get(
f'https://sponsor.ajay.app/api/skipSegments/{hashPrefix}?categories=["music_offtopic"]'
)
print(response.text)
if response.status_code == 200 and (
results := list(filter(lambda v: videoId == v["videoID"], response.json()))
):
sponsorblock_cache[videoId] = results[0]
return results[0]

View File

@ -15,4 +15,5 @@ idle_tracker = {"is_idle": False, "last_used": time.time()}
kill = {"transcript": False} kill = {"transcript": False}
message_responses = LimitedSizeDict() message_responses = LimitedSizeDict()
players = {} players = {}
sponsorblock_cache = LimitedSizeDict(size_limit=100)
start_time = time.time() start_time = time.time()