<reponame>brettkelly/Bible-kjv<filename>sqlGen.py
#!/usr/bin/python3

import json
import os
import os.path
from string import punctuation

BOOKFILE = './Books.json'
SRCDIR = './kjv-data'
bookData = json.load(open(BOOKFILE))


def getBookByLongName(bookName):
    for b in books:
        if b.longName == bookName:
            return b
    return None


class Book(object):
    def __init__(self, sequence, name):
        self.longName = name
        self.shortName = name.strip(punctuation).replace(" ", "")[:4].upper()
        self.sequence = sequence
        self.verses = []

    def __str__(self):
        return f"{self.sequence} — {self.longName} ({self.shortName})"

    def sqlInsert(self):
        return f"INSERT into books(book_id, book_key_name, book_short_name, book_full_name) VALUES ({self.sequence},'{self.shortName}','{self.longName}','{self.longName}');"

    def addVerse(self, verseObj):
        self.verses.append(verseObj)


class Verse(object):
    def __init__(self, book, chapter, verseNumber, verseText, translation):
        self.book = book  # this is the Book objecfg
        self.chapter = chapter
        self.verseNumber = verseNumber
        self.verseText = verseText
        self.translation = translation

    def __str__(self):
        return f"{self.verseText} — {self.book.longName} {self.chapter}:{self.verseNumber}"

    def sqlInsert(self):
        return f"""INSERT into verses(book_id, translation_short_name, verse_chapter, verse_number, verse_text) VALUES ({self.book.sequence},'{self.translation}',{self.chapter},{self.verseNumber.strip()},\"{self.verseText.strip()}\");"""


books = []
bookNameIdMap = {}

for i in range(len(bookData)):
    b = Book(i+1, bookData[i])
    books.append(b)
    bookNameIdMap[b.longName] = b.sequence

bookFiles = os.listdir(SRCDIR)
for f in bookFiles:
    with open(os.path.join(SRCDIR, f)) as fd:
        try:
            versesFile = json.load(fd)
        except:
            print(f"Error loading {f}; skipping...")
            continue
        bookObj = getBookByLongName(versesFile['book'])
        chapters = versesFile['chapters']
        for chapter in chapters:
            chapterNum = chapter['chapter']
            for v in chapter['verses']:
                verseObj = Verse(bookObj, chapterNum,
                                 v['verse'], v['text'], 'KJV')
                bookObj.addVerse(verseObj)

for b in books:
    print(b.sqlInsert())
for b in books:
    for v in b.verses:
        print(v.sqlInsert())

#!/usr/bin/python3

import json
import os
import os.path
from string import punctuation

BOOKFILE = './Books.json'
SRCDIR = './kjv-data'
bookData = json.load(open(BOOKFILE))


def getBookByLongName(bookName):
    for b in books:
        if b.longName == bookName:
            return b
    return None


class Book(object):
    def __init__(self, sequence, name):
        self.longName = name
        self.shortName = name.strip(punctuation).replace(" ", "")[:4].upper()
        self.sequence = sequence
        self.verses = []

    def __str__(self):
        return f"{self.sequence} — {self.longName} ({self.shortName})"

    def sqlInsert(self):
        return f"INSERT into books(book_id, book_key_name, book_short_name, book_full_name) VALUES ({self.sequence},'{self.shortName}','{self.longName}','{self.longName}');"

    def addVerse(self, verseObj):
        self.verses.append(verseObj)


class Verse(object):
    def __init__(self, book, chapter, verseNumber, verseText, translation):
        self.book = book  # this is the Book objecfg
        self.chapter = chapter
        self.verseNumber = verseNumber
        self.verseText = verseText
        self.translation = translation

    def __str__(self):
        return f"{self.verseText} — {self.book.longName} {self.chapter}:{self.verseNumber}"

    def sqlInsert(self):
        return f"""INSERT into verses(book_id, translation_short_name, verse_chapter, verse_number, verse_text) VALUES ({self.book.sequence},'{self.translation}',{self.chapter},{self.verseNumber.strip()},\"{self.verseText.strip()}\");"""


books = []
bookNameIdMap = {}

for i in range(len(bookData)):
    b = Book(i+1, bookData[i])
    books.append(b)
    bookNameIdMap[b.longName] = b.sequence

bookFiles = os.listdir(SRCDIR)
for f in bookFiles:
    with open(os.path.join(SRCDIR, f)) as fd:
        try:
            versesFile = json.load(fd)
        except:
            print(f"Error loading {f}; skipping...")
            continue
        bookObj = getBookByLongName(versesFile['book'])
        chapters = versesFile['chapters']
        for chapter in chapters:
            chapterNum = chapter['chapter']
            for v in chapter['verses']:
                verseObj = Verse(bookObj, chapterNum,
                                 v['verse'], v['text'], 'KJV')
                bookObj.addVerse(verseObj)

for b in books:
    print(b.sqlInsert())
for b in books:
    for v in b.verses:
        print(v.sqlInsert())
#!/usr/bin/python3 # this is the Book objecfg INSERT into verses(book_id, translation_short_name, verse_chapter, verse_number, verse_text) VALUES ({self.book.sequence},'{self.translation}',{self.chapter},{self.verseNumber.strip()},\"{self.verseText.strip()}\");