2009-09-08 19:58:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2009 Raoul Snyman #
|
2009-11-30 20:29:26 +00:00
|
|
|
# Portions copyright (c) 2008-2009 Tim Bentley, Jonathan Corwin, Michael #
|
|
|
|
# Gorven, Scott Guerrieri, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
|
|
|
|
# Carsten Tinggaard #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2008-10-30 20:44:54 +00:00
|
|
|
|
2009-09-25 00:43:42 +00:00
|
|
|
import logging
|
2008-10-30 20:44:54 +00:00
|
|
|
|
2009-02-24 20:24:01 +00:00
|
|
|
from common import BibleCommon, SearchResults
|
2008-11-09 20:11:31 +00:00
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
class BGExtract(BibleCommon):
|
2009-06-16 18:21:24 +00:00
|
|
|
global log
|
2009-09-28 17:45:14 +00:00
|
|
|
log = logging.getLogger(u'BibleHTTPMgr(BG_extract)')
|
2009-06-16 18:21:24 +00:00
|
|
|
log.info(u'BG_extract loaded')
|
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
def __init__(self, proxyurl= None):
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'init %s', proxyurl)
|
2008-11-16 07:40:46 +00:00
|
|
|
self.proxyurl = proxyurl
|
2009-06-16 18:21:24 +00:00
|
|
|
|
2009-10-26 16:19:26 +00:00
|
|
|
def get_bible_chapter(self, version, bookname, chapter) :
|
2008-11-09 20:11:31 +00:00
|
|
|
"""
|
|
|
|
Access and decode bibles via the BibleGateway website
|
2009-09-21 17:56:36 +00:00
|
|
|
|
|
|
|
``Version``
|
|
|
|
The version of the bible like 31 for New International version
|
|
|
|
|
|
|
|
``bookname``
|
2009-10-26 16:19:26 +00:00
|
|
|
Name of the Book
|
2009-09-21 17:56:36 +00:00
|
|
|
|
|
|
|
``chapter``
|
|
|
|
Chapter number
|
2008-11-09 20:11:31 +00:00
|
|
|
"""
|
2009-10-26 16:19:26 +00:00
|
|
|
log.debug(u'get_bible_chapter %s,%s,%s',
|
|
|
|
version, bookname, chapter)
|
|
|
|
urlstring = \
|
2009-10-28 20:57:14 +00:00
|
|
|
u'http://www.biblegateway.com/passage/?search=%s+%d&version=%s' % \
|
|
|
|
(bookname, chapter, version)
|
|
|
|
log.debug(u'BibleGateway urm = %s' % urlstring)
|
2008-12-16 17:29:39 +00:00
|
|
|
xml_string = self._get_web_text(urlstring, self.proxyurl)
|
2009-10-27 16:55:09 +00:00
|
|
|
verseSearch = u'<sup class=\"versenum'
|
|
|
|
verseFootnote = u'<sup class=\'footnote'
|
2008-11-09 20:11:31 +00:00
|
|
|
verse = 1
|
2009-10-27 07:20:01 +00:00
|
|
|
i = xml_string.find(u'result-text-style-normal') + 26
|
2008-11-09 20:11:31 +00:00
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
2009-10-27 07:20:01 +00:00
|
|
|
versePos = xml_string.find(verseSearch)
|
2008-11-09 20:11:31 +00:00
|
|
|
bible = {}
|
2008-11-13 20:51:37 +00:00
|
|
|
while versePos > -1:
|
2009-09-26 18:22:10 +00:00
|
|
|
# clear out string
|
2009-09-28 17:45:14 +00:00
|
|
|
verseText = u''
|
2009-10-27 07:20:01 +00:00
|
|
|
versePos = xml_string.find(u'</sup>', versePos) + 6
|
|
|
|
i = xml_string.find(verseSearch, versePos + 1)
|
2009-10-27 20:52:30 +00:00
|
|
|
# Not sure if this is needed now
|
2008-11-09 20:11:31 +00:00
|
|
|
if i == -1:
|
2009-10-27 07:20:01 +00:00
|
|
|
i = xml_string.find(u'</div', versePos + 1)
|
|
|
|
j = xml_string.find(u'<strong', versePos + 1)
|
2008-11-09 20:11:31 +00:00
|
|
|
if j > 0 and j < i:
|
|
|
|
i = j
|
2009-06-16 18:21:24 +00:00
|
|
|
verseText = xml_string[versePos + 7 : i ]
|
2009-10-27 20:52:30 +00:00
|
|
|
# store the verse
|
|
|
|
bible[verse] = self._clean_text(verseText)
|
2008-11-13 20:51:37 +00:00
|
|
|
versePos = -1
|
2008-11-09 20:11:31 +00:00
|
|
|
else:
|
2009-10-27 07:20:01 +00:00
|
|
|
verseText = xml_string[versePos: i]
|
2009-10-27 16:55:09 +00:00
|
|
|
start_tag = verseText.find(verseFootnote)
|
|
|
|
while start_tag > -1:
|
|
|
|
end_tag = verseText.find(u'</sup>')
|
|
|
|
verseText = verseText[:start_tag] + verseText[end_tag + 6:len(verseText)]
|
|
|
|
start_tag = verseText.find(verseFootnote)
|
2009-10-27 07:20:01 +00:00
|
|
|
# Chop off verse and start again
|
|
|
|
xml_string = xml_string[i:]
|
2009-10-27 20:52:30 +00:00
|
|
|
#look for the next verse
|
|
|
|
versePos = xml_string.find(verseSearch)
|
|
|
|
# store the verse
|
|
|
|
bible[verse] = self._clean_text(verseText)
|
2008-11-09 20:11:31 +00:00
|
|
|
verse += 1
|
2009-10-27 07:20:01 +00:00
|
|
|
return SearchResults(bookname, chapter, bible)
|
2009-06-16 18:21:24 +00:00
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
class CWExtract(BibleCommon):
|
2009-06-16 18:21:24 +00:00
|
|
|
global log
|
2009-09-28 20:45:04 +00:00
|
|
|
log = logging.getLogger(u'BibleHTTPMgr(CWExtract)')
|
2009-06-16 18:21:24 +00:00
|
|
|
log.info(u'CWExtract loaded')
|
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
def __init__(self, proxyurl=None):
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'init %s', proxyurl)
|
2008-11-16 07:40:46 +00:00
|
|
|
self.proxyurl = proxyurl
|
2009-06-16 18:21:24 +00:00
|
|
|
|
2009-10-26 16:19:26 +00:00
|
|
|
def get_bible_chapter(self, version, bookname, chapter) :
|
|
|
|
log.debug(u'getBibleChapter %s,%s,%s',
|
|
|
|
version,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
|
2009-09-21 17:56:36 +00:00
|
|
|
|
|
|
|
``version``
|
|
|
|
The version of the bible like niv for New International Version
|
|
|
|
|
|
|
|
``bookname``
|
|
|
|
Text name of in english e.g. 'gen' for Genesis
|
|
|
|
|
|
|
|
``chapter``
|
|
|
|
Chapter number
|
2009-06-16 18:21:24 +00:00
|
|
|
"""
|
2009-10-27 07:20:01 +00:00
|
|
|
log.debug(u'get_bible_chapter %s,%s,%s',
|
|
|
|
version, bookname, chapter)
|
2009-08-02 13:44:41 +00:00
|
|
|
bookname = bookname.replace(u' ', u'')
|
2009-09-26 18:22:10 +00:00
|
|
|
urlstring = u'http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word=%s+%d&version=%s'\
|
|
|
|
% (bookname, chapter, version)
|
2008-12-16 17:29:39 +00:00
|
|
|
xml_string = self._get_web_text(urlstring, self.proxyurl)
|
2009-02-21 20:40:38 +00:00
|
|
|
## Strip Book Title from Heading to return it to system
|
|
|
|
##
|
2009-09-28 17:45:14 +00:00
|
|
|
i = xml_string.find(u'<title>')
|
|
|
|
j = xml_string.find(u'-', i)
|
2009-02-21 20:40:38 +00:00
|
|
|
book_title = xml_string[i + 7:j]
|
|
|
|
book_title = book_title.rstrip()
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'Book Title %s', book_title)
|
|
|
|
i = book_title.rfind(u' ')
|
2009-02-21 20:40:38 +00:00
|
|
|
book_chapter = book_title[i+1:len(book_title)].rstrip()
|
|
|
|
book_title = book_title[:i].rstrip()
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'Book Title %s', book_title)
|
|
|
|
log.debug(u'Book Chapter %s', book_chapter)
|
2009-09-26 18:22:10 +00:00
|
|
|
# Strip Verse Data from Page and build an array
|
2009-02-21 20:40:38 +00:00
|
|
|
|
2009-09-28 17:45:14 +00:00
|
|
|
i = xml_string.find(u'NavCurrentChapter')
|
2008-10-30 20:44:54 +00:00
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
2009-09-28 17:45:14 +00:00
|
|
|
i = xml_string.find(u'<TABLE')
|
2008-10-30 20:44:54 +00:00
|
|
|
xml_string = xml_string[i:len(xml_string)]
|
2009-09-28 17:45:14 +00:00
|
|
|
i = xml_string.find(u'<B>')
|
2009-06-20 19:11:17 +00:00
|
|
|
#remove the <B> at the front
|
|
|
|
xml_string = xml_string[i + 3 :len(xml_string)]
|
|
|
|
# Remove the heading for the book
|
2009-09-28 17:45:14 +00:00
|
|
|
i = xml_string.find(u'<B>')
|
2009-06-20 19:11:17 +00:00
|
|
|
#remove the <B> at the front
|
|
|
|
xml_string = xml_string[i + 3 :len(xml_string)]
|
2009-06-16 18:21:24 +00:00
|
|
|
versePos = xml_string.find(u'<BLOCKQUOTE>')
|
2008-10-30 20:44:54 +00:00
|
|
|
bible = {}
|
|
|
|
while versePos > 0:
|
2009-06-20 19:11:17 +00:00
|
|
|
verseText = u''
|
2009-03-16 17:33:51 +00:00
|
|
|
versePos = xml_string.find(u'<B><I>', versePos) + 6
|
2009-06-16 18:21:24 +00:00
|
|
|
i = xml_string.find(u'</I></B>', versePos)
|
2009-09-26 18:22:10 +00:00
|
|
|
# Got the Chapter
|
2009-09-28 17:45:14 +00:00
|
|
|
verse = xml_string[versePos:i]
|
2009-06-20 19:11:17 +00:00
|
|
|
# move the starting position to begining of the text
|
|
|
|
versePos = i + 8
|
2009-07-12 15:11:57 +00:00
|
|
|
# find the start of the next verse
|
2009-06-20 19:11:17 +00:00
|
|
|
i = xml_string.find(u'<B><I>', versePos)
|
2008-10-30 20:44:54 +00:00
|
|
|
if i == -1:
|
2009-03-16 17:33:51 +00:00
|
|
|
i = xml_string.find(u'</BLOCKQUOTE>',versePos)
|
2008-10-30 20:44:54 +00:00
|
|
|
verseText = xml_string[versePos: i]
|
|
|
|
versePos = 0
|
|
|
|
else:
|
|
|
|
verseText = xml_string[versePos: i]
|
|
|
|
versePos = i
|
2008-12-16 17:29:39 +00:00
|
|
|
bible[verse] = self._clean_text(verseText)
|
2009-02-24 20:24:01 +00:00
|
|
|
return SearchResults(book_title, book_chapter, bible)
|
2009-06-16 18:21:24 +00:00
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
class BibleHTTPImpl():
|
2009-06-16 18:21:24 +00:00
|
|
|
global log
|
2009-09-28 20:45:04 +00:00
|
|
|
log = logging.getLogger(u'BibleHTTPMgr')
|
2009-06-16 18:21:24 +00:00
|
|
|
log.info(u'BibleHTTP manager loaded')
|
2008-11-16 07:40:46 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Finds all the bibles defined for the system
|
2009-09-21 17:56:36 +00:00
|
|
|
Creates an Interface Object for each bible containing connection
|
|
|
|
information
|
|
|
|
|
2008-11-16 07:40:46 +00:00
|
|
|
Throws Exception if no Bibles are found.
|
|
|
|
|
|
|
|
Init confirms the bible exists and stores the database path.
|
|
|
|
"""
|
2009-09-28 17:45:14 +00:00
|
|
|
self.biblesource = u''
|
2009-09-25 23:06:54 +00:00
|
|
|
self.proxyurl = None
|
|
|
|
self.bibleid = None
|
2009-06-16 18:21:24 +00:00
|
|
|
|
2009-09-25 23:06:54 +00:00
|
|
|
def set_proxy(self, proxyurl):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
|
|
|
Set the Proxy Url
|
|
|
|
"""
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'set_proxy %s', proxyurl)
|
|
|
|
self.proxyurl = proxyurl
|
|
|
|
|
2009-09-25 23:06:54 +00:00
|
|
|
def set_bibleid(self, bibleid):
|
2009-02-15 19:11:55 +00:00
|
|
|
"""
|
|
|
|
Set the bible id.
|
|
|
|
The shore identifier of the the bible.
|
|
|
|
"""
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'set_bibleid %s', bibleid)
|
|
|
|
self.bibleid = bibleid
|
|
|
|
|
2009-09-25 23:06:54 +00:00
|
|
|
def set_bible_source(self, biblesource):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
2009-02-02 19:54:38 +00:00
|
|
|
Set the source of where the bible text is coming from
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'set_bible_source %s', biblesource)
|
2008-11-16 07:40:46 +00:00
|
|
|
self.biblesource = biblesource
|
|
|
|
|
2009-10-27 07:20:01 +00:00
|
|
|
def get_bible_chapter(self, version, bookname, chapter):
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
2009-02-02 19:54:38 +00:00
|
|
|
Receive the request and call the relevant handler methods
|
2008-11-16 07:40:46 +00:00
|
|
|
"""
|
2009-10-27 07:20:01 +00:00
|
|
|
log.debug(u'get_bible_chapter %s,%s,%s',
|
|
|
|
version, bookname, chapter)
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'biblesource = %s', self.biblesource)
|
2008-11-16 07:40:46 +00:00
|
|
|
try:
|
2009-03-16 17:33:51 +00:00
|
|
|
if self.biblesource.lower() == u'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)
|
2009-10-27 07:20:01 +00:00
|
|
|
return ev.get_bible_chapter(self.bibleid, bookname, chapter)
|
2009-09-25 23:06:54 +00:00
|
|
|
except:
|
|
|
|
log.exception("Failed to get bible chapter")
|