Last active
May 20, 2021 00:26
-
-
Save dunhamsteve/feed462c8cd1a44aa4c8d75b6c06ca0d to your computer and use it in GitHub Desktop.
Copy a shared craft document to pasteboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# pip3 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment