forked from covode/bible-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bible
executable file
·49 lines (40 loc) · 1.44 KB
/
bible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
import sys
from bs4 import BeautifulSoup
import argparse
import requests
def versions():
return {'NIV', 'NLT'}
def usage():
print('./bible [passage] [version]')
sys.exit()
def parse_args():
parser = argparse.ArgumentParser(description='Fetch Bible references from biblegateway.com')
parser.add_argument('reference', nargs='+', help='bible reference')
parser.add_argument('--version', nargs='?', help='translation (e.g. NIV, NLT')
parser.add_argument('--hideversenumbers', action='store_true', default=False)
args = parser.parse_args()
return ' '.join(args.reference), args.version, args.hideversenumbers
def get_soup(query, version):
r = requests.get('https://www.biblegateway.com/passage/?search=' + query + '&version=' + version)
return BeautifulSoup(r.text, "html.parser")
def clean(tag):
for t in tag.find_all('sup'):
if not hide_verse_numbers and t['class'] == 'versenum':
continue
t.decompose()
def render(tag):
clean(tag)
text = ''
for t in tag.descendants:
if isinstance(t, str):
text += t
elif t.name == 'br':
text += '\n'
return text
def get_passage(soup):
tags = soup.find(class_="result-text-style-normal").select('p, div.poetry p')
return "\n\n".join(map(lambda t : render(t), tags))
query, version, hide_verse_numbers = parse_args()
soup = get_soup(query, version)
print(get_passage(soup))