Permalink
Browse files

Add caching of passage text

  • Loading branch information...
ssaamm committed Nov 26, 2014
1 parent dd2cb96 commit af168e6bfd6e413836f9cf787c4509ec6b3c904b
Showing with 40 additions and 0 deletions.
  1. +26 −0 BibleService.py
  2. +14 −0 tools/db.py
@@ -1,10 +1,36 @@
import requests
import sqlite3

class BibleService(object):
def __init__(self):
self.API_BASE_URL = 'http://www.esvapi.org/v2/rest/passageQuery'

def get_passage_text(self, reference):
cached_text = self.get_cached_text(reference)
if cached_text:
return cached_text
passage_text = self.get_api_text(reference)
self.set_cached_text(reference, passage_text)
return passage_text

def set_cached_text(self, reference, passage_text):
conn = sqlite3.connect('plan.db')
c = conn.cursor()
c.execute('INSERT INTO passage_text (reference,passage) VALUES (?,?)',
(reference, passage_text))
conn.commit()
conn.close()

def get_cached_text(self, reference):
conn = sqlite3.connect('plan.db')
c = conn.cursor()
c.execute('SELECT passage FROM passage_text WHERE reference = ?',
(reference,))
text = c.fetchone()
conn.close()
return text[0]

def get_api_text(self, reference):
payload = {
'key': 'IP',
'passage' : reference,
@@ -8,6 +8,10 @@ def init_db():
day_id INT PRIMARY KEY NOT NULL,
passages TEXT NOT NULL
);''')
c.execute('''CREATE TABLE passage_text (
reference TEXT NOT NULL,
passage TEXT NOT NULL
);''')
conn.commit()
conn.close()

@@ -20,6 +24,15 @@ def dump_plan():
conn.commit()
conn.close()

def dump_passage_text():
conn = sqlite3.connect('../plan.db')
c = conn.cursor()
c.execute('SELECT * FROM passage_text')
for r in c:
print r
conn.commit()
conn.close()

def init_plan():
conn = sqlite3.connect('plan.db')
c = conn.cursor()
@@ -33,3 +46,4 @@ def init_plan():

if __name__ == "__main__":
dump_plan()
dump_passage_text()

0 comments on commit af168e6

Please sign in to comment.