Skip to content

Instantly share code, notes, and snippets.

@pfcbenjamin
Last active January 14, 2018 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pfcbenjamin/423b27d4a56635220be9 to your computer and use it in GitHub Desktop.
Save pfcbenjamin/423b27d4a56635220be9 to your computer and use it in GitHub Desktop.
BibleDraft.py
'''
!/usr/bin/env python
All Scripture generated by this script is from The Holy Bible, English Standard Version. Copyright &copy;2001 by <a href="http://www.crosswaybibles.org">Crossway Bibles</a>, a publishing ministry of Good News Publishers. Used by permission. All rights reserved. Text provided by the <a href="http://www.gnpcb.org/esv/share/services/">Crossway Bibles Web Service</a>.
A lot of this code is from Christian Wyglendowski found at http://www.esvapi.org/api#verse
Adapted for Drafts & Pythonista on iOS by @pfcbenjamin. More info at http://sweetnessoffreedom.wordpress.com/projects
'''
import sys
import console
import webbrowser
import urllib
import urllib2
import re
class ESVSession:
def __init__(self, key):
options = ['include-short-copyright=0',
'output-format=plain-text',
'include-passage-horizontal-lines=0',
'include-heading-horizontal-lines=0',
'include-headings=0',
'include-subheadings=0',
'include-selahs=0',
'line-length=0',
'include-passage-references=0',
'include-footnotes=0']
if destination == 'Twitter':
options.append('include-verse-numbers=0')
options.append('include-first-verse-numbers=0') # need to find a more elegant way to append multiple items yet maintain a string
self.options = '&'.join(options)
self.baseUrl = 'http://www.esvapi.org/v2/rest/passageQuery?key=%s' % (key)
def doPassageQuery(self, passage):
passage = passage.split()
passage = '+'.join(passage)
url = self.baseUrl + '&passage=%s&%s' % (passage, self.options)
page = urllib.urlopen(url)
return page.read()
def PassageLink(self, passage):
clean = re.sub('\s','%20',passage)
link = 'http://esvbible.org/' + clean
return link
def TextFormat(ref, destination):
if destination == 'Twitter':
reflength = len(ref)
textlength = 140-22-6-3-reflength #tweet length - link - 'ESV' - '...'
cleantext = bible.doPassageQuery(ref)
cleantext = re.sub('\n',' ',cleantext) # gets rid of new lines
cleantext = re.sub(' +',' ',cleantext) # gets rid of all the extra spaces
shorttext = cleantext[:textlength] # shorten the passage so it all fits in a tweet
shorttext = shorttext.rsplit(' ',1)[0] # strips out any incomplete words that got chopped earlier
bibletext = ref + ' (ESV)' + shorttext + '...' + bible.PassageLink(ref)
fulltext = re.sub(' +',' ',bibletext) # gets rid of all the extra spaces
if draftsV == 'Drafts4':
url = 'drafts4://x-callback-url/runAction?text=' + urllib.quote(fulltext) + '&action=Tweet' # Tweets verse without creating a new draft
return url
else:
url = 'drafts://x-callback-url/create?text=' + urllib.quote(fulltext) + '&action=Tweet&afterSuccess=Delete'
return url
elif destination == 'Drafts':
link_status = 'OFF' # if you want to wrap your references with a reference link to the ESV Bible online, change this to 'ON'
lines = str.splitlines(ref) # this is a working version to count the number of lines
fulltext = [] # Make list to spit multiple passages into
for ref in lines:
if link_status == 'ON':
bibletext = '[**' + ref + '**][' + re.sub('\D+','',ref) + ']' + bible.doPassageQuery(ref) # This gives you the reference as Markdown-style bold with a link.
else:
bibletext = '**' + ref + '**' + bible.doPassageQuery(ref)
fulltext.append(bibletext)
fulltext = '\n\n'.join(fulltext) # Converts list to string
if link_status == 'ON':
links = []
for ref in lines:
link = '[' + re.sub('\D+','',ref) + ']: ' + bible.PassageLink(ref)
links.append(link)
links = '\n'.join(links)
else:
links = ''
fulltext = fulltext + '\n\n' + links + '\n\nScripture taken from The Holy Bible, English Standard Version. Copyright (C)2001 by [Crossway Bibles](http://www.crosswaybibles.org), a publishing ministry of Good News Publishers. Used by permission. All rights reserved. Text provided by the [Crossway Bibles Web Service](http://www.gnpcb.org/esv/share/services/).'
if draftsV == 'Drafts4':
url = 'drafts4://x-callback-url/create?text=' + urllib.quote(fulltext)
return url
else:
url = 'drafts://x-callback-url/create?text=' + urllib.quote(fulltext)
return url
elif destination == 'Text':
lines = str.splitlines(ref) # this is a working version to count the number of lines
fulltext = [] # Make list to spit multiple passages into
for ref in lines:
bibletext = bible.doPassageQuery(ref)
bibletext = re.sub('\s+',' ',bibletext)
bibletext = ref + ' (ESV)\n' + bibletext
fulltext.append(bibletext)
fulltext = '\n\n'.join(fulltext) # Converts list to string
if draftsV == 'Drafts4':
url = 'drafts4://x-callback-url/runAction?text=' + urllib.quote(fulltext) + '&action=Message'
return url
else:
url = 'drafts://x-callback-url/create?text=' + urllib.quote(fulltext) + '&action=Message'
return url
def DraftsVer():
if webbrowser.can_open('drafts4://') == True: # check to see if Drafts is installed
return 'Drafts4'
elif webbrowser.can_open('drafts://') == True:
return 'Drafts3'
else:
return console.alert('Drafts is Required','You need Drafts for this script. Buy now?','Yes')
if buyoption == 1:
webbrowser.open('https://itunes.apple.com/us/app/drafts-4-quickly-capture-notes/id905337691?mt=8&uo=4&at=11l8rP')
else:
console.hud_alert('I can\'t find any version of Drafts installed.','error')
base = sys.argv[1:]
base = ''.join(base)
#base = urllib.unquote(base)
base = str.splitlines(base) # this should only be necessary for testing
destination = base[0]
key = 'IP' # You can access the ESV text using the key "IP" (without the quotes). This key limits you to 5,000 queries per day from a single IP address. You are bound by the below conditions of use, including the non-commercial aspects.
bible = ESVSession(key)
ref = base[1:]
ref = '\n'.join(ref)
ref = urllib.unquote(ref)
ref = ref.replace(u'\u2013', '-')
alt_line_split = re.search(';',ref)
if alt_line_split:
ref = re.sub(';','\n',ref)
ref = ref.encode() # converts Unicode to String
ref = ref.title() # Convert to title case
draftsV = DraftsVer()
url = TextFormat(ref,destination)
webbrowser.open(url)
@JeffMelton
Copy link

I made a few modifications to your script, many of which are based on personal preference and my different use case. One, though, you might consider implementing:

Instead of Twitter, I use ADN, which has a 256-character limit. I updated textlength accordingly, but then noticed that even when reflength <= textlength, the last word was being truncated. So I added a simple if statement such that the text is only truncated if it's over length. You can see it at my fork.

Thanks for putting this out there!

@gjohnhazel
Copy link

Hey @pfcbenjamin, I admire & appreciate your work! Was wondering if you made any update to this script? Mine no longer works for some reason, haven't had time to troubleshoot yet but I hope to, unless you have already encountered and resolved it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment