Allow a cache lookup to be performed even in error
This commit is contained in:
parent
d1efcf91f1
commit
adb7faf433
1 changed files with 71 additions and 2 deletions
|
@ -3,6 +3,7 @@ import asyncio
|
|||
import json
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import typing
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
|
@ -77,13 +78,21 @@ class MSCGetter(niobot.Module):
|
|||
)
|
||||
response.raise_for_status()
|
||||
issues = response.json()["items"]
|
||||
except httpx.HTTPStatusError as err:
|
||||
issue = {
|
||||
"html_url": "https://http.cat/%d" % err.response.status_code,
|
||||
"title": "There was an issue contacting GitHub's search."
|
||||
}
|
||||
if err.response.status_code == 429:
|
||||
issue["title"] = "Ratelimited while querying github. Try again later."
|
||||
found.append(issue)
|
||||
except Exception:
|
||||
return [
|
||||
found.append(
|
||||
{
|
||||
"title": "Error querying GitHub.",
|
||||
"html_url": "https://http.cat/500"
|
||||
}
|
||||
]
|
||||
)
|
||||
else:
|
||||
for issue in issues:
|
||||
file = self.msc_cache / ("%d.json" % issue["number"])
|
||||
|
@ -183,6 +192,66 @@ class MSCGetter(niobot.Module):
|
|||
return await msg.edit(data["error"])
|
||||
return await msg.edit(self.pr_to_display(data))
|
||||
|
||||
@niobot.command("msc.cache")
|
||||
@niobot.is_owner()
|
||||
async def msc_cache_manager(self, ctx: niobot.Context, *args):
|
||||
"""
|
||||
MSC Cache manager
|
||||
|
||||
Available commands: get, download, remove, clear, list
|
||||
"""
|
||||
operation, *args = args
|
||||
if operation == "list":
|
||||
known = []
|
||||
total_size = 0
|
||||
for file in self.msc_cache.glob("*.json"):
|
||||
known.append(int(file.name.split(".")[0]))
|
||||
total_size += (await asyncio.to_thread(
|
||||
file.stat
|
||||
)).st_size
|
||||
known.sort()
|
||||
|
||||
units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]
|
||||
size = 0
|
||||
while size > 1024:
|
||||
units.pop(0)
|
||||
size /= 1024
|
||||
|
||||
return await ctx.respond(
|
||||
"{:,} cached entries (amounting to {:,.2f}{}):\n{}".format(
|
||||
len(known),
|
||||
size,
|
||||
units[0],
|
||||
", ".join(map(str, known))
|
||||
)
|
||||
)
|
||||
elif operation in ["get", "view"]:
|
||||
file = self.msc_cache / ("{:0>4}.json".format(int(args[0])))
|
||||
if file.exists():
|
||||
data = json.loads(file.read_text())
|
||||
out = json.dumps(data, indent=4)
|
||||
return await ctx.respond(
|
||||
"```json\n%s\n```" % out
|
||||
)
|
||||
return await ctx.respond("%s does not exist." % file.name)
|
||||
elif operation in ["download", "dl", "fetch"]:
|
||||
x = await self.get_msc_with_cache(int(args[0]))
|
||||
return await ctx.respond(
|
||||
"```json\n%s\n```" % json.dumps(x, indent=4)
|
||||
)
|
||||
elif operation in ["delete", "remove", "rm", "del"]:
|
||||
file = self.msc_cache / ("{:0>4}.json".format(int(args[0])))
|
||||
if file.exists():
|
||||
file.remove()
|
||||
return await ctx.respond("Removed cached file %s." % file.name)
|
||||
return await ctx.respond("No cached file to remove")
|
||||
elif operation == "clear":
|
||||
shutil.rmtree(self.msc_cache)
|
||||
self.msc_cache.mkdir()
|
||||
return await ctx.respond("Cleared all cached MSCs.")
|
||||
else:
|
||||
return await ctx.respond("Unknown operation.")
|
||||
|
||||
@niobot.command("automsc.enable")
|
||||
async def auto_msc_enable(self, ctx: niobot.Context):
|
||||
"""Automatically enables MSC linking. Requires a power level of at least 50."""
|
||||
|
|
Loading…
Reference in a new issue