Skip to content

Instantly share code, notes, and snippets.

@Aareon
Created May 22, 2021 22:21
Show Gist options
  • Save Aareon/79fa1a7108475ebcd52ca8bc027b4eb5 to your computer and use it in GitHub Desktop.
Save Aareon/79fa1a7108475ebcd52ca8bc027b4eb5 to your computer and use it in GitHub Desktop.
Extension for Pythonista
# import 'pythonista'
# -*- coding: utf-8 -*-
"""Copy WebPage Text
A Pythonista script that works from the sharesheet or the clipboard to copy the text from a webpage to the clipboard. If you have copy a URL and run this script, it will open the URL from your clipboard.
"""
import os
import re
import sys
import appex
import clipboard
import console
import shortcuts
import requests
URL_REGEX = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
SHARE = appex.is_running_extension() # running from the sharesheet
if not SHARE:
import editor
def alert_abort(title, message=None):
console.alert(title, message or '')
def yes_no(title, message=None):
return console.alert(title, message or '', 'Yes', 'No', hide_cancel_button=True) == 1
def validate_url(url):
"""Django URL validation
"""
return re.match(URL_REGEX, url) is not None
def get_url():
url = clipboard.get()
if validate_url(url):
return url
#print(f'clipboard: {url}', url)
if SHARE:
url = appex.get_url()
if url and validate_url(url):
return url
url = appex.get_text()
if url and validate_url(url):
return url
page = appex.get_web_page_info()
if page:
url = page.get('url')
if url is not None and validate_url(url):
return url
def main():
"""Find a URL in the clipboard or sharesheet
"""
url = get_url()
if (
url is None or
not validate_url(url)
):
if yes_no('No URL found in clipboard', 'Want to enter one?'):
url = ''
title = 'Enter URL'
while not validate_url(url):
url = console.input_alert(title, '', url)
title = 'Invalid URL'
console.show_activity()
print(f'GET {url}')
try:
resp = requests.get(url)
except r.execeptions.MissingSchema:
print('Invalid url')
return
console.hide_activity()
if resp.status_code != 200:
console.alert(f'Error {resp.status_code}', 'Could not connect to "{url}"')
clipboard.set(resp.text)
choices = ('Save', 'Save and open')
if SHARE:
choices = ('Save',)
choice = console.alert('Copied!', '', *choices)
if choice:
print('choice:', choice)
default_fn = url.split('/')[-1]
fn = console.input_alert('Enter Filename', '', default_fn)
home_dir = os.path.expanduser('~')
if 'Pythonista' in sys.executable:
home_dir = os.path.join(home_dir, 'Documents') # user files in Pythonista
fp = os.path.join(home_dir, fn)
print(f'Saving to {fp}')
with open(fp, 'w+') as f:
text = resp.text.encode('ascii')
text = text.lstrip().rstrip()
text += '\n'
f.write(resp.text.encode('ascii', 'ignore').decode())
if choice == 1:
console.alert(
'Saved!',
f'"{fp}"', 'Ok', hide_cancel_button=True)
elif choice == 2:
editor.open_file(fp, new_tab=True)
console.hide_output()
print('Done')
shortcuts.pythonista_url(fp, 'open')
if __name__ == '__main__':
if not SHARE and not yes_no('Running outside of appex. Continue?'):
sys.exit(0)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment