feat: add sponsorblock integration
Add the -S option to the fast forward command.
This commit is contained in:
parent
3ca58a4c19
commit
640e750e3d
@ -2,6 +2,7 @@ import disnake_paginator
|
||||
|
||||
import arguments
|
||||
import commands
|
||||
import sponsorblock
|
||||
import utils
|
||||
from constants import EMBED_COLOR
|
||||
from state import players
|
||||
@ -89,11 +90,19 @@ async def pause(message):
|
||||
async def fast_forward(message):
|
||||
tokens = commands.tokenize(message.content)
|
||||
parser = arguments.ArgumentParser(tokens[0], "fast forward audio playback")
|
||||
parser.add_argument(
|
||||
"seconds",
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument(
|
||||
"-s",
|
||||
"--seconds",
|
||||
type=lambda v: arguments.range_type(v, min=0, max=300),
|
||||
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)):
|
||||
return
|
||||
|
||||
@ -104,8 +113,26 @@ async def fast_forward(message):
|
||||
await utils.reply(message, "nothing is playing!")
|
||||
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.source.original.fast_forward(args.seconds)
|
||||
message.guild.voice_client.source.original.fast_forward(seconds)
|
||||
message.guild.voice_client.resume()
|
||||
|
||||
await utils.add_check_reaction(message)
|
||||
|
@ -36,6 +36,7 @@ RELOADABLE_MODULES = [
|
||||
"events",
|
||||
"extra",
|
||||
"fun",
|
||||
"sponsorblock",
|
||||
"tasks",
|
||||
"utils",
|
||||
"voice",
|
||||
|
21
sponsorblock.py
Normal file
21
sponsorblock.py
Normal 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]
|
Loading…
x
Reference in New Issue
Block a user