Take snip into account
This commit is contained in:
parent
3625ff5d4a
commit
7a643a3079
1 changed files with 28 additions and 11 deletions
|
@ -82,23 +82,34 @@ class YTDLCog(commands.Cog):
|
||||||
await db.commit()
|
await db.commit()
|
||||||
return
|
return
|
||||||
|
|
||||||
async def save_link(self, message: discord.Message, webpage_url: str, format_id: str, attachment_index: int = 0):
|
async def save_link(
|
||||||
|
self,
|
||||||
|
message: discord.Message,
|
||||||
|
webpage_url: str,
|
||||||
|
format_id: str,
|
||||||
|
attachment_index: int = 0,
|
||||||
|
*,
|
||||||
|
snip: typing.Optional[str] = None
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Saves a link to discord to prevent having to re-download it.
|
Saves a link to discord to prevent having to re-download it.
|
||||||
:param message: The download message with the attachment.
|
:param message: The download message with the attachment.
|
||||||
:param webpage_url: The "webpage_url" key of the metadata
|
:param webpage_url: The "webpage_url" key of the metadata
|
||||||
:param format_id: The "format_Id" key of the metadata
|
:param format_id: The "format_Id" key of the metadata
|
||||||
:param attachment_index: The index of the attachment. Defaults to 0
|
:param attachment_index: The index of the attachment. Defaults to 0
|
||||||
|
:param snip: The start and end time to snip the video. e.g. 00:00:00-00:10:00
|
||||||
:return: The created hash key
|
:return: The created hash key
|
||||||
"""
|
"""
|
||||||
|
snip = snip or '*'
|
||||||
await self._init_db()
|
await self._init_db()
|
||||||
async with aiosqlite.connect("./data/ytdl.db") as db:
|
async with aiosqlite.connect("./data/ytdl.db") as db:
|
||||||
_hash = hashlib.md5(f"{webpage_url}:{format_id}".encode()).hexdigest()
|
_hash = hashlib.md5(f"{webpage_url}:{format_id}:{snip}".encode()).hexdigest()
|
||||||
self.log.debug(
|
self.log.debug(
|
||||||
"Saving %r (%r:%r) with message %d>%d, index %d",
|
"Saving %r (%r:%r:%r) with message %d>%d, index %d",
|
||||||
_hash,
|
_hash,
|
||||||
webpage_url,
|
webpage_url,
|
||||||
format_id,
|
format_id,
|
||||||
|
snip,
|
||||||
message.channel.id,
|
message.channel.id,
|
||||||
message.id,
|
message.id,
|
||||||
attachment_index
|
attachment_index
|
||||||
|
@ -117,20 +128,27 @@ class YTDLCog(commands.Cog):
|
||||||
await db.commit()
|
await db.commit()
|
||||||
return _hash
|
return _hash
|
||||||
|
|
||||||
async def get_saved(self, webpage_url: str, format_id: str) -> typing.Optional[str]:
|
async def get_saved(
|
||||||
|
self,
|
||||||
|
webpage_url: str,
|
||||||
|
format_id: str,
|
||||||
|
snip: str
|
||||||
|
) -> typing.Optional[str]:
|
||||||
"""
|
"""
|
||||||
Attempts to retrieve the attachment URL of a previously saved download.
|
Attempts to retrieve the attachment URL of a previously saved download.
|
||||||
:param webpage_url: The webpage url
|
:param webpage_url: The webpage url
|
||||||
:param format_id: The format ID
|
:param format_id: The format ID
|
||||||
|
:param snip: The start and end time to snip the video. e.g. 00:00:00-00:10:00
|
||||||
:return: the URL, if found and valid.
|
:return: the URL, if found and valid.
|
||||||
"""
|
"""
|
||||||
await self._init_db()
|
await self._init_db()
|
||||||
async with aiosqlite.connect("./data/ytdl.db") as db:
|
async with aiosqlite.connect("./data/ytdl.db") as db:
|
||||||
_hash = hashlib.md5(f"{webpage_url}:{format_id}".encode()).hexdigest()
|
_hash = hashlib.md5(f"{webpage_url}:{format_id}:{snip}".encode()).hexdigest()
|
||||||
self.log.debug(
|
self.log.debug(
|
||||||
"Attempting to find a saved download for '%s:%s' (%r).",
|
"Attempting to find a saved download for '%s:%s:%s' (%r).",
|
||||||
webpage_url,
|
webpage_url,
|
||||||
format_id,
|
format_id,
|
||||||
|
snip,
|
||||||
_hash
|
_hash
|
||||||
)
|
)
|
||||||
cursor = await db.execute(
|
cursor = await db.execute(
|
||||||
|
@ -229,7 +247,6 @@ class YTDLCog(commands.Cog):
|
||||||
snip: typing.Annotated[
|
snip: typing.Annotated[
|
||||||
typing.Optional[str],
|
typing.Optional[str],
|
||||||
discord.Option(
|
discord.Option(
|
||||||
str,
|
|
||||||
description="A start and end position to trim. e.g. 00:00:00-00:10:00.",
|
description="A start and end position to trim. e.g. 00:00:00-00:10:00.",
|
||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
|
@ -347,7 +364,7 @@ class YTDLCog(commands.Cog):
|
||||||
colour=self.colours.get(domain, discord.Colour.og_blurple())
|
colour=self.colours.get(domain, discord.Colour.og_blurple())
|
||||||
).set_footer(text="Downloading (step 2/10)").set_thumbnail(url=thumbnail_url)
|
).set_footer(text="Downloading (step 2/10)").set_thumbnail(url=thumbnail_url)
|
||||||
)
|
)
|
||||||
previous = await self.get_saved(webpage_url, extracted_info["format_id"])
|
previous = await self.get_saved(webpage_url, extracted_info["format_id"], snip or '*')
|
||||||
if previous:
|
if previous:
|
||||||
await ctx.edit(
|
await ctx.edit(
|
||||||
content=previous,
|
content=previous,
|
||||||
|
@ -505,7 +522,7 @@ class YTDLCog(commands.Cog):
|
||||||
url=webpage_url
|
url=webpage_url
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
await self.save_link(msg, webpage_url, chosen_format_id)
|
await self.save_link(msg, webpage_url, chosen_format_id, snip=snip or '*')
|
||||||
except discord.HTTPException as e:
|
except discord.HTTPException as e:
|
||||||
self.log.error(e, exc_info=True)
|
self.log.error(e, exc_info=True)
|
||||||
return await ctx.edit(
|
return await ctx.edit(
|
||||||
|
|
Loading…
Reference in a new issue