mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-20 02:26:32 +01:00
Add dump-metadata command
This commit is contained in:
parent
b142ee4bfb
commit
1b0b1630e0
1 changed files with 56 additions and 0 deletions
|
@ -1,12 +1,28 @@
|
||||||
|
import io
|
||||||
import shutil
|
import shutil
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
|
||||||
|
class TransparentQueue(asyncio.Queue):
|
||||||
|
def __init__(self, maxsize: int = 0) -> None:
|
||||||
|
super().__init__(maxsize)
|
||||||
|
self._internal_queue = []
|
||||||
|
|
||||||
|
async def put(self, item):
|
||||||
|
await super().put(item)
|
||||||
|
self._internal_queue.append(item)
|
||||||
|
|
||||||
|
async def job_done(self):
|
||||||
|
await super().job_done()
|
||||||
|
self._internal_queue.pop(0)
|
||||||
|
|
||||||
|
|
||||||
class YTDLSource(discord.PCMVolumeTransformer):
|
class YTDLSource(discord.PCMVolumeTransformer):
|
||||||
def __init__(self, source: discord.AudioSource, *, data: dict, volume: float = 0.5):
|
def __init__(self, source: discord.AudioSource, *, data: dict, volume: float = 0.5):
|
||||||
super().__init__(source, volume)
|
super().__init__(source, volume)
|
||||||
|
@ -16,6 +32,10 @@ class YTDLSource(discord.PCMVolumeTransformer):
|
||||||
self.title = data.get("title")
|
self.title = data.get("title")
|
||||||
self.url = data.get("url")
|
self.url = data.get("url")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def duration(self):
|
||||||
|
return self.data.get("duration")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_url(cls, ytdl: yt_dlp.YoutubeDL, url, *, loop=None, stream=False):
|
async def from_url(cls, ytdl: yt_dlp.YoutubeDL, url, *, loop=None, stream=False):
|
||||||
ffmpeg_options = {"options": "-vn -b:a 44.1k"}
|
ffmpeg_options = {"options": "-vn -b:a 44.1k"}
|
||||||
|
@ -51,6 +71,31 @@ class VoiceCog(commands.Cog):
|
||||||
}
|
}
|
||||||
self.cache = Path(tempfile.mkdtemp("jimmy-voice-cache-")).resolve()
|
self.cache = Path(tempfile.mkdtemp("jimmy-voice-cache-")).resolve()
|
||||||
self.yt_dl = yt_dlp.YoutubeDL(self.ytdl_options)
|
self.yt_dl = yt_dlp.YoutubeDL(self.ytdl_options)
|
||||||
|
# self.queue = TransparentQueue(100)
|
||||||
|
# self._queue_task = self.bot.loop.create_task(self.queue_task())
|
||||||
|
|
||||||
|
async def queue_task(self):
|
||||||
|
if not self.bot.is_ready():
|
||||||
|
await self.bot.wait_until_ready()
|
||||||
|
while True:
|
||||||
|
ctx, player, author, inserted_at = await self.queue.get()
|
||||||
|
ctx.guild.voice_client.play(player, after=self.after_player(ctx))
|
||||||
|
|
||||||
|
embed = discord.Embed(
|
||||||
|
description=f"Now playing: [{player.title}]({player.url}), as requested by {author.mention}.",
|
||||||
|
color=discord.Color.green(),
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await ctx.respond(
|
||||||
|
embed=embed
|
||||||
|
)
|
||||||
|
except discord.HTTPException:
|
||||||
|
try:
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
except discord.HTTPException:
|
||||||
|
pass
|
||||||
|
|
||||||
|
await self.queue.job_done()
|
||||||
|
|
||||||
def cog_unload(self):
|
def cog_unload(self):
|
||||||
shutil.rmtree(self.cache)
|
shutil.rmtree(self.cache)
|
||||||
|
@ -122,6 +167,17 @@ class VoiceCog(commands.Cog):
|
||||||
else:
|
else:
|
||||||
await ctx.respond("Not connected to a voice channel.")
|
await ctx.respond("Not connected to a voice channel.")
|
||||||
|
|
||||||
|
@commands.command(name="dump-metadata")
|
||||||
|
async def dump_metadata(self, ctx: commands.Context, *, url: str):
|
||||||
|
"""Dumps JSON YT-DLP metadata to a file"""
|
||||||
|
async with ctx.channel.typing():
|
||||||
|
file = io.StringIO()
|
||||||
|
data = await self.unblock(self.yt_dl.extract_info, url, download=False)
|
||||||
|
data = await self.unblock(self.yt_dl.sanitize_info, data)
|
||||||
|
json.dump(data, file, indent=4)
|
||||||
|
file.seek(0)
|
||||||
|
return await ctx.respond(file=discord.File(file, filename="metadata.json"))
|
||||||
|
|
||||||
async def cog_before_invoke(self, ctx: discord.ApplicationContext):
|
async def cog_before_invoke(self, ctx: discord.ApplicationContext):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
if not self.cache.exists():
|
if not self.cache.exists():
|
||||||
|
|
Loading…
Reference in a new issue