feat(bible): implemented API.bible's FUMS system

This commit is contained in:
Seaswimmer 2024-02-02 01:35:43 -05:00
parent 220e8da790
commit d24d8f9603
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -48,7 +48,11 @@ class Bible(commands.Cog):
raise ValueError(error(f"Book {book_name} not found."))
async def _get_passage(
self, bible_id: str, passage_id: str, include_verse_numbers: bool
self,
ctx: commands.Context,
bible_id: str,
passage_id: str,
include_verse_numbers: bool,
) -> dict:
"""Get a Bible passage from the API.bible API."""
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/passages/{passage_id}"
@ -78,9 +82,25 @@ class Bible(commands.Cog):
raise bible.errors.NotFound()
if response.status == 503:
raise bible.errors.ServiceUnavailable()
return data["data"]
async def _get_books(self, bible_id: str) -> dict:
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_passage FUMS executed with a response code of: %s\n%s",
response.status,
json.dumps(fums_data),
)
return data["data"]
async def _get_books(self, ctx: commands.Context, bible_id: str) -> dict:
"""Get the books of the Bible from the API.bible API."""
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books"
headers = await self.bot.get_shared_api_tokens("api.bible")
@ -98,9 +118,27 @@ class Bible(commands.Cog):
raise bible.errors.BibleAccessError()
if response.status == 503:
raise bible.errors.ServiceUnavailable()
return data["data"]
async def _get_chapters(self, bible_id: str, book_id: str) -> dict:
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_books FUMS executed with a response code of: %s\n%s",
response.status,
json.dumps(fums_data),
)
return data["data"]
async def _get_chapters(
self, ctx: commands.Context, bible_id: str, book_id: str
) -> dict:
"""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"
headers = await self.bot.get_shared_api_tokens("api.bible")
@ -118,9 +156,27 @@ class Bible(commands.Cog):
raise bible.errors.BibleAccessError()
if response.status == 503:
raise bible.errors.ServiceUnavailable()
return data["data"]
async def _get_verses(self, bible_id: str, book_id: str, chapter: int) -> dict:
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"]
async def _get_verses(
self, ctx: commands.Context, bible_id: str, book_id: str, chapter: int
) -> dict:
"""Get the verses of a chapter from the API.bible API."""
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/chapters/{book_id}.{chapter}/verses"
headers = await self.bot.get_shared_api_tokens("api.bible")
@ -138,7 +194,23 @@ class Bible(commands.Cog):
raise bible.errors.BibleAccessError()
if response.status == 503:
raise bible.errors.ServiceUnavailable()
return data["data"]
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_verses FUMS executed with a response code of: %s\n%s",
response.status,
json.dumps(fums_data),
)
return data["data"]
@commands.group(autohelp=True)
async def bible(self, ctx: commands.Context):
@ -165,11 +237,11 @@ class Bible(commands.Cog):
if "." not in to_verse:
to_verse = f"{from_verse.split('.')[0]}.{to_verse}"
passage = await self._get_passage(
bible_id, f"{book_id}.{from_verse}-{book_id}.{to_verse}", True
ctx, bible_id, f"{book_id}.{from_verse}-{book_id}.{to_verse}", True
)
else:
passage = await self._get_passage(
bible_id, f"{book_id}.{passage.replace(':', '.')}", False
ctx, bible_id, f"{book_id}.{passage.replace(':', '.')}", False
)
except (
bible.errors.BibleAccessError,
@ -201,16 +273,18 @@ class Bible(commands.Cog):
bible_id = await self.config.bible()
try:
books = await self._get_books(bible_id)
books = await self._get_books(ctx, bible_id)
book = random.choice(books)
chapters = await self._get_chapters(bible_id, book["id"])
chapters = await self._get_chapters(ctx, bible_id, book["id"])
chapter = random.choice(chapters)
verses = await self._get_verses(bible_id, book["id"], chapter["number"])
verses = await self._get_verses(
ctx, bible_id, book["id"], chapter["number"]
)
verse = random.choice(verses)["id"]
passage = await self._get_passage(bible_id, verse, False)
passage = await self._get_passage(ctx, bible_id, verse, False)
except (
bible.errors.BibleAccessError,
bible.errors.NotFound,