fix(bible): do not use fums on routes that don't support it (part 2)

This commit is contained in:
Seaswimmer 2024-02-02 01:50:31 -05:00
parent 83edd36d6b
commit 09e91da62f
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -35,15 +35,13 @@ class Bible(commands.Cog):
self.config.register_global(bible="de4e12af7f28f599-02") self.config.register_global(bible="de4e12af7f28f599-02")
self.config.register_user(bible=None) self.config.register_user(bible=None)
async def translate_book_name( async def translate_book_name(self, bible_id: str, book_name: str) -> str:
self, ctx: commands.Context, bible_id: str, book_name: str
) -> str:
"""Translate a book name to a book ID.""" """Translate a book name to a book ID."""
book_name_list = [ book_name_list = [
w.lower() if w.lower() == "of" else w.title() for w in book_name.split() w.lower() if w.lower() == "of" else w.title() for w in book_name.split()
] ]
book_name = " ".join(book_name_list) book_name = " ".join(book_name_list)
books = await self._get_books(ctx, bible_id) books = await self._get_books(bible_id)
for book in books: for book in books:
if book_name in (book["abbreviation"], book["name"]): if book_name in (book["abbreviation"], book["name"]):
return book["id"] return book["id"]
@ -103,7 +101,7 @@ class Bible(commands.Cog):
) )
return data["data"] return data["data"]
async def _get_books(self, ctx: commands.Context, bible_id: str) -> dict: async def _get_books(self, bible_id: str) -> dict:
"""Get the books of the Bible from the API.bible API.""" """Get the books of the Bible from the API.bible API."""
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books" url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books"
headers = await self.bot.get_shared_api_tokens("api.bible") headers = await self.bot.get_shared_api_tokens("api.bible")
@ -123,17 +121,12 @@ class Bible(commands.Cog):
raise bible.errors.ServiceUnavailable() raise bible.errors.ServiceUnavailable()
return data["data"] return data["data"]
async def _get_chapters( async def _get_chapters(self, bible_id: str, book_id: str) -> dict:
self, ctx: commands.Context, bible_id: str, book_id: str
) -> dict:
"""Get the chapters of a book from the API.bible API.""" """Get the chapters of a book from the API.bible API."""
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books/{book_id}/chapters" url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books/{book_id}/chapters"
headers = await self.bot.get_shared_api_tokens("api.bible") headers = await self.bot.get_shared_api_tokens("api.bible")
params = {
"fums-version": "3",
}
async with self.session.get(url, headers=headers, params=params) as response: async with self.session.get(url, headers=headers) as response:
data = await response.json() data = await response.json()
self.logger.debug( self.logger.debug(
"_get_chapters executed with a response code of: %s\n%s", "_get_chapters executed with a response code of: %s\n%s",
@ -146,22 +139,6 @@ class Bible(commands.Cog):
raise bible.errors.BibleAccessError() raise bible.errors.BibleAccessError()
if response.status == 503: if response.status == 503:
raise bible.errors.ServiceUnavailable() raise bible.errors.ServiceUnavailable()
fums_url = "https://fums.api.bible/f3"
fums_params = {
"t": data["meta"]["fumsToken"],
"dId": "discord-" + str(self.bot.user.id),
"sId": "discord-" + str(ctx.message.created_at.timestamp()),
"uId": ctx.author.id,
}
async with self.session.get(fums_url, params=fums_params) as response:
fums_data = await response.json()
self.logger.debug(
"_get_chapters FUMS executed with a response code of: %s\n%s",
response.status,
json.dumps(fums_data),
)
return data["data"] return data["data"]
async def _get_verses( async def _get_verses(
@ -219,7 +196,7 @@ class Bible(commands.Cog):
bible_id = await self.config.bible() bible_id = await self.config.bible()
try: try:
book_id = await self.translate_book_name(ctx, bible_id, book) book_id = await self.translate_book_name(bible_id, book)
except ValueError as e: except ValueError as e:
await ctx.send(str(e)) await ctx.send(str(e))
return return
@ -266,10 +243,10 @@ class Bible(commands.Cog):
bible_id = await self.config.bible() bible_id = await self.config.bible()
try: try:
books = await self._get_books(ctx, bible_id) books = await self._get_books(bible_id)
book = random.choice(books) book = random.choice(books)
chapters = await self._get_chapters(ctx, bible_id, book["id"]) chapters = await self._get_chapters(bible_id, book["id"])
chapter = random.choice(chapters) chapter = random.choice(chapters)
verses = await self._get_verses( verses = await self._get_verses(