mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Escape filenames to ensure they can be loaded from Discord
This commit is contained in:
parent
969591a4e6
commit
2e7ef6f008
3 changed files with 26 additions and 11 deletions
|
@ -1,8 +1,13 @@
|
||||||
|
import logging
|
||||||
import ffmpeg
|
import ffmpeg
|
||||||
import os
|
import os
|
||||||
import typing
|
import typing
|
||||||
import time
|
import time
|
||||||
from .youtubedl import YtdlFile, YtdlInfo
|
from .youtubedl import YtdlFile, YtdlInfo
|
||||||
|
from ..utils import safefilename
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class RoyalPCMFile(YtdlFile):
|
class RoyalPCMFile(YtdlFile):
|
||||||
|
@ -20,17 +25,21 @@ class RoyalPCMFile(YtdlFile):
|
||||||
raise FileExistsError("Can't overwrite file")
|
raise FileExistsError("Can't overwrite file")
|
||||||
# Overwrite the new ytdl_args
|
# Overwrite the new ytdl_args
|
||||||
self.ytdl_args = {**self.ytdl_args, **ytdl_args}
|
self.ytdl_args = {**self.ytdl_args, **ytdl_args}
|
||||||
self.ytdl_args["log"].info(f"Now downloading {info.webpage_url}")
|
log.info(f"Now downloading {info.webpage_url}")
|
||||||
super().__init__(info, outtmpl=self._ytdl_filename, **self.ytdl_args)
|
super().__init__(info, outtmpl=self._ytdl_filename, **self.ytdl_args)
|
||||||
# Find the audio_filename with a regex (should be video.opus)
|
# Find the audio_filename with a regex (should be video.opus)
|
||||||
self.ytdl_args["log"].info(f"Preparing {self.video_filename}...")
|
log.info(f"Preparing {self.video_filename}...")
|
||||||
# Convert the video to pcm
|
# Convert the video to pcm
|
||||||
ffmpeg.input(f"./{self.video_filename}") \
|
try:
|
||||||
.output(self.audio_filename, format="s16le", ac=2, ar="48000") \
|
ffmpeg.input(f"./{self.video_filename}") \
|
||||||
.overwrite_output() \
|
.output(self.audio_filename, format="s16le", ac=2, ar="48000") \
|
||||||
.run(quiet=False)
|
.overwrite_output() \
|
||||||
|
.run(quiet=False)
|
||||||
|
except ffmpeg.Error as exc:
|
||||||
|
log.error(f"FFmpeg error: {exc.stderr}")
|
||||||
|
raise
|
||||||
# Delete the video file
|
# Delete the video file
|
||||||
self.ytdl_args["log"].info(f"Deleting {self.video_filename}")
|
log.info(f"Deleting {self.video_filename}")
|
||||||
self.delete_video_file()
|
self.delete_video_file()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -43,12 +52,12 @@ class RoyalPCMFile(YtdlFile):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _ytdl_filename(self):
|
def _ytdl_filename(self):
|
||||||
return f"./downloads/{self.info.title}-{str(int(self._time))}.ytdl"
|
return f"./downloads/{safefilename(self.info.title)}-{safefilename(str(int(self._time)))}.ytdl"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def audio_filename(self):
|
def audio_filename(self):
|
||||||
return f"./downloads/{self.info.title}-{str(int(self._time))}.pcm"
|
return f"./downloads/{safefilename(self.info.title)}-{safefilename(str(int(self._time)))}.pcm"
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.ytdl_args["log"].info(f"Deleting {self.audio_filename}")
|
log.info(f"Deleting {self.audio_filename}")
|
||||||
os.remove(self.audio_filename)
|
os.remove(self.audio_filename)
|
||||||
|
|
|
@ -7,6 +7,7 @@ from .classdictjanitor import cdj
|
||||||
from .sleepuntil import sleep_until
|
from .sleepuntil import sleep_until
|
||||||
from .plusformat import plusformat
|
from .plusformat import plusformat
|
||||||
from .networkhandler import NetworkHandler
|
from .networkhandler import NetworkHandler
|
||||||
|
from .safefilename import safefilename
|
||||||
|
|
||||||
__all__ = ["asyncify", "Call", "Command", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs",
|
__all__ = ["asyncify", "Call", "Command", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs",
|
||||||
"NetworkHandler"]
|
"NetworkHandler", "safefilename"]
|
||||||
|
|
5
royalnet/utils/safefilename.py
Normal file
5
royalnet/utils/safefilename.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def safefilename(string: str):
|
||||||
|
return re.sub(r"\W", "_", string)
|
Loading…
Reference in a new issue