Webmaster4o
Feb 06, 2016 - 18:03
There was a recent post about opening page source in Textastic. I don't own Textastic personally, so I wrote a script to open page source in Pythonista:
import appex
import urllib2
from objc_util import *
#Helper functions
def openUrl(url):
'''Allows webbrowser.open()-esque functionality from the app extension'''
app=UIApplication.sharedApplication()
app._openURL_(nsurl(url))
def getDocPath():
'''Gets the path to ~/Documents'''
split=__file__.split('/')
path=split[:split.index('Documents')+1]
return '/'.join(path)+'/'
#Get the url
url=appex.get_url()
#Read page contents
f=urllib2.urlopen(url)
source=f.read()
f.close()
#Detect the type of page we're viewing
test=source.lower().strip()
if '<html>' in test or test.startswith('<!doctype html>'): #Page is HTML
extension='.html'
else: #fallback to .txt
extension='.txt'
#Where to save the source
filename='source'+extension
filepath=getPath()+filename
#Save the source
with open(filepath,'w') as f:
f.write(source)
#Close appex window
appex.finish()
#Open in pythonista
openUrl('pythonista://'+filename)
It's under 50 lines so I can justify not putting it in a Gist for the time being :)