-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsinglefile2trilium-sender.py
85 lines (68 loc) · 2.16 KB
/
singlefile2trilium-sender.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
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
# HowTo:
# 1- Put this file somewhere on your computer (needs python3).
# 2- Change `BASEDIR` and `TRILIUM_URL` vars as needed.
# 3- Make it run at session startup as a daemon
# (https://smallbusiness.chron.com/run-command-startup-linux-27796.html)
import os
import pathlib
import time
import requests
BASEDIR = "~/Downloads"
TRILIUM_URL = "http://127.0.0.1:37840/custom/singlefile2trilium"
path = pathlib.Path(BASEDIR).expanduser().absolute()
assert path.is_dir()
os.chdir(path)
mtime = path.stat().st_mtime
def log(msg):
print(msg)
if msg.startswith("[-] "):
icon = "dialog-error"
else:
icon = "dialog-information"
os.system("notify-send `hostname` %r --icon=%s" % (msg[4:], icon))
while True:
time.sleep(0.3)
new_mtime = path.stat().st_mtime
if new_mtime <= mtime:
continue
for fname in os.listdir():
if not fname.endswith(".html"):
continue
if not os.path.isfile(fname):
continue
if os.stat(fname).st_mtime <= mtime:
continue
with open(fname) as fd:
head = fd.read(4096)
idx = head.find("Page saved with SingleFile")
if idx == -1:
continue
url = None
title = None
for line in head.splitlines():
line = line.strip()
if url is None and line.startswith("url: "):
url = line[5:]
elif line.startswith("title: "):
title = line[7:]
if url is None:
continue
if title is None:
title = fname[:-5] # filename without '.html' suffix
with open(fname) as fd:
content = fd.read()
try:
resp = requests.post(TRILIUM_URL,
json={
"title": title,
"url": url,
"content": content
}
)
# remove file if transfer succeeded
os.unlink(fname)
log("[+] moved %r to trilium" % fname)
except:
log("[-] failed moving %r to trilium" % fname)
mtime = new_mtime