#!/usr/bin/env python import sys import urllib sys.path.append("/usr/lib/openoffice.org2.0/program") import uno ##################################################################### # ESV Web Services for Linux/OpenOffice.org2 # The ESVSession class below is taken unashamedly and gratefully from # Christian Wyglendowski's python code on the ESV API website: # http://www.gnpcb.org/esv/share/services/api/#python # for using the ESV Bible as a Web Service. It use the 'IP' based key # with its concomitant restrictions. # # To use this python UNO script you need to start openoffice.org2 with # the following options: # # ooffice "-accept=socket,host=localhost,port=2002;urp;" # # Open a text document then run this script in a separate terminal window. # # If port 2002 is not suitable for your installation, then change the # port number in the way ooffice is started *AND* in the code below # in the line which connects to the openoffice.org2 context. # # Peter Gale Sydney,Australia 2006 ##################################################################### class ESVSession: def __init__(self, key): options = ['include-short-copyright=0', 'output-format=plain-text', 'include-passage-horizontal-lines=0', 'line-lenth=0', 'include-heading-horizontal-lines=0'] self.options = '&'.join(options) self.baseUrl = 'http://www.gnpcb.org/esv/share/get/?key=%s' % (key) def doPassageQuery(self, passage): passage = passage.split() passage = '+'.join(passage) url = self.baseUrl + '&passage=%s&action=doPassageQuery&%s' % (passage, self.options) page = urllib.urlopen(url) return page.read() # get the uno component context from the PyUNO runtime localContext = uno.getComponentContext() # create the UnoUrlResolver resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext ) # connect to the running office ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" ) smgr = ctx.ServiceManager # get the central desktop object desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) # access the current writer document model = desktop.getCurrentComponent() # access the document's text property text = model.Text #try: # key = sys.argv[1] #except IndexError: key = 'IP' bible = ESVSession(key) passage = raw_input("Enter Passage or 'quit': ") # create a cursor coincident with the oowriter ViewCursor cursor = model.CurrentController.getViewCursor() while passage != 'quit': text.insertString( cursor, bible.doPassageQuery(passage), 0 ) passage = raw_input('Enter Passage: ') ctx.ServiceManager