Sometimes you find the solution right after asking
This works!
Code:
#!/usr/bin/env python
import sys, os
import json
import rson
def readFile(paramFile): ## for the manifest
file = open(paramFile, 'rb')
arrayOfByte = file.read()
file.close()
return(readString(arrayOfByte))
def readString(arrayOfByte): ## for the manifest
return(arrayOfByte.decode("utf-8"))
def readFile2(paramFile):
file = open(paramFile, 'rb')
arrayOfByte = file.read()
file.close()
return(readString2(arrayOfByte))
def readString2(arrayOfByte):
i = 0
byteArray = []
while (i < len(arrayOfByte)):
if (len(arrayOfByte) > i + 1):
j = ((int('0xFF',16) & arrayOfByte[(i + 1)]) >> 5 | (int('0xFF',16) & arrayOfByte[(i + 1)]) << 3)
byteArray.append(j)
byteArray.append(((int('0xFF',16) & arrayOfByte[i]) >> 5 | (int('0xFF',16) & arrayOfByte[i]) << 3))
else:
byteArray.append(((int('0xFF',16) & arrayOfByte[i]) >> 5 | (int('0xFF',16) & arrayOfByte[i]) << 3))
i += 2;
byteArray2 = bytearray((x & 0xFF) for x in byteArray)
return(byteArray2.decode("utf8"))
def loadJson(string):
return rson.loads(string)
def yvesDir2HTML(yvesfile, yves_temp_directory):
# for iOS bibles, everything is stored in a plist, eventually use something like this:
# xmltodict.parse(plistDecode.plistFromString(base64.decodestring(xmltodict.parse(plistDecode.plistFromFile('tv.lifechurch.bible.plist','xml1'))['plist']['dict']['dict'][0]['data'][0]),'xml1'))
bibleMetaData = loadJson(readFile(yvesfile))
yvesDir = os.path.dirname(yvesfile)
bibleName = bibleMetaData['abbreviation'] + ".html"
DEST = open(os.path.join(yves_temp_directory, bibleName), 'w', encoding="utf8")
DEST.write( '<html dir="' + bibleMetaData['language']['text_direction'] + '"><head><title>' )
DEST.write( bibleMetaData['local_title'] )
DEST.write( '</title>\n')
if( ('publisher' in bibleMetaData) and bibleMetaData['publisher'] and ('name' in bibleMetaData['publisher']) and bibleMetaData['publisher']['name'] ):
DEST.write( '<meta name="Publisher" content="' + bibleMetaData['publisher']['name'] + '">\n')
if( ('copyright_long' in bibleMetaData) and ('text' in bibleMetaData) and bibleMetaData['copyright_long']['text'] ):
DEST.write( '<meta name="Copyright" content="' + bibleMetaData['copyright_long']['text'] + '">\n')
if( ('language' in bibleMetaData) and ('iso_639_1' in bibleMetaData['language']) and bibleMetaData['language']['iso_639_1'] ):
DEST.write( '<meta name="DC.language" content="' + bibleMetaData['language']['iso_639_1'] + '">\n')
elif( ('language' in bibleMetaData) and ('iso_639_3' in bibleMetaData['language']) and bibleMetaData['language']['iso_639_3'] ):
DEST.write( '<meta name="DC.language" content="' + bibleMetaData['language']['iso_639_3'] + '">\n')
DEST.write( '<meta name="Source" content="YouVersion">\n')
DEST.write( '<meta charset="utf-8" />\n')
DEST.write( '<style type="text/css">' )
DEST.write( '</style>' )
DEST.write( '</head><body>\n' )
#DEBUG
file = open('out2.txt', 'w', encoding="utf8")
for book in bibleMetaData['books']:
DEST.write('<div class="book">\n<div class="bookTitle">')
DEST.write(book['human_long'])
DEST.write('</div>\n')
for chapter in book['chapters']:
##chapterFile = chapter['usfm'][len(book['usfm'])+1:]
chapterFile = chapter['usfm']
# DEST.write('<a href="')
# DEST.write(book['usfm'])
# DEST.write("/")
# DEST.write(chapterFile)
# DEST.write('.html">')
# DEST.write(book['abbreviation'].encode('utf8'))
# DEST.write(' ')
# DEST.write(chapter['human'])
# DEST.write('</a>')
# DEST.write('\n<br />\n')
print(yvesDir,chapterFile) # DEBUG
bibleData = loadJson(readFile2(os.path.join(yvesDir,chapterFile)))
if( ('content' in bibleData) and bibleData['content'] ):
## COMMENT: Some bibles need JSON parsing first
chapterLines = bibleData['content'].splitlines()
DEST.writelines( chapterLines[2:len(chapterLines)-2] )
else: ## COMMENT: Some bibles have direct HTML content
DEST.write(readFile2(os.path.join(yvesDir,chapterFile)))
DEST.write('</div>\n')
DEST.write( '</body></html>\n' )
DEST.close()
file.close()
return bibleName
if __name__ == '__main__':
if len(sys.argv[1:]) > 0:
for file in sys.argv[1:]:
print(loadJson(readFile(file)))
else:
print(yvesDir2HTML("_version_","."))