Add some debug to saved io

This commit is contained in:
Nexus 2024-01-09 09:47:01 +00:00
parent 2c6ee6e533
commit 7e87b9b5ac
2 changed files with 41 additions and 6 deletions

View file

@ -4,7 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="aa6d62a8-d64d-4a60-a85f-8d9fa52b6b49" name="Changes" comment="Fix aiosqlite again">
<list default="true" id="aa6d62a8-d64d-4a60-a85f-8d9fa52b6b49" name="Changes" comment="Forgot to hexdigest">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cogs/ytdl.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cogs/ytdl.py" afterDir="false" />
</list>
@ -81,7 +81,7 @@
<workItem from="1704572901106" duration="125000" />
<workItem from="1704573038888" duration="7749000" />
<workItem from="1704760229948" duration="946000" />
<workItem from="1704790508193" duration="2436000" />
<workItem from="1704790508193" duration="2855000" />
</task>
<task id="LOCAL-00001" summary="Update gitignore">
<option name="closed" value="true" />
@ -275,7 +275,23 @@
<option name="project" value="LOCAL" />
<updated>1704793106430</updated>
</task>
<option name="localTasksCounter" value="25" />
<task id="LOCAL-00025" summary="I hate SQL">
<option name="closed" value="true" />
<created>1704793198163</created>
<option name="number" value="00025" />
<option name="presentableId" value="LOCAL-00025" />
<option name="project" value="LOCAL" />
<updated>1704793198163</updated>
</task>
<task id="LOCAL-00026" summary="Forgot to hexdigest">
<option name="closed" value="true" />
<created>1704793320744</created>
<option name="number" value="00026" />
<option name="presentableId" value="LOCAL-00026" />
<option name="project" value="LOCAL" />
<updated>1704793320744</updated>
</task>
<option name="localTasksCounter" value="27" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -283,7 +299,6 @@
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="Update gitignore" />
<MESSAGE value="Beta 1&#10;&#10;* Screenshot (unpolished, functional)&#10;* Dig&#10;* Traceroute&#10;* Whois&#10;* Ping&#10;* yt-dl" />
<MESSAGE value="Beta 1&#10;&#10;* Screenshot (unpolished, functional)&#10;* Dig&#10;* Traceroute&#10;* Whois&#10;* Ping&#10;* yt-dl&#10;* Docker file" />
<MESSAGE value="Update the docker image" />
@ -307,6 +322,8 @@
<MESSAGE value="Allow re-use of previous downloads" />
<MESSAGE value="Fix aiosqlite" />
<MESSAGE value="Fix aiosqlite again" />
<option name="LAST_COMMIT_MESSAGE" value="Fix aiosqlite again" />
<MESSAGE value="I hate SQL" />
<MESSAGE value="Forgot to hexdigest" />
<option name="LAST_COMMIT_MESSAGE" value="Forgot to hexdigest" />
</component>
</project>

View file

@ -93,6 +93,13 @@ class YTDLCog(commands.Cog):
await self._init_db()
async with aiosqlite.connect("./data/ytdl.db") as db:
_hash = hashlib.md5(f"{webpage_url}:{format_id}".encode()).hexdigest()
self.log.debug(
"Saving %r (%r:%r) with message %d>%d, index %d",
_hash,
message.channel.id,
message.id,
attachment_index
)
await db.execute(
"""
INSERT INTO downloads (key, message_id, channel_id, webpage_url, format_id, attachment_index)
@ -113,26 +120,37 @@ class YTDLCog(commands.Cog):
await self._init_db()
async with aiosqlite.connect("./data/ytdl.db") as db:
_hash = hashlib.md5(f"{webpage_url}:{format_id}".encode()).hexdigest()
self.log.debug(
"Attempting to find a saved download for '%s:%s' (%r).",
webpage_url,
format_id,
_hash
)
cursor = await db.execute(
"SELECT message_id, channel_id, attachment_index FROM downloads WHERE key=?",
(_hash,)
)
entry = await cursor.fetchone()
if not entry:
self.log.debug("There was no saved download.")
return
message_id, channel_id, attachment_index = entry
channel = self.bot.get_channel(channel_id)
if not channel:
self.log.debug("Channel %r was not found.", channel_id)
return
try:
message = await channel.fetch_message(message_id)
except discord.HTTPException:
self.log.debug("%r did not contain a message with ID %r", channel, message_id)
await db.execute("DELETE FROM downloads WHERE key=?", (_hash,))
return
try:
return message.attachments[attachment_index].url
url = message.attachments[attachment_index].url
self.log.debug("Found URL %r, returning.", url)
except IndexError:
self.log.debug("Attachment index %d is out of range (%r)", attachment_index, message.attachments)
return