2014-03-26

OpenLPを新改訳聖書に対応させるパッチ

OpenLPは教会向けの聖書・歌詞プロジェクションソフトで, Launchpad 上で開発され, 無償で配布されている. BiblegatewayやCrosswalkなどから聖書本文を取得する機能が実装されており, 英語やフランス語, 中国語訳には対応しているが, 日本語訳にはまだ対応していない. そこで, 日本語の新改訳聖書を富山国際大学の高尾先生の検索エンジンより取得するためのパッチを書いてみた.
=== modified file 'openlp/plugins/bibles/forms/bibleimportform.py'
--- openlp/plugins/bibles/forms/bibleimportform.py
+++ openlp/plugins/bibles/forms/bibleimportform.py
@@ -52,8 +52,9 @@
     Crosswalk = 0
     BibleGateway = 1
     Bibleserver = 2
+    TUINSTakao = 3
 
-    Names = ['Crosswalk', 'BibleGateway', 'Bibleserver']
+    Names = ['Crosswalk', 'BibleGateway', 'Bibleserver', 'TUINSTakao']
 
 
 class BibleImportForm(OpenLPWizard):
@@ -212,7 +213,7 @@
         self.webBibleLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.webSourceLabel)
         self.webSourceComboBox = QtGui.QComboBox(self.webBibleTab)
         self.webSourceComboBox.setObjectName('WebSourceComboBox')
-        self.webSourceComboBox.addItems(['', '', ''])
+        self.webSourceComboBox.addItems(['', '', '', ''])
         self.webBibleLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.webSourceComboBox)
         self.webTranslationLabel = QtGui.QLabel(self.webBibleTab)
         self.webTranslationLabel.setObjectName('webTranslationLabel')
@@ -305,6 +306,8 @@
             translate('BiblesPlugin.ImportWizardForm', 'BibleGateway'))
         self.webSourceComboBox.setItemText(WebDownload.Bibleserver,
             translate('BiblesPlugin.ImportWizardForm', 'Bibleserver'))
+        self.webSourceComboBox.setItemText(WebDownload.TUINSTakao,
+            translate('BiblesPlugin.ImportWizardForm', 'TUINSTakao'))
         self.webTranslationLabel.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible:'))
         self.webTabWidget.setTabText(self.webTabWidget.indexOf(self.webBibleTab),
             translate('BiblesPlugin.ImportWizardForm', 'Download Options'))
@@ -484,7 +487,7 @@
 
     def loadWebBibles(self):
         """
-        Load the lists of Crosswalk, BibleGateway and Bibleserver bibles.
+        Load the lists of Crosswalk, BibleGateway, Bibleserver and TUINSTakao bibles.
         """
         # Load Crosswalk Bibles.
         self.loadBibleResource(WebDownload.Crosswalk)
@@ -492,6 +495,8 @@
         self.loadBibleResource(WebDownload.BibleGateway)
         # Load and Bibleserver Bibles.
         self.loadBibleResource(WebDownload.Bibleserver)
+        # Load and TUINSTakao Bibles.
+        self.loadBibleResource(WebDownload.TUINSTakao)
 
     def loadBibleResource(self, download_type):
         """

=== modified file 'openlp/plugins/bibles/lib/http.py'
--- openlp/plugins/bibles/lib/http.py
+++ openlp/plugins/bibles/lib/http.py
@@ -59,7 +59,6 @@
 
 log = logging.getLogger(__name__)
 
-
 class BGExtract(RegistryProperties):
     """
     Extract verses from BibleGateway
@@ -429,6 +428,153 @@
         return books
 
 
+class TTExtract(RegistryProperties):
+    """
+    Extract verses from Dr. Takao webpage on TUINS
+    """
+    _tt_bw = [
+            (u'創世記', 'Gen'),
+            (u'出エジプト記', 'Exod'),
+            (u'レビ記', 'Lev'),
+            (u'民数記', 'Num'),
+            (u'申命記', 'Deut'),
+            (u'ヨシュア記', 'Josh'),
+            (u'士師記', 'Judg'),
+            (u'ルツ記', 'Ruth'),
+            (u'サムエル記第1', '1Sam'),
+            (u'サムエル記第2', '2Sam'),
+            (u'列王記第1', '1Kgs'),
+            (u'列王記第2', '2Kgs'),
+            (u'歴代誌第1', '1Chron'),
+            (u'歴代誌第2', '2Chron'),
+            (u'エズラ記', 'Ezra'),
+            (u'ネヘミヤ記', 'Neh'),
+            (u'エステル記', 'Esther'),
+            (u'ヨブ記', 'Job'),
+            (u'詩篇', 'Ps'),
+            (u'箴言', 'Prov'),
+            (u'伝道者の書', 'Eccles'),
+            (u'雅歌', 'S_of_S'),
+            (u'イザヤ書', 'Isa'),
+            (u'エレミヤ書', 'Jer'),
+            (u'哀歌', 'Lam'),
+            (u'エゼキエル書', 'Ezek'),
+            (u'ダニエル書', 'Dan'),
+            (u'ホセア書', 'Hos'),
+            (u'ヨエル書', 'Joel'),
+            (u'アモス書', 'Amos'),
+            (u'オバデヤ書', 'Obad'),
+            (u'ヨナ書', 'Jonah'),
+            (u'ミカ書', 'Micah'),
+            (u'ナホム書', 'Nahum'),
+            (u'ハバクク書', 'Hab'),
+            (u'ゼパニヤ書', 'Zeph'),
+            (u'ハガイ書', 'Hag'),
+            (u'ゼカリヤ書', 'Zech'),
+            (u'マラキ書', 'Mal'),
+            #
+            (u'マタイの福音書', 'Matt'),
+            (u'マルコの福音書', 'Mark'),
+            (u'ルカの福音書', 'Luke'),
+            (u'ヨハネの福音書', 'John'),
+            (u'使徒の働き', 'Acts'),
+            (u'ローマ人への手紙', 'Rom'),
+            (u'コリント人への手紙第1', '1Cor'),
+            (u'コリント人への手紙第2', '2Cor'),
+            (u'ガラテヤ人への手紙', 'Gal'),
+            (u'エペソ人への手紙', 'Eph'),
+            (u'ピリピ人への手紙', 'Phil'),
+            (u'コロサイ人への手紙', 'Col'),
+            (u'テサロニケ人への手紙第1', '1Thess'),
+            (u'テサロニケ人への手紙第2', '2Thess'),
+            (u'テモテへの手紙第1', '1Tim'),
+            (u'テモテへの手紙第2', '2Tim'),
+            (u'テトスへの手紙', 'Tit'),
+            (u'ピレモンへの手紙', 'Philem'),
+            (u'ヘブル人への手紙', 'Heb'),
+            (u'ヤコブの手紙', 'Jas'),
+            (u'ペテロの手紙第1', '1Pet'),
+            (u'ペテロの手紙第2', '2Pet'),
+            (u'ヨハネの手紙第1', '1John'),
+            (u'ヨハネの手紙第2', '2John'),
+            (u'ヨハネの手紙第3', '3John'),
+            (u'ユダの手紙', 'Jude'),
+            (u'ヨハネの黙示録', 'Rev'),
+            ]
+    _tt_verse_split = re.compile(r'^([a-z0-9_]*) *([0-9]*):? *([0-9]*)')
+    def __init__(self, proxy_url=None):
+        log.debug('TTExtract.init("%s")', proxy_url)
+        self.proxy_url = proxy_url
+        socket.setdefaulttimeout(30)
+        self._tt_b2w = {}
+        for k,v in self._tt_bw:
+            self._tt_b2w[k] = v
+    def get_bible_chapter(self, version, book_name, chapter):
+        """
+        Access and decode bibles via TUINS search cgi
+
+        :param version: The version of the bible like NIV for New International Version
+        :param book_name: Text name of bible book e.g. Genesis, 1. John, 1John or Offenbarung
+        :param chapter: Chapter number
+        """
+        log.debug('TTExtract.get_bible_chapter("%s", "%s", "%s")', version, book_name, chapter)
+        verse = 1
+        no_chapter = False
+        verses = {}
+        while True:
+            book_en = self._tt_b2w[book_name].lower()
+            if no_chapter:
+                word = '%s %d-'%(book_en, verse)
+            else:
+                word = '%s %d:%d-'%(book_en, chapter, verse)
+            word = urllib.parse.quote(word.encode("utf-8"))
+            chapter_url = 'http://www.tuins.ac.jp/~takao/cgi-bin/biblesearch.cgi?key=&word=%s'%word
+            header = ('Accept-Language', 'ja')
+            soup = get_soup_for_bible_ref(chapter_url, header)
+            if not soup:
+                return None
+            self.application.process_events()
+            dl = soup.find('dl')
+            count = 0
+            (dt_book_name, dt_chapter_number, dt_verse_number) = (0, 0, 0)
+            for d in dl:
+                log.debug('TTExtract d="%s"', str(d))
+                if d.name=='dt':
+                    vv = re.split(self._tt_verse_split, d.text)
+                    log.debug('TTExtract vv="%s"', str(vv))
+                    if len(vv[3]):
+                        dt_book_name = vv[1]
+                        dt_chapter_number = int(vv[2])
+                        dt_verse_number = int(vv[3])
+                    else:
+                        dt_book_name = vv[1]
+                        dt_chapter_number = 1
+                        dt_verse_number = int(vv[2])
+                        no_chapter = True
+                    log.debug('TTExtract "%s" %d:%d', dt_book_name, dt_chapter_number, dt_verse_number)
+                elif d.name=='dd':
+                    if dt_book_name.lower() == book_en and dt_chapter_number == chapter:
+                        verses[dt_verse_number] = d.text.strip()
+                        count += 1
+            if count<10:
+                break
+            verse += 10
+        if not verses:
+            log.error('No verses found in the TUINS response.')
+            send_error_message('parse')
+            return None
+        return SearchResults(book_name, chapter, verses)
+
+    def get_books_from_http(self, version):
+        """
+        Return a list of all books
+
+        :param version: The version of the Bible like NIV for New International Version
+        """
+        log.debug('TTExtract.get_books_from_http("%s")', version)
+        return [book for book, data in self._tt_bw]
+
+
 class HTTPBible(BibleDB, RegistryProperties):
     log.info('%s HTTPBible loaded', __name__)
 
@@ -481,6 +627,8 @@
             handler = BGExtract(self.proxy_server)
         elif self.download_source.lower() == 'bibleserver':
             handler = BSExtract(self.proxy_server)
+        elif self.download_source.lower() == 'tuinstakao':
+            handler = TTExtract(self.proxy_server)
         books = handler.get_books_from_http(self.download_name)
         if not books:
             log.error('Importing books from %s - download name: "%s" failed' %
@@ -576,6 +724,8 @@
             handler = BGExtract(self.proxy_server)
         elif self.download_source.lower() == 'bibleserver':
             handler = BSExtract(self.proxy_server)
+        elif self.download_source.lower() == 'tuinstakao':
+            handler = TTExtract(self.proxy_server)
         return handler.get_bible_chapter(self.download_name, book, chapter)
 
     def get_books(self):

bibles_resources.sqlite の変更は, 以下のように行った.
$ sqlite3 openlp/plugins/bibles/resources/bibles_resources.sqlite
insert into download_source values (4,'tuinstakao');
insert into webbibles (name, abbreviation, language_id, download_source_id) values ("新改訳聖書", "ja2003", 57, 4);

0 件のコメント:

コメントを投稿