-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome-add-to-things.py
executable file
·72 lines (51 loc) · 1.63 KB
/
chrome-add-to-things.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Add Page To Things
# @raycast.mode silent
# @raycast.packageName Google Chrome
#
# Optional parameters:
# @raycast.icon ✅
#
# Documentation:
# @raycast.description Add current page URL and title to Things
# @raycast.author Harvey Rogers
# @raycast.authorURL https://github.com/harveyr
import subprocess
from urllib import parse
def main():
url: str = (
subprocess.check_output(["osascript", "lib/print-chrome-url"])
.decode("utf-8")
.strip()
)
title: str = (
subprocess.check_output(["osascript", "lib/print-chrome-title"])
.decode("utf-8")
.strip()
)
parsed_url = parse.urlparse(url)
trimmed_url = get_trimmed_url(parsed_url)
title += f" - {parsed_url.hostname}"
if url != trimmed_url:
notes = f"- Original: {url}\n- Trimmed: {trimmed_url}"
else:
notes = url
payload = {"title": title, "notes": notes}
things_qs = parse.urlencode(payload, quote_via=parse.quote)
# Docs: https://culturedcode.com/things/support/articles/2803573/
things_url = f"things:///add?{things_qs}"
subprocess.check_call(["open", things_url])
def get_trimmed_url(parsed: parse.ParseResult) -> str:
query_parsed = parse.parse_qs(parsed.query)
trimmed_query = {}
for key, val in query_parsed.items():
if key.startswith("utm_"):
continue
trimmed_query[key] = val
new_qs = parse.urlencode(trimmed_query)
return parsed._replace(fragment="", query=new_qs).geturl()
if __name__ == "__main__":
main()