Skip to content

Instantly share code, notes, and snippets.

Last active Jan 29, 2018
What would you like to do?
Pythonista Forum Post Copy Code
  • Copy script into the Pythonista
  • Go to Settings - Share Extension Shortcuts
  • Tap on + button and add there
  • Open Safari, visit forum, find a topic & post with some code
  • Make sure that URL ends with /NUMBER, like /5, /6, ... (it's a post number)
  • Tap on action button in Safari
  • Tap on Run Pythonista Script
  • Tap on shortcut you did previously add
  • All code elements are copied to the clipboard

Sample URL:

Clipboard content:

# <code> element no 1

import notification

Simple example to show setting a notification in a loop each minute
for x mins.

In my real implementation I am using Arrow.span.
For example, I would like to set notifications for:
    from (a local time) every 1 hour with a message
    from (a local time) every 3 hours with a message
    from (a local time) every 24 hours with a message
    etc... With a repeat/duration I set.

I can set this up and do it using arrow correctly. But the behaviour of the
notifications module makes it unsuable for this senerio.

num_mins = 5
delays = [60 * (i + 1) for i in range(num_mins)]

for i, d in enumerate(delays):
    x = notification.schedule('Your Game Notification, Mins={}'.format(i + 1),

# A notification so you know this msg group is finished
x = notification.schedule('Game Nofications are over!!!',
                          delay=delays[num_mins - 1] + 10,

# just put this here in testing so you aviod running multiple times
print('Finished Running')
import requests
import urllib3
import json
import re
import clipboard
import appex
CODE_PATTERN = re.compile('\<code.*?\>(.*?)\<\/code\>', re.MULTILINE | re.DOTALL)
def get_code_elements(url):
parsed = urllib3.util.parse_url(url)
if not == '':
raise ValueError(f'{url} does not point to the Pythonista Forum')
path = parsed.path
if not path.startswith('/api/'):
path = f'/api{path}'
url = f'{parsed.scheme}://{}{path}'
j = json.loads(requests.get(url).text)
post = int(url.split('/')[-1])
content = j['posts'][post-1]['content']
return re.findall(CODE_PATTERN, content)
except ValueError as e:
raise ValueError('You have to select specific post from topic, ie. URL should end with /NUMBER') from e
def main():
if appex.is_running_extension():
url = appex.get_url() or appex.get_text()
import console
url = console.input_alert('Enter URL').strip()
if not url:
except KeyboardInterrupt:
print(f'Searching for <code> elements at {url}')
elements = get_code_elements(url)
if not elements:
print(f'No <code> elements found')
result = '\n\n'.join([
f'# <code> element no {idx+1}\n\n' + code
for idx, code in enumerate(elements)
print(f'{len(elements)} <code> element(s) copied to the clipboard')
except Exception as e:
print(f'Failed to get <code> element values: {e}')
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment