Editorial Workflows

Create Gist

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Creates a gist of the selected file.

Comments: Comment Feed (RSS)

Ryan — 25 Apr 2016
Can this be used to update or sync an existing gist (such as a to-do list)?

+ Add Comment

Workflow Preview
Set Variable ?
Variable Name
title
Value
File Name.File Extension
Request Text Input ?
Title
Enter A Comment For The Gist Or Leave Blank.
Initial Text
  • Single Line
  • Multiple Lines
Keyboard Options:
Set Variable ?
Variable Name
gist_comment
Value
Input
Run Python Script ?
Source Code
#coding: utf-8
import workflow
import keychain
import console
import pickle
import requests
import editor
import json
import webbrowser
import pprint


pp = pprint.PrettyPrinter(indent=4)


# Setting for creating private or public gists. Must be bool. True is public.
gist_view = False 


def formatData(gist_title, gist_body, gist_comment):
    """ Creates dictionary structure for gist api request
        Returns encoded JSON
    """
    payload = {
        'description' : gist_comment,
        'public' : gist_view,
        'files': {
            gist_title : {
                'content' : gist_body
            }
        }
    }
    # JSON encoding data
    payload = json.dumps(payload)
    return payload

def makeGist (api_url, payload):
    """ Makes HTTP request to GitHub API
    Checks for a status code of 201
    If gist was created, returns JSON encoded response
    """
    # Making POST request to GitHub
    r = requests.post(api_url, data=payload)
    # Verifying Response
    if r.status_code != 201:
        print '\n\nERROR: Gist NOT CREATED. ERROR CODE: ' + str(r.status_code)
    return json.loads(r.content)

def main():
    # The keychain bit is ganked from Macdrifters HTML to Evernote Workflow
    login = keychain.get_password('gist', 'editorial')
    if login is not None:
        auth_token = pickle.loads(login)    
    else:
        token_choice = console.alert('Token Needed', 'An Application token is needed. Go get one?', 'Yes', 'I have One', 'Cancel')
        if token_choice == 1:
            webbrowser.open('safari-https://github.com/settings/tokens/new')
            raise KeyboardInterrupt
        elif token_choice == 2:
            auth_token = console.password_alert('GitHub Application Token', 'Paste Your GitHub Application Token')
            pickle_token = pickle.dumps(auth_token)
            keychain.set_password('gist', 'editorial', pickle_token)
        else:
            raise KeyboardInterrupt
		
		# GetHub URL
    api_url = 'https://api.github.com/gists?access_token=' + auth_token


    gist_title = workflow.get_variable('title')
    gist_body = editor.get_text()
    
    gist_comment = workflow.get_variable('gist_comment')

    
    payload = formatData(gist_title, gist_body, gist_comment)
    gist_response = makeGist(api_url, payload)

    # In case anyone wants to see the data returned
    # pp.pprint(gist_response)
    webbrowser.open(gist_response["files"][gist_title]["raw_url"])

if __name__ == '__main__':
    main()