-
Notifications
You must be signed in to change notification settings - Fork 0
/
xurl.py
executable file
·54 lines (42 loc) · 1.56 KB
/
xurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
from urllib.parse import urlencode, quote, urlunsplit
import subprocess
import argparse
def build_url(app: str, action: str, query: dict):
"""<scheme>://<netloc>/<path>?<query>#<fragment>
Examples:
- craftdocs://open?spaceId=<spaceId>&blockId=<blockId>
- craftdocs://createdocument?spaceId=<spaceId>&title=<title>&content=<content>&folderId=<folderId>
- omnifocus://x-callback-url/add?name=Pick%20up%20milk¬e=Low%20fat
- def asdfasdf
- drafts://x-callback-url/[actionName]?[queryParameters]
- mvim://open?url=file:///etc/profile&line=20
"""
url = (
app,
"x-callback-url",
action,
urlencode(query, quote_via=quote),
"",
)
url = urlunsplit(url)
if app == "mvim":
url = url.replace("x-callback-url/", "")
return url
def main():
parser = argparse.ArgumentParser(description="xurl: x-callback-builder")
parser.add_argument("app", type=str, help='app name (e.g. drafts, omnifocus, etc.)')
parser.add_argument("action", type=str, help='app name (e.g. create, add, etc.)')
parser.add_argument(
"query", type=str, nargs="+", help='query values (e.g. "text=hello")'
)
parser.add_argument("-p", help="print but do not open url.", action="store_true")
args = parser.parse_args()
args.query = dict([v.split("=") for v in args.query])
url = build_url(args.app, args.action, args.query)
if args.p:
print(url)
else:
subprocess.run(["open", url])
if __name__ == "__main__":
main()