2008-10-30 20:44:54 +00:00
|
|
|
"""
|
|
|
|
OpenLP - Open Source Lyrics Projection
|
|
|
|
Copyright (c) 2008 Raoul Snyman
|
|
|
|
Portions copyright (c) 2008 Martin Thompson, Tim Bentley
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os, os.path
|
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
|
2009-01-20 19:50:37 +00:00
|
|
|
from common import BibleCommon
|
2008-11-09 20:11:31 +00:00
|
|
|
|
2008-11-06 18:49:36 +00:00
|
|
|
import logging
|
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
class BGExtract(BibleCommon):
|
2008-11-06 18:49:36 +00:00
|
|
|
global log
|
2008-12-16 17:29:39 +00:00
|
|
|
log=logging.getLogger("BibleHTTPMgr(BG_extract)")
|
|
|
|
log.info("BG_extract loaded")
|
2008-11-16 07:40:46 +00:00
|
|
|
def __init__(self, proxyurl= None):
|
|
|
|
log.debug("init %s", proxyurl)
|
|
|
|
self.proxyurl = proxyurl
|
2008-12-16 17:29:39 +00:00
|
|
|
def get_bible_chapter(self, version, bookid, bookname, chapter) :
|
2008-11-09 20:11:31 +00:00
|
|
|
"""
|
|
|
|
Access and decode bibles via the BibleGateway website
|
|
|
|
Version - the version of the bible like 31 for New International version
|
|
|
|
bookid - Book id for the book of the bible - eg 1 for Genesis
|
|
|
|
bookname - not used
|
|
|
|
chapter - chapter number
|
|
|
|
|
|
|
|
"""
|
|
|
|
version = 49
|
2008-12-16 17:29:39 +00:00
|
|
|
log.debug( "get_bible_chapter %s,%s,%s,%s", version, bookid, bookname, chapter)
|
2008-11-09 20:11:31 +00:00
|
|
|
urlstring = "http://www.biblegateway.com/passage/?book_id="+str(bookid)+"&chapter"+str(chapter)+"&version="+str(version)
|
2008-12-16 17:29:39 +00:00
|
|
|
xml_string = self._get_web_text(urlstring, self.proxyurl)
|
2008-11-16 07:40:46 +00:00
|
|
|
|
2008-11-09 20:11:31 +00:00
|
|
|
VerseSearch = "class="+'"'+"sup"+'"'+">"
|
|
|
|
verse = 1
|
|
|
|
i= xml_string.find("result-text-style-normal")
|
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
|
|
|
versePos = xml_string.find(VerseSearch)
|
|
|
|
#print versePos
|
|
|
|
bible = {}
|
2008-11-13 20:51:37 +00:00
|
|
|
while versePos > -1:
|
2008-11-09 20:11:31 +00:00
|
|
|
verseText = "" # clear out string
|
|
|
|
versePos = xml_string.find("</span", versePos)
|
|
|
|
i = xml_string.find(VerseSearch, versePos+1)
|
|
|
|
#print i , versePos
|
|
|
|
if i == -1:
|
|
|
|
i = xml_string.find("</div", versePos+1)
|
|
|
|
j = xml_string.find("<strong", versePos+1)
|
|
|
|
#print i , j
|
|
|
|
if j > 0 and j < i:
|
|
|
|
i = j
|
|
|
|
verseText = xml_string[versePos + 7 : i ]
|
|
|
|
#print xml_string
|
2008-11-13 20:51:37 +00:00
|
|
|
#print "VerseText = " + str(verse) +" "+ verseText
|
2008-12-16 17:29:39 +00:00
|
|
|
bible[verse] = self._clean_text(verseText) # store the verse
|
2008-11-13 20:51:37 +00:00
|
|
|
versePos = -1
|
2008-11-09 20:11:31 +00:00
|
|
|
else:
|
|
|
|
i = xml_string[:i].rfind("<span")+1
|
2008-11-13 20:51:37 +00:00
|
|
|
verseText = xml_string[versePos + 7 : i - 1 ] # Loose the </span>
|
|
|
|
xml_string = xml_string[i - 1 :len(xml_string)] # chop off verse 1
|
2008-11-09 20:11:31 +00:00
|
|
|
versePos = xml_string.find(VerseSearch) #look for the next verse
|
2008-12-16 17:29:39 +00:00
|
|
|
bible[verse] = self._clean_text(verseText) # store the verse
|
2008-11-09 20:11:31 +00:00
|
|
|
verse += 1
|
2008-11-16 07:40:46 +00:00
|
|
|
return bible
|
2008-11-09 20:11:31 +00:00
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
class CWExtract(BibleCommon):
|
|
|
|
global log
|
|
|
|
log=logging.getLogger("BibleHTTPMgr(CWExtract)")
|
|
|
|
log.info("CWExtract loaded")
|
|
|
|
def __init__(self, proxyurl=None):
|
|
|
|
log.debug("init %s", proxyurl)
|
|
|
|
self.proxyurl = proxyurl
|
2008-12-16 17:29:39 +00:00
|
|
|
def get_bible_chapter(self, version, bookid, bookname, chapter) :
|
2008-11-16 07:40:46 +00:00
|
|
|
log.debug( "getBibleChapter %s,%s,%s,%s", version, bookid, bookname, chapter)
|
2008-11-09 20:11:31 +00:00
|
|
|
"""
|
2008-12-06 19:34:48 +00:00
|
|
|
Access and decode bibles via the Crosswalk website
|
2008-11-09 20:11:31 +00:00
|
|
|
Version - the version of the bible like niv for New International version
|
|
|
|
bookid - not used
|
|
|
|
bookname - text name of in english eg 'gen' for Genesis
|
|
|
|
chapter - chapter number
|
|
|
|
"""
|
2008-12-16 17:29:39 +00:00
|
|
|
log.debug( "get_bible_chapter %s,%s,%s,%s", version, bookid, bookname, chapter)
|
2008-11-06 18:49:36 +00:00
|
|
|
urlstring = "http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word="+bookname+"+"+str(chapter)+"&version="+version
|
2008-12-16 17:29:39 +00:00
|
|
|
xml_string = self._get_web_text(urlstring, self.proxyurl)
|
2008-10-30 20:44:54 +00:00
|
|
|
|
|
|
|
i= xml_string.find("NavCurrentChapter")
|
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
|
|
|
i= xml_string.find("<TABLE")
|
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
|
|
|
i= xml_string.find("<B>")
|
|
|
|
xml_string = xml_string[i + 3 :len(xml_string)] #remove the <B> at the front
|
|
|
|
i= xml_string.find("<B>") # Remove the heading for the book
|
|
|
|
xml_string = xml_string[i + 3 :len(xml_string)] #remove the <B> at the front
|
|
|
|
versePos = xml_string.find("<BLOCKQUOTE>")
|
2008-11-06 18:49:36 +00:00
|
|
|
#log.debug( versePos)
|
2008-10-30 20:44:54 +00:00
|
|
|
bible = {}
|
|
|
|
while versePos > 0:
|
2008-11-09 20:11:31 +00:00
|
|
|
verseText = "" # clear out string
|
2008-10-30 20:44:54 +00:00
|
|
|
versePos = xml_string.find("<B><I>", versePos) + 6
|
|
|
|
i = xml_string.find("</I></B>", versePos)
|
2008-11-06 18:49:36 +00:00
|
|
|
#log.debug( versePos, i)
|
2008-10-30 20:44:54 +00:00
|
|
|
verse= xml_string[versePos:i] # Got the Chapter
|
|
|
|
#verse = int(temp)
|
2008-11-06 18:49:36 +00:00
|
|
|
#log.debug( "Chapter = " + str(temp))
|
2008-10-30 20:44:54 +00:00
|
|
|
versePos = i + 8 # move the starting position to negining of the text
|
|
|
|
i = xml_string.find("<B><I>", versePos) # fine the start of the next verse
|
|
|
|
if i == -1:
|
|
|
|
i = xml_string.find("</BLOCKQUOTE>",versePos)
|
|
|
|
verseText = xml_string[versePos: i]
|
|
|
|
versePos = 0
|
|
|
|
else:
|
2008-11-06 18:49:36 +00:00
|
|
|
#log.debug( i, versePos)
|
2008-10-30 20:44:54 +00:00
|
|
|
verseText = xml_string[versePos: i]
|
|
|
|
versePos = i
|
2008-12-16 17:29:39 +00:00
|
|
|
bible[verse] = self._clean_text(verseText)
|
2008-11-09 20:11:31 +00:00
|
|
|
#bible[verse] = verseText
|
2008-10-30 20:44:54 +00:00
|
|
|
|
2008-11-06 18:49:36 +00:00
|
|
|
#log.debug( bible)
|
2008-10-30 20:44:54 +00:00
|
|
|
return bible
|
2008-11-16 07:40:46 +00:00
|
|
|
|
|
|
|
class BibleHTTPImpl():
|
|
|
|
global log
|
|
|
|
log=logging.getLogger("BibleHTTPMgr")
|
|
|
|
log.info("BibleHTTP manager loaded")
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Finds all the bibles defined for the system
|
|
|
|
Creates an Interface Object for each bible containing connection information
|
|
|
|
Throws Exception if no Bibles are found.
|
|
|
|
|
|
|
|
Init confirms the bible exists and stores the database path.
|
|
|
|
"""
|
|
|
|
bible = {}
|
|
|
|
biblesource = ""
|
|
|
|
proxyurl = None
|
|
|
|
|
2008-12-16 17:29:39 +00:00
|
|
|
def set_proxy(self,proxyurl):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
|
|
|
Set the Proxy Url
|
|
|
|
"""
|
2008-12-16 17:29:39 +00:00
|
|
|
log.debug("set_proxy %s", proxyurl)
|
2008-11-16 07:40:46 +00:00
|
|
|
self.proxyurl = proxyurl
|
|
|
|
|
2008-12-16 17:29:39 +00:00
|
|
|
def set_bible_source(self,biblesource):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
|
|
|
Set the source of where the bible text is comming from
|
|
|
|
"""
|
2008-12-16 17:29:39 +00:00
|
|
|
log.debug("set_bible_source %s", biblesource)
|
2008-11-16 07:40:46 +00:00
|
|
|
self.biblesource = biblesource
|
|
|
|
|
2008-12-16 17:29:39 +00:00
|
|
|
def get_bible_chapter(self, version, bookid, bookname, chapter):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
|
|
|
Recieve the request and call the relevent handler methods
|
|
|
|
"""
|
2008-12-16 17:29:39 +00:00
|
|
|
log.debug( "get_bible_chapter %s,%s,%s,%s", version, bookid, bookname, chapter)
|
2008-11-16 07:40:46 +00:00
|
|
|
log.debug("biblesource = %s", self.biblesource)
|
|
|
|
try:
|
2009-01-24 08:06:36 +00:00
|
|
|
if self.biblesource.lower() == 'crosswalk':
|
2008-11-16 07:40:46 +00:00
|
|
|
ev = CWExtract(self.proxyurl)
|
|
|
|
else:
|
2008-12-16 17:29:39 +00:00
|
|
|
ev = BGExtract(self.proxyurl)
|
2008-11-16 07:40:46 +00:00
|
|
|
|
2008-12-16 17:29:39 +00:00
|
|
|
return ev.get_bible_chapter(version, bookid, bookname, chapter)
|
2008-11-16 07:40:46 +00:00
|
|
|
except:
|
|
|
|
log.error("Error thrown = %s", sys.exc_info()[1])
|
|
|
|
|
2008-10-30 20:44:54 +00:00
|
|
|
|