Skip to content

Instantly share code, notes, and snippets.

@dunhamsteve
Last active May 20, 2021 00:26
Show Gist options
  • Save dunhamsteve/feed462c8cd1a44aa4c8d75b6c06ca0d to your computer and use it in GitHub Desktop.
Save dunhamsteve/feed462c8cd1a44aa4c8d75b6c06ca0d to your computer and use it in GitHub Desktop.

Revisions

  1. dunhamsteve revised this gist May 20, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions getcraftdoc
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python
    #!/usr/bin/env python3

    # pip install pyobjc requests
    # pip3 install pyobjc requests

    from AppKit import NSPasteboard
    import json, requests, re, sys
  2. dunhamsteve created this gist May 20, 2021.
    52 changes: 52 additions & 0 deletions getcraftdoc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/usr/bin/env python

    # pip install pyobjc requests

    from AppKit import NSPasteboard
    import json, requests, re, sys

    def rewrite(url):
    m = re.match(r'https://www.craft.do/s/(\w+)',url)
    if not m:
    raise Exception('url not in correct form')
    key = m[1]
    return f'https://www.craft.do/api/share/{key}'

    def get_data(url):
    durl = rewrite(url)
    r = requests.get(durl)
    assert r.ok
    return r.json()

    JATYPE='io.luki.json.array'
    JTYPE='io.luki.json'

    def copy_to_pasteboard(blocks):
    gp = NSPasteboard.generalPasteboard()
    gp.clearContents()
    if isinstance(blocks,list):
    gp.setString_forType_(json.dumps(blocks), JATYPE)
    else:
    gp.setString_forType_(json.dumps(blocks), JTYPE)

    def rethread(data):
    def populate(block):
    block['subBlocks'] = [populate(bdict[id]) for id in block['blocks']]
    del block['blocks']
    return block

    blocks = data['blocks']
    bdict = {b['id']:b for b in blocks}
    root = blocks[0]
    return populate(root)

    if __name__ == '__main__':
    if len(sys.argv) < 2:
    print(f'Usage: {sys.argv[0]} https://www.craft.do/s/hkJWMhJiXyqA2J')
    sys.exit()
    url = sys.argv[1]
    print(url)
    data = get_data(url)
    root = rethread(data)
    copy_to_pasteboard(root)
    print("Document copied to pasteboard")