feat(bible): starting the process of moving over to free_python_bible_api

This commit is contained in:
Seaswimmer 2024-02-02 01:23:55 -05:00
parent 220e8da790
commit b07ba604b7
Signed by: cswimr
GPG key ID: B8953EC01E5C4063
6 changed files with 33907 additions and 13 deletions

244
bible/api.py Normal file
View file

@ -0,0 +1,244 @@
# All of this code was stolen from the 'free_python_bible_api' repository on GitHub.
# https://github.com/MatthiasELISEE/free_python_bible_api/blob/master/free_bible_api.py
import re
from collections import OrderedDict
import xmltodict
bible = "!failed"
def set_bible(file_name):
global bible
try:
with open(file_name) as fd:
bible = xmltodict.parse(fd.read())
bible = bible["osis"]["osisText"]["div"]
except KeyError:
raise Exception("Your xml OSIS bible is not correctly formatted.")
# only works for french and english without accents
def osisID_from_reference(reference):
reference = reference.replace(" ", "").lower()
parts = re.search("(\d?[a-z]+)(\d+)[:,.](\d+)", reference)
book = parts.group(1)
osis_chapter = parts.group(2)
osis_verse = parts.group(3)
# Esther not at the right place, but is just before Ezra
# James not at the right place, but is just after Luke
if "gen" in book:
osis_book = "Gen"
elif "exo" in book:
osis_book = "Exod"
elif "lev" in book:
osis_book = "Lev"
elif "deut" in book:
osis_book = "Num"
elif "jos" in book:
osis_book = "Josh"
elif "jug" in book or "judg" in book:
osis_book = "Judg"
elif "ruth" in book:
osis_book = "Ruth"
elif "1s" in book:
osis_book = "1Sam"
elif "2s" in book:
osis_book = "2Sam"
elif "1k" in book or "1r" in book:
osis_book = "1Kgs"
elif "2k" in book or "2r" in book:
osis_book = "2Kgs"
elif "1ch" in book:
osis_book = "1Chr"
elif "2ch" in book:
osis_book = "2Chr"
elif book.startswith("est"):
osis_book = "Esth"
elif book.startswith("e"):
osis_book = "Ezra"
elif book.startswith("n"):
osis_book = "Neh"
elif "job" in book:
osis_book = "Job"
elif book.startswith("ps"):
osis_book = "Ps"
elif book.startswith("pr"):
osis_book = "Prov"
elif "ecc" in book:
osis_book = "Eccl"
elif "song" in book or "cant" in book:
osis_book = "Song"
elif "isa" in book or "esa" in book:
osis_book = "Isa"
elif "jer" in book:
osis_book = "Jer"
elif "lam" in book:
osis_book = "Lam"
elif "eze" in book:
osis_book = "Ezek"
elif "dan" in book:
osis_book = "Dan"
elif book.startswith("ho") or book.startswith("os"):
osis_book = "Hos"
elif book.startswith("jo"):
osis_book = "Joel"
elif book.startswith("am"):
osis_book = "Amos"
elif book.startswith("ob"):
osis_book = "Obad"
elif book.startswith("mi"):
osis_book = "Mic"
elif "nah" in book:
osis_book = "Nah"
elif "hab" in book:
osis_book = "Hab"
elif "ph" in book and not book.startswith("ph"):
osis_book = "Zeph"
elif book.startswith("agg") or book.startswith("hag"):
osis_book = "Hag"
elif book.startswith("z"):
osis_book = "Zech"
elif book.startswith("ma"):
osis_book = "Mal"
elif book.startswith("mt") or book.startswith("mat"):
osis_book = "Matt"
elif book.startswith("ma"):
osis_book = "Mark"
elif book.startswith("lu"):
osis_book = "Luke"
elif book.startswith("ja"):
osis_book = "Jas"
elif book.startswith("j"):
osis_book = "John"
elif book.startswith("a"):
osis_book = "Acts"
elif book.startswith("ro") or book.startswith("rm"):
osis_book = "Rom"
elif book.startswith("1c"):
osis_book = "1Cor"
elif book.startswith("2c"):
osis_book = "2Cor"
elif book.startswith("g"):
osis_book = "Gal"
elif book.startswith("e"):
osis_book = "Eph"
elif "pp" in book:
osis_book = "Phil"
elif book.startswith("c"):
osis_book = "Col"
elif book.startswith("1th"):
osis_book = "1Thess"
elif book.startswith("2th"):
osis_book = "2Thess"
elif "1t" in book:
osis_book = "1Tim"
elif "2t" in book:
osis_book = "2Tim"
elif book.startswith("t"):
osis_book = "Titus"
elif book.startswith("p"):
osis_book = "Phlm"
elif book.startswith("h"):
osis_book = "Heb"
elif book.startswith("1p"):
osis_book = "1Pet"
elif book.startswith("2p"):
osis_book = "2Pet"
elif book.startswith("1"):
osis_book = "1John"
elif book.startswith("2"):
osis_book = "2John"
elif book.startswith("3"):
osis_book = "3John"
elif book.startswith("j"):
osis_book = "Jude"
elif book.startswith("r"):
osis_book = "Rev"
return osis_book + "." + osis_chapter + "." + osis_verse
def text_from_osisID(osisID):
global bible
if bible == "!failed":
raise IOError(
"You forgot to set the bible using the set_bible function. Sorry."
)
book_trigger = False
chapter_trigger = False
verse_trigger = False
returned = ""
for book in bible:
if book["@osisID"] == osisID:
book_trigger = True
# Workaround for 1-chapter books
if type(book["chapter"]) == OrderedDict:
chapters = [book["chapter"]]
else:
chapters = book["chapter"]
for chapter in chapters:
# Workaround to solve the problem of the preceding workaround
if type(chapter) != OrderedDict:
chapter = chapter[0]
if chapter["@osisID"] == osisID:
chapter_trigger = True
# But there's no 1-verse chapter lmbo
for verse in chapter["verse"]:
if verse["@osisID"] == osisID:
verse_trigger = True
if verse_trigger or chapter_trigger or book_trigger:
returned += verse["#text"] + "\n"
verse_trigger = False
chapter_trigger = False
book_trigger = False
if returned == "":
return "!failed"
return returned
def text_from_reference(reference):
return reference + " : " + text_from_osisID(osisID_from_reference(reference))
def text_from_references(reference):
reference_list = reference.split(";")
osisID_list = [osisID_from_reference(r) for r in reference_list]
splitted_references = []
for r in reference_list:
rs = r.split("-")
if len(rs) < 2:
continue
elif len(rs) > 2:
raise IOError("There are too much '-'")
else:
end = int(float(rs[1]))
osisID_start = osisID_from_reference(rs[0])
osisID = osisID_start.split(".")
start = int(float(osisID[2]))
del osisID[2]
for i in range(start, end + 1):
splitted_references.append(
"".join([osisID[0]] + [" "] + [osisID[1]] + [":"] + [str(i)])
)
reference_list.remove(r)
reference_list = splitted_references + reference_list
return "".join(text_from_reference(str(s)) for s in reference_list)
# using it with node https://ourcodeworld.com/articles/read/286/how-to-execute-a-python-script-and-retrieve-output-data-and-errors-in-node-js

33642
bible/asv.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,7 @@ import random
import aiohttp import aiohttp
from discord import Embed from discord import Embed
from redbot.core import Config, commands from redbot.core import commands
from redbot.core.bot import Red from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import error from redbot.core.utils.chat_formatting import error
@ -28,12 +28,7 @@ class Bible(commands.Cog):
super().__init__() super().__init__()
self.bot = bot self.bot = bot
self.session = aiohttp.ClientSession() self.session = aiohttp.ClientSession()
self.config = Config.get_conf(
self, identifier=481923957134912, force_registration=True
)
self.logger = logging.getLogger("red.sea.bible") self.logger = logging.getLogger("red.sea.bible")
self.config.register_global(bible="de4e12af7f28f599-02")
self.config.register_user(bible=None)
async def translate_book_name(self, bible_id: str, book_name: str) -> str: async def translate_book_name(self, bible_id: str, book_name: str) -> str:
"""Translate a book name to a book ID.""" """Translate a book name to a book ID."""

View file

@ -1,17 +1,17 @@
{ {
"author" : ["SeaswimmerTheFsh (seasw.)"], "author" : ["SeaswimmerTheFsh (seasw.)"],
"install_msg" : "Thank you for installing Bible!\nThis cog requires setting an API key for API.Bible. Please read the [documentation](https://seacogs.coastalcommits.com/bible/#setup) for more information.\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).", "install_msg" : "Thank you for installing Bible!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).",
"name" : "Bible", "name" : "Bible",
"short" : "Retrieve Bible verses from API.Bible.", "short" : "Retrieve Bible verses.",
"description" : "Retrieve Bible verses from the API.Bible API. This cog requires an API.Bible api key.", "description" : "Retrieve Bible verses from the World English Bible.",
"end_user_data_statement" : "This cog does not store end user data.", "end_user_data_statement" : "This cog does not store end user data.",
"hidden": false, "hidden": false,
"disabled": false, "disabled": false,
"min_bot_version": "3.5.0", "min_bot_version": "3.5.0",
"min_python_version": [3, 10, 0], "min_python_version": [3, 10, 0],
"requirements": ["xmltodict"]
"tags": [ "tags": [
"fun", "fun",
"utility", "utility"
"api"
] ]
} }

16
poetry.lock generated
View file

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. # This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
[[package]] [[package]]
name = "aiodns" name = "aiodns"
@ -1770,6 +1770,7 @@ files = [
{file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"},
{file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"},
{file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"},
@ -2453,6 +2454,17 @@ files = [
{file = "wrapt-1.16.0.tar.gz", hash = "sha256:5f370f952971e7d17c7d1ead40e49f32345a7f7a5373571ef44d800d06b1899d"}, {file = "wrapt-1.16.0.tar.gz", hash = "sha256:5f370f952971e7d17c7d1ead40e49f32345a7f7a5373571ef44d800d06b1899d"},
] ]
[[package]]
name = "xmltodict"
version = "0.13.0"
description = "Makes working with XML feel like you are working with JSON"
optional = false
python-versions = ">=3.4"
files = [
{file = "xmltodict-0.13.0-py2.py3-none-any.whl", hash = "sha256:aa89e8fd76320154a40d19a0df04a4695fb9dc5ba977cbb68ab3e4eb225e7852"},
{file = "xmltodict-0.13.0.tar.gz", hash = "sha256:341595a488e3e01a85a9d8911d8912fd922ede5fecc4dce437eb4b6c8d037e56"},
]
[[package]] [[package]]
name = "yarl" name = "yarl"
version = "1.9.2" version = "1.9.2"
@ -2558,4 +2570,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">=3.9,<3.12" python-versions = ">=3.9,<3.12"
content-hash = "b3a94060974d7ece0d58a3625794c721dd84d34c8215296deeb352b0eb49e29b" content-hash = "1564862475650fc59c9d241c8a4b58b07a49f55108bc7e4ba339d08deea1bfbe"

View file

@ -11,6 +11,7 @@ python = ">=3.9,<3.12"
Red-DiscordBot = "^3.5.5" Red-DiscordBot = "^3.5.5"
pytimeparse2 = "^1.7.1" pytimeparse2 = "^1.7.1"
humanize = "^4.8.0" humanize = "^4.8.0"
xmltodict = "^0.13.0"
[tool.poetry.group.dev] [tool.poetry.group.dev]
optional = true optional = true