-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbible_com.py
executable file
·147 lines (117 loc) · 5 KB
/
bible_com.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import argparse
import configparser
import webbrowser
import sys
import os
def read_config():
"""Read the config file and return the config object."""
# Get the directory of the current script file
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, 'config.cfg')
# Read the config file
config = configparser.ConfigParser(allow_no_value=True)
config.read(config_path)
# for every langeuage in the config file read the config file for the language
languages = list(config['languages'].keys())
for language in languages:
config_path = os.path.join(script_dir, f'config_{language}.cfg')
config.read(config_path)
# Read the config file for the configured language
config_path = os.path.join(script_dir, f'config_{language}.cfg')
config.read(config_path)
return config
def get_book_name(book):
"""Return the book name from the config file and the given book variable.
Allow the book variable to be similar or incomplete to the book names from the config file."""
# Read the list of books from the config file
config = read_config()
books = config.options('books')
# Check if the given book variable is similar or incomplete to the book names from the config file
for book_name in books:
if book_name.lower().startswith(book.lower()):
return book_name
# If the given book variable is not similar or incomplete to the book names from the config file, return the given book variable
return book
def get_book(book):
"""Return the book shortcut from the config file and the given book variable."""
# Read the list of books from the config file
config = read_config()
book = get_book_name(book)
try:
book_shortcut = config.get('books', book)
except ValueError:
print('Invalid book name.')
print(f'Config: {config}')
exit(1)
except (configparser.NoSectionError, configparser.NoOptionError) as e:
print(f'Error by reading config: {e}')
exit(1)
return book_shortcut
def get_translation(translation):
"""Return the translation id from the config file and the given translation variable."""
# Read the list of translations from the config file
section_name = 'translations'
config = read_config()
if translation == 'None':
translation_id = config.get(section_name, config.options(section_name)[0])
return translation_id
else:
try:
translation_id = config.get(section_name, translation)
except (ValueError, configparser.NoOptionError):
print('Invalid translation name. Using default.')
return 1
return translation_id
def get_translations():
"""Return a list of all translations from the config file."""
# Read the list of translations from the config file
config = read_config()
translations = config.options('translations')
return translations
def create_parser() -> argparse.ArgumentParser:
"""Create an argument parser."""
translation_list = get_translations()
parser = argparse.ArgumentParser(description='Open Bible verses in web browser.',
epilog=f'Available translations: {", ".join(translation_list)}')
# Define the command line arguments
parser.add_argument('book', help='Name of the book - can be upper or lower case, also just the beginning of the book name (e.g. \'heb\' for Hebrews)')
parser.add_argument('chapter', type=int, help='Chapter number')
parser.add_argument('verse', nargs='?', type=int, help='Verse number')
parser.add_argument('-t', '--translation', type=str, help='Translation version')
parser.add_argument('-p', '--parallel', type=str, help='Translation for parallel view')
return parser
def construct_url(book, chapter, verse=None, translation=None, parallel=None):
"""Construct the URL for the given book, chapter, and verse."""
# Get the index of the book in the list of books
book_shortcut = get_book(book)
if not translation:
translation_id = get_translation('None')
else:
translation_id = get_translation(translation)
if parallel:
parallel_translation_id = get_translation(parallel)
# Construct the URL
url = f'https://www.bible.com/bible/{translation_id}/{book_shortcut}.{chapter}'
if verse:
url += f'.{verse}'
if parallel:
url += f'?parallel={parallel_translation_id}'
return url
def open_url(url):
"""Open the given URL in a web browser."""
webbrowser.open(url)
def main():
# Create an argument parser
parser = create_parser()
if len(sys.argv) == 1:
print(parser.format_help())
sys.exit(1)
# Parse the command line arguments
args = parser.parse_args()
# Construct the URL
url = construct_url(args.book, args.chapter, args.verse, args.translation, args.parallel)
# Open the URL in a web browser
open_url(url)
if __name__ == '__main__':
main()