Skip to content

Instantly share code, notes, and snippets.

@b00gizm
Created October 7, 2013 11:45
Show Gist options
  • Save b00gizm/6866497 to your computer and use it in GitHub Desktop.
Save b00gizm/6866497 to your computer and use it in GitHub Desktop.
Gist importer script for Pythonista (iOS)
import keychain
import requests
import editor
import sys
import re
GITHUB_USER = 'your_github_username'
GITHUB_TOKEN = keychain.get_password('github', GITHUB_USER)
FOLDER_PREFIX = '' # Usage: 'Path/to/folder/'
if len(sys.argv) < 2:
print 'No Gist URL provided'
sys.exit(1)
pattern = re.compile('https://gist.github.com/[a-zA-Z0-9_]+/([a-f0-9]+)')
match = pattern.search(sys.argv[1])
try:
gist_id = match.group(1)
except IndexError:
print "Could not parse Gist ID from URL '%s'" % sys.argv[1]
sys.exit(1)
gist_url = 'https://api.github.com/gists/' + gist_id
r = requests.get(gist_url, auth=(GITHUB_USER, GITHUB_TOKEN))
if r.status_code is not 200:
print "Something's wrong! API returned status %d" % r.status_code
sys.exit(1)
files = r.json['files']
for key in files.keys():
tup = key.rpartition('.')
name = tup[0]
if name is '':
name = tup[2]
if files[key]['language'] == 'Python':
# Create file
editor.make_new_file(FOLDER_PREFIX + name, files[key]['content'])
print "Imported script '%s'" % name
editor.reload_files()
@b00gizm
Copy link
Author

b00gizm commented Oct 7, 2013

Since there's no Git or Dropbox support in Pythonista (yet), here's a little script for importing Python gists into your Pythonista library. Here's how you do it:

  1. Create a personal access token for your Github account

  2. Launch Pythonista on your iPad or iPad and open the console

  3. Run the following code:

    import keychain
    keychain.set_passwort('github', <your_github_username>, <your_access_token>)
  4. Copy the contents of this gist onto your clipboard and paste it into a new Pythonista script called gist_importer

  5. Change the values for GITHUB_USER and FOLDER_PREFIX to match your account / setup

  6. Install the following bookmarklet for your iOS browser of choice

    javascript:window.location='pythonista://gist_importer?action=run&argv='+encodeURIComponent(document.location.href);
  7. Find yourself a nice Python gist and hit the bookmarklet. Boom.

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