├── DraftsGist.py └── ReadMe.md /DraftsGist.py: -------------------------------------------------------------------------------- 1 | # drafts_to_gist.py 2 | # Sean Korzdorfer 3 | # 2013-01-25 4 | # Verson 1 5 | 6 | import requests 7 | import sys 8 | import json 9 | import pprint 10 | import webbrowser 11 | pp = pprint.PrettyPrinter(indent=4) 12 | 13 | # Setting for Github Token 14 | gistToken = '' 15 | 16 | # Setting for creating private or public gists. Must be bool. True is public. 17 | gistView = False 18 | 19 | # GetHub URL 20 | apiURL = 'https://api.github.com/gists?access_token=' + gistToken 21 | 22 | 23 | def formatData(gistTitle, gistBody): 24 | """ Creates dictionary structure for gist api request 25 | Returns encoded JSON 26 | """ 27 | payload = { 28 | 'public' : gistView, 29 | 'files': { 30 | gistTitle : { 31 | 'content' : gistBody 32 | } 33 | } 34 | } 35 | # JSON encoding data 36 | payload = json.dumps(payload) 37 | return payload 38 | 39 | def makeGist (apiURL, payload): 40 | """ Makes HTTP request to GitHub API 41 | Checks for a status code of 201 42 | If gist was created, returns JSON encoded response 43 | """ 44 | # Making POST request to GitHub 45 | r = requests.post(apiURL, data=payload) 46 | # Verifying Response 47 | if r.status_code != 201: 48 | print '\n\nERROR: Gist NOT CREATED. ERROR CODE: ' + str(r.status_code) 49 | return json.loads(r.content) 50 | 51 | def main(): 52 | # Get data passed by Drafts 53 | gistTitle = sys.argv[1] 54 | gistBody = sys.argv[2] 55 | 56 | payload = formatData(gistTitle, gistBody) 57 | gistResponse = makeGist(apiURL, payload) 58 | 59 | # In case anyone wants to see the data returned 60 | # pp.pprint(parsed_json) 61 | webbrowser.open(gistResponse["files"][gistTitle]["raw_url"]) 62 | 63 | if __name__ == '__main__': 64 | main() 65 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | # Drafts App Action: Create Gist 2 | 3 | A [Drafts App][1] URL action combined with a python script for creating quick gists on iOS. 4 | 5 | ## Requirements 6 | 7 | - [Drafts App][1] 8 | - [Pythonista App][2] 9 | - Git Hub OAuth token 10 | 11 | ## Installation 12 | 13 | ### Step 1: Create GitHub OAuth token 14 | 15 | Since this script runs on an iOS app, a GitHub OAuth token must be created prior to use. It's easy to create a token using curl from the terminal. Make sure to **add your GitHub username** to the following command: 16 | 17 | curl https://api.github.com/authorizations \ 18 | --user "YOUR_GITHUB_USERNAME" \ 19 | --data '{"scopes":["gist"],"note":"Drafts"}' 20 | 21 | ### Edit DraftsGist.py 22 | The response will contain a line: `"token" : foo` where foo is the token string. 23 | Add the token to line 14. 24 | 25 | There is also an option for creating public or private gists on line 17. 26 | 27 | You can verify the token is enabled at: 28 | 29 | 30 | ##Step 2: Create a Drafts URL Action 31 | 32 | Paste the following URL into Mobile Safari to a create an URL action in Drafts: 33 | 34 | 35 | drafts://x-callback-url/import_action?type=URL&name=Gist&url=pythonista%3A%2F%2FDraftsGist%3Faction%3Drun%26args%3D%2522%5B%5Btitle%5D%5D%2522%2520%2522%5B%5Bbody%5D%5D%2522 36 | 37 | Or manually create a URL Action: 38 | 39 | pythonista://DraftsGist?action=run&args=%22[[title]]%22%20%22[[body]]%22 40 | 41 | 42 | ## Step 3: Create Pythonista Script 43 | 44 | Paste the contents of Drafts.py into a new Pythonista file. It **must have the title: DraftsGist**. 45 | 46 | ## Using The Drafts Action. 47 | 48 | Easy peasy. Format your note as such: 49 | 50 | This is the first line of the note and the gist's title 51 | Everything else is the content of the note. This is the 1st paragraph. 52 | 53 | Line breaks are preserved. 54 | 55 | This will be the third paragraph of the note. 56 | 57 | Use the Gist action to send the note to Pythonista. Once gist is created, a web browser will open to show the raw contents of the gist. 58 | 59 | 60 | 61 | [1]: http://agiletortoise.com/drafts 62 | [2]: http://omz-software.com/pythonista/ 63 | 64 | --------------------------------------------------------------------------------