Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active December 21, 2017 17:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dlo/4644960 to your computer and use it in GitHub Desktop.
Save dlo/4644960 to your computer and use it in GitHub Desktop.
Gist.py is a straightforward way to create a Gist on your iOS device using Pythonista and (optionally) Drafts for iOS.

Gist.py

Gist.py is a simple script that makes it super easy to create a Gist on your iOS device by combining the powers of Drafts and Pythonista.

How to install

Copy and paste the code in Gist.py into a Pythonista file called "Gist". If you only use Pythonista and don't want to use Drafts, put in default values for the variables defined by sys.argv. E.g., change these lines:

username = sys.argv[1]
filename = sys.argv[2]
try:
    content = sys.argv[3]
except IndexError:
    content = clipboard.get()
try:
    public = sys.argv[4] == "public"
except:
    public = False

to something like this:

username = "dlo"
filename = "content.txt"
content = clipboard.get()
public = False

If you want to integrate with Drafts, use the following URL scheme with the name "Create Gist":

pythonista://Gist?action=run&argv=YOUR_USERNAME&argv=[[title]]&argv=[[body]]

After installing this, the first line of your Draft will be the filename, and the body will be the file contents.

The first time Gist.py runs, it will prompt you for your GitHub username and password. This is so that it can create an access token. Gist.py saves your access token in your Pythonista keychain after the first successful authentication, so you'll only be prompted once.

After it creates the gist, Gist.py copies the URL to your clipboard so you can do whatever you want with it.

Enjoy!

import clipboard
import console
import requests
import json
import keychain
import webbrowser
from datetime import datetime
argument_index = 1
try:
username = sys.argv[argument_index]
argument_index += 1
except IndexError:
username = "dlo"
default_extension = ".crash"
try:
filename = sys.argv[argument_index]
argument_index += 1
except IndexError:
now = datetime.now()
filename = "{}{}".format(now.strftime("%Y-%m-%d %H:%M:%S"), default_extension)
content = clipboard.get()
public = False
else:
try:
content = sys.argv[argument_index]
argument_index += 1
except IndexError:
content = clipboard.get()
# Gists created are private by default
try:
public = sys.argv[public_index] == "public"
except:
public = False
# Check if the user has already authenticated
access_token = keychain.get_password("GitHub", username)
if access_token is None:
try:
username, password = console.login_alert("GitHub Login")
except KeyboardInterrupt:
pass
else:
data = json.dumps({ "scopes": ["gist"], "note": "Pythonista"})
console.show_activity()
response = requests.post("https://api.github.com/authorizations", data=data, auth=(username, password))
console.hide_activity()
if response.status_code == 201:
access_token = response.json['token']
keychain.set_password("GitHub", username, access_token)
else:
console.alert("Invalid credentials. Exiting.")
sys.exit(0)
data = json.dumps({"public": public, "files": {filename: {'content': content} }})
console.show_activity()
response = requests.post("https://api.github.com/gists?access_token={}".format(access_token), data=data)
console.hide_activity()
if response.status_code == 201:
clipboard.set(response.json['html_url'])
button_id = console.alert("Success", "Gist was successfully created.", "Open in Drafts")
if button_id == 1:
webbrowser.open("drafts://x-callback-url/create?text={}".format(response.json['html_url']))
else:
console.alert("Couldn't create Gist.")
print response.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment