1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Do some more stuff

This commit is contained in:
Steffo 2020-01-31 18:54:15 +01:00
parent 23ba2ed746
commit 71e2fbfaea
6 changed files with 7 additions and 76 deletions

View file

@ -17,13 +17,11 @@ except ImportError:
from .ytdlinfo import YtdlInfo from .ytdlinfo import YtdlInfo
from .ytdlfile import YtdlFile from .ytdlfile import YtdlFile
from .ytdlmp3 import YtdlMp3
from .errors import BardError, YtdlError, NotFoundError, MultipleFilesError from .errors import BardError, YtdlError, NotFoundError, MultipleFilesError
__all__ = [ __all__ = [
"YtdlInfo", "YtdlInfo",
"YtdlFile", "YtdlFile",
"YtdlMp3",
"BardError", "BardError",
"YtdlError", "YtdlError",
"NotFoundError", "NotFoundError",

View file

@ -1,67 +0,0 @@
from typing import *
import re
import os
import ffmpeg
import royalnet.utils as ru
from .ytdlinfo import YtdlInfo
from .ytdlfile import YtdlFile
class YtdlMp3:
"""A representation of a :class:`YtdlFile` conversion to mp3."""
def __init__(self, ytdl_file: YtdlFile):
self.ytdl_file: YtdlFile = ytdl_file
self.mp3_filename: Optional[str] = None
self.lock: ru.MultiLock = ru.MultiLock()
def __repr__(self):
if not self.ytdl_file.has_info:
return f"<{self.__class__.__qualname__} without info>"
elif not self.ytdl_file.is_downloaded:
return f"<{self.__class__.__qualname__} not downloaded>"
elif not self.is_converted:
return f"<{self.__class__.__qualname__} at '{self.ytdl_file.filename}' not converted>"
else:
return f"<{self.__class__.__qualname__} at '{self.mp3_filename}'>"
@property
def is_converted(self):
"""Has the file been converted?"""
return self.mp3_filename is not None
async def convert_to_mp3(self) -> None:
"""Convert the file to mp3 with :mod:`ffmpeg`."""
await self.ytdl_file.download_file()
if self.mp3_filename is None:
async with self.ytdl_file.lock.normal():
destination_filename = re.sub(r"\.[^.]+$", ".mp3", self.ytdl_file.filename)
async with self.lock.exclusive():
await ru.asyncify(
ffmpeg.input(self.ytdl_file.filename)
.output(destination_filename, format="mp3")
.overwrite_output()
.run
)
self.mp3_filename = destination_filename
async def delete_asap(self) -> None:
"""Delete the mp3 file."""
if self.is_converted:
async with self.lock.exclusive():
os.remove(self.mp3_filename)
self.mp3_filename = None
@classmethod
async def from_url(cls, url, **ytdl_args) -> List["YtdlMp3"]:
"""Create a :class:`list` of :class:`YtdlMp3` from a URL."""
files = await YtdlFile.from_url(url, **ytdl_args)
dfiles = []
for file in files:
dfile = YtdlMp3(file)
dfiles.append(dfile)
return dfiles
@property
def info(self) -> Optional[YtdlInfo]:
"""Shortcut to get the :class:`YtdlInfo` of the object."""
return self.ytdl_file.info

View file

@ -1,5 +1,5 @@
import re import re
from typing import * import typing
from .errors import InvalidInputError from .errors import InvalidInputError
@ -66,7 +66,7 @@ class CommandArgs(list):
raise InvalidInputError(f"Not enough arguments specified (minimum is {require_at_least}).") raise InvalidInputError(f"Not enough arguments specified (minimum is {require_at_least}).")
return " ".join(self) return " ".join(self)
def match(self, pattern: Union[str, Pattern], *flags) -> Sequence[AnyStr]: def match(self, pattern: typing.Union[str, typing.Pattern], *flags) -> typing.Sequence[typing.AnyStr]:
"""Match the :meth:`.joined` string to a :class:`re.Pattern`-like object. """Match the :meth:`.joined` string to a :class:`re.Pattern`-like object.
Parameters: Parameters:
@ -83,7 +83,7 @@ class CommandArgs(list):
raise InvalidInputError("Invalid syntax.") raise InvalidInputError("Invalid syntax.")
return match.groups() return match.groups()
def optional(self, index: int, default=None) -> Optional[str]: def optional(self, index: int, default=None) -> typing.Optional[str]:
"""Get the argument at a specific index, but don't raise an error if nothing is found, instead returning the """Get the argument at a specific index, but don't raise an error if nothing is found, instead returning the
``default`` value. ``default`` value.

View file

@ -18,7 +18,7 @@ class Event:
"""The :class:`CommandInterface` available to this :class:`Event`.""" """The :class:`CommandInterface` available to this :class:`Event`."""
@property @property
def serf(self) -> Serf: def serf(self) -> "Serf":
"""A shortcut for :attr:`.interface.serf`.""" """A shortcut for :attr:`.interface.serf`."""
return self.interface.serf return self.interface.serf

View file

@ -20,7 +20,6 @@ class VoicePlayer:
self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
else: else:
self.loop = loop self.loop = loop
# FIXME: this looks like spaghetti
self._playback_ended_event: threading.Event = threading.Event() self._playback_ended_event: threading.Event = threading.Event()
async def connect(self, channel: "discord.VoiceChannel") -> "discord.VoiceClient": async def connect(self, channel: "discord.VoiceChannel") -> "discord.VoiceClient":
@ -88,6 +87,7 @@ class VoicePlayer:
raise PlayerNotConnectedError() raise PlayerNotConnectedError()
if self.voice_client.is_playing(): if self.voice_client.is_playing():
raise PlayerAlreadyPlaying() raise PlayerAlreadyPlaying()
self.playing = None
log.debug("Getting next AudioSource...") log.debug("Getting next AudioSource...")
next_source: Optional["discord.AudioSource"] = await self.playing.next() next_source: Optional["discord.AudioSource"] = await self.playing.next()
if next_source is None: if next_source is None:
@ -98,7 +98,6 @@ class VoicePlayer:
self.loop.create_task(self._playback_check()) self.loop.create_task(self._playback_check())
async def _playback_check(self): async def _playback_check(self):
# FIXME: quite spaghetti
while True: while True:
if self._playback_ended_event.is_set(): if self._playback_ended_event.is_set():
self._playback_ended_event.clear() self._playback_ended_event.clear()

View file

@ -8,6 +8,7 @@ import royalnet.utils as ru
import royalnet.alchemy as ra import royalnet.alchemy as ra
import royalnet.backpack as rb import royalnet.backpack as rb
import royalnet.herald as rh import royalnet.herald as rh
import traceback
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -39,7 +40,7 @@ class Serf:
try: try:
packs[pack_name] = importlib.import_module(pack_name) packs[pack_name] = importlib.import_module(pack_name)
except ImportError as e: except ImportError as e:
log.error(f"Error during the import of {pack_name}: {e}") log.error(f"{e.__class__.__name__} during the import of {pack_name}: {e}")
log.info(f"Packs: {len(packs)} imported") log.info(f"Packs: {len(packs)} imported")
self.alchemy: Optional[ra.Alchemy] = None self.alchemy: Optional[ra.Alchemy] = None