mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Add /dlmusic command
This commit is contained in:
parent
ae69c077a0
commit
1006909881
6 changed files with 106 additions and 4 deletions
|
@ -5,5 +5,6 @@ from .ytdlinfo import YtdlInfo
|
||||||
from .ytdlfile import YtdlFile
|
from .ytdlfile import YtdlFile
|
||||||
from .fileaudiosource import FileAudioSource
|
from .fileaudiosource import FileAudioSource
|
||||||
from .ytdldiscord import YtdlDiscord
|
from .ytdldiscord import YtdlDiscord
|
||||||
|
from .ytdlvorbis import YtdlVorbis
|
||||||
|
|
||||||
__all__ = ["playmodes", "YtdlInfo", "YtdlFile", "FileAudioSource", "YtdlDiscord"]
|
__all__ = ["playmodes", "YtdlInfo", "YtdlFile", "FileAudioSource", "YtdlDiscord", "YtdlVorbis"]
|
||||||
|
|
|
@ -28,6 +28,7 @@ class YtdlFile:
|
||||||
return self.info is not None
|
return self.info is not None
|
||||||
|
|
||||||
def is_downloaded(self) -> bool:
|
def is_downloaded(self) -> bool:
|
||||||
|
# FIXME: Weird characters break everything
|
||||||
return self.filename is not None and os.path.exists(self.filename)
|
return self.filename is not None and os.path.exists(self.filename)
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
|
66
royalnet/audio/ytdlvorbis.py
Normal file
66
royalnet/audio/ytdlvorbis.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import typing
|
||||||
|
import re
|
||||||
|
import ffmpeg
|
||||||
|
import os
|
||||||
|
from .ytdlinfo import YtdlInfo
|
||||||
|
from .ytdlfile import YtdlFile
|
||||||
|
from .fileaudiosource import FileAudioSource
|
||||||
|
|
||||||
|
|
||||||
|
class YtdlVorbis:
|
||||||
|
def __init__(self, ytdl_file: YtdlFile):
|
||||||
|
self.ytdl_file: YtdlFile = ytdl_file
|
||||||
|
self.vorbis_filename: typing.Optional[str] = None
|
||||||
|
self._fas_spawned: typing.List[FileAudioSource] = []
|
||||||
|
|
||||||
|
def pcm_available(self):
|
||||||
|
return self.vorbis_filename is not None and os.path.exists(self.vorbis_filename)
|
||||||
|
|
||||||
|
def convert_to_vorbis(self) -> None:
|
||||||
|
if not self.ytdl_file.is_downloaded():
|
||||||
|
raise FileNotFoundError("File hasn't been downloaded yet")
|
||||||
|
destination_filename = re.sub(r"\.[^.]+$", ".mp3", self.ytdl_file.filename)
|
||||||
|
out, err = (
|
||||||
|
ffmpeg.input(self.ytdl_file.filename)
|
||||||
|
.output(destination_filename, format="mp3")
|
||||||
|
.overwrite_output()
|
||||||
|
.run()
|
||||||
|
)
|
||||||
|
self.vorbis_filename = destination_filename
|
||||||
|
|
||||||
|
def ready_up(self):
|
||||||
|
if not self.ytdl_file.has_info():
|
||||||
|
self.ytdl_file.update_info()
|
||||||
|
if not self.ytdl_file.is_downloaded():
|
||||||
|
self.ytdl_file.download_file()
|
||||||
|
if not self.pcm_available():
|
||||||
|
self.convert_to_vorbis()
|
||||||
|
|
||||||
|
def delete(self) -> None:
|
||||||
|
if self.pcm_available():
|
||||||
|
for source in self._fas_spawned:
|
||||||
|
if not source.file.closed:
|
||||||
|
source.file.close()
|
||||||
|
os.remove(self.vorbis_filename)
|
||||||
|
self.vorbis_filename = None
|
||||||
|
self.ytdl_file.delete()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_from_url(cls, url, **ytdl_args) -> typing.List["YtdlVorbis"]:
|
||||||
|
files = YtdlFile.download_from_url(url, **ytdl_args)
|
||||||
|
dfiles = []
|
||||||
|
for file in files:
|
||||||
|
dfile = YtdlVorbis(file)
|
||||||
|
dfiles.append(dfile)
|
||||||
|
return dfiles
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_and_ready_from_url(cls, url, **ytdl_args) -> typing.List["YtdlVorbis"]:
|
||||||
|
dfiles = cls.create_from_url(url, **ytdl_args)
|
||||||
|
for dfile in dfiles:
|
||||||
|
dfile.ready_up()
|
||||||
|
return dfiles
|
||||||
|
|
||||||
|
@property
|
||||||
|
def info(self) -> typing.Optional[YtdlInfo]:
|
||||||
|
return self.ytdl_file.info
|
|
@ -29,10 +29,11 @@ from .pause import PauseCommand
|
||||||
from .queue import QueueCommand
|
from .queue import QueueCommand
|
||||||
from .royalnetprofile import RoyalnetprofileCommand
|
from .royalnetprofile import RoyalnetprofileCommand
|
||||||
from .id import IdCommand
|
from .id import IdCommand
|
||||||
|
from .dlmusic import DlmusicCommand
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand",
|
__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand",
|
||||||
"SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand",
|
"SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand",
|
||||||
"KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand",
|
"KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand",
|
||||||
"SkipCommand", "PlaymodeCommand", "VideochannelCommand", "MissingCommand", "CvCommand", "PauseCommand",
|
"SkipCommand", "PlaymodeCommand", "VideochannelCommand", "MissingCommand", "CvCommand", "PauseCommand",
|
||||||
"QueueCommand", "RoyalnetprofileCommand", "IdCommand"]
|
"QueueCommand", "RoyalnetprofileCommand", "IdCommand", "DlmusicCommand"]
|
||||||
|
|
31
royalnet/commands/dlmusic.py
Normal file
31
royalnet/commands/dlmusic.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import asyncio
|
||||||
|
import typing
|
||||||
|
import urllib.parse
|
||||||
|
from ..utils import Command, Call, asyncify
|
||||||
|
from ..audio import YtdlVorbis
|
||||||
|
|
||||||
|
|
||||||
|
ytdl_args = {
|
||||||
|
"format": "bestaudio",
|
||||||
|
"outtmpl": f"./downloads/%(epoch)s_%(title)s.%(ext)s"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
seconds_before_deletion = 15*60
|
||||||
|
|
||||||
|
|
||||||
|
class DlmusicCommand(Command):
|
||||||
|
|
||||||
|
command_name = "dlmusic"
|
||||||
|
command_description = "Scarica un video."
|
||||||
|
command_syntax = "(url)"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def common(cls, call: Call):
|
||||||
|
url = call.args[0]
|
||||||
|
files: typing.List[YtdlVorbis] = await asyncify(YtdlVorbis.create_and_ready_from_url, url, **ytdl_args)
|
||||||
|
for file in files:
|
||||||
|
await call.reply(f"⬇️ https://scaleway.steffo.eu/musicbot_cache/{urllib.parse.quote(file.vorbis_filename)}")
|
||||||
|
await asyncio.sleep(seconds_before_deletion)
|
||||||
|
for file in files:
|
||||||
|
file.delete()
|
|
@ -18,17 +18,19 @@ log = logging.root
|
||||||
stream_handler = logging.StreamHandler()
|
stream_handler = logging.StreamHandler()
|
||||||
stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{")
|
stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{")
|
||||||
log.addHandler(stream_handler)
|
log.addHandler(stream_handler)
|
||||||
log.setLevel(logging.INFO)
|
|
||||||
|
|
||||||
commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand,
|
commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand,
|
||||||
DiarioCommand, RageCommand, ReminderCommand, KvactiveCommand, KvCommand,
|
DiarioCommand, RageCommand, ReminderCommand, KvactiveCommand, KvCommand,
|
||||||
KvrollCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand,
|
KvrollCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand,
|
||||||
VideochannelCommand, CvCommand, PauseCommand, QueueCommand, RoyalnetprofileCommand, VideoinfoCommand,
|
VideochannelCommand, CvCommand, PauseCommand, QueueCommand, RoyalnetprofileCommand, VideoinfoCommand,
|
||||||
IdCommand]
|
IdCommand, DlmusicCommand]
|
||||||
|
|
||||||
# noinspection PyUnreachableCode
|
# noinspection PyUnreachableCode
|
||||||
if __debug__:
|
if __debug__:
|
||||||
commands = [DebugCreateCommand, DateparserCommand, AuthorCommand, *commands]
|
commands = [DebugCreateCommand, DateparserCommand, AuthorCommand, *commands]
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
else:
|
||||||
|
log.setLevel(logging.INFO)
|
||||||
|
|
||||||
address, port = "127.0.0.1", 1234
|
address, port = "127.0.0.1", 1234
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue