convert reference to BGW style, use requests library

This commit is contained in:
admin 2026-02-25 13:36:28 -08:00
parent d1e0e1a161
commit 3aedfe2741

25
app.py
View File

@ -3,6 +3,8 @@ import json
from io import BytesIO from io import BytesIO
import zipfile import zipfile
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import requests
app = Flask(__name__) app = Flask(__name__)
@ -11,7 +13,7 @@ def index():
reference = '' reference = ''
version = '' version = ''
verse = '' verse = ''
error = '' error = 'No error.'
if request.method == 'POST': if request.method == 'POST':
reference = request.form.get('scripture_reference') reference = request.form.get('scripture_reference')
@ -19,27 +21,24 @@ def index():
# Map version to ESV Bible ID # Map version to ESV Bible ID
bible_id = { bible_id = {
'NRSV': 'nrs', 'NRSV': 'nrsv',
'NIV': 'niv', 'NIV': 'niv',
'NLT': 'nlt' 'NLT': 'nlt'
}.get(version) }.get(version)
if bible_id and reference: if bible_id and reference:
# ESV API endpoint # ESV API endpoint
api_key = 'YOUR_ESV_API_KEY' # Replace with your actual API key url = f'https://api.esv.org/v2/verse?verse={reference}&bible={bible_id}'
url = f'https://api.esv.org/v2/verse?verse={reference}&bible={bible_id}&key={api_key}' webfriendlyref=reference.replace(' ','%20').replace(':','%3A')
url = f'https://www.biblegateway.com/passage/?search={webfriendlyref}&version={bible_id}'
print(url)
try: try:
response = request.get(url) response = requests.get(url)
response.raise_for_status() #response.raise_for_status()
data = response.json() verse = response.text
if data['verses']:
verse = data['verses'][0]['text']
else:
error = "Verse not found."
except request.RequestException as e: except request.RequestException as e:
error = f"Error fetching verse: {str(e)}" error = f"Error fetching from Bible Gateway: {str(e)}"
else: else:
error = "Please provide both a reference and a version." error = "Please provide both a reference and a version."