Skip to content

Instantly share code, notes, and snippets.

@zrzka zrzka/README.md
Last active Jan 29, 2018

Embed
What would you like to do?
Pythonista Forum Post Copy Code
  • Copy forum-post-copy-code.py script into the Pythonista
  • Go to Settings - Share Extension Shortcuts
  • Tap on + button and add forum-post-copy-code.py 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),
                              delay=d,
                              sound_name='arcade:Powerup_1',
                              action_url=None)

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

# just put this here in testing so you aviod running multiple times
print('Finished Running')
#!python3
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 parsed.host == 'forum.omz-software.com':
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}://{parsed.host}{path}'
j = json.loads(requests.get(url).text)
try:
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()
else:
import console
try:
url = console.input_alert('Enter URL').strip()
if not url:
return
except KeyboardInterrupt:
return
print(f'Searching for <code> elements at {url}')
try:
elements = get_code_elements(url)
if not elements:
print(f'No <code> elements found')
return
result = '\n\n'.join([
f'# <code> element no {idx+1}\n\n' + code
for idx, code in enumerate(elements)
])
clipboard.set(result)
print(result)
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__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.