-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rev_Data.py
66 lines (51 loc) · 1.92 KB
/
Rev_Data.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
""" This is the collection of all data that will be stored and used
throughout the program """
import pickle
import os
import sys
import requests
from Bible import Bible
from Timestamp import Timestamp
_APPENDIX_URL = 'https://www.revisedenglishversion.com/jsondload.php?fil=203'
_BIBLE_URL = 'https://www.revisedenglishversion.com/jsondload.php?fil=201'
_COMMENTARY_URL = 'https://www.revisedenglishversion.com/jsondload.php?fil=202'
_FILENAME = 'RevData.dat'
class RevData:
""" Contains the bible, commentary, and appendix together. """
__slots__ = ('__bible', '__timestamp')
__bible: Bible
__timestamp: Timestamp
def __init__(self) -> None:
self.__timestamp = Timestamp()
if self.__timestamp.needs_update:
self.__download_bible()
else:
self.__load_bible()
def __download_bible(self) -> bool:
bibleJson = requests.get(_BIBLE_URL).json()
commentaryJson = requests.get(_COMMENTARY_URL).json()
appendixJson = requests.get(_APPENDIX_URL).json()
self.__bible = Bible(bibleJson, commentaryJson, appendixJson)
if self.__save_bible():
self.__timestamp.save()
return True
return False
def __load_bible(self) -> bool:
try:
with open(_FILENAME, 'rb') as file:
self.__bible = pickle.load(file)
return True
except (EnvironmentError, pickle.UnpicklingError):
return self.__download_bible()
def __save_bible(self) -> bool:
try:
with open(_FILENAME, 'wb') as file:
pickle.dump(self.__bible, file, pickle.HIGHEST_PROTOCOL)
return True
except (EnvironmentError, pickle.PicklingError) as err:
print(f"{0}: export error: {1}".format(
os.path.basename(sys.argv[0]), err))
return False
@property
def bible(self):
return self.__bible