forked from JackGruber/Joplin-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotfolder.py
154 lines (132 loc) · 4.04 KB
/
hotfolder.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# -*- coding: utf-8 -*-
import click
import sys
import os
import time
from joplin import joplinapi
@click.command()
@click.option(
"-n",
"--notebook",
"notebook",
default="Import",
show_default=True,
help="""Specify the notebook in which to place newly created notes."""
""" Specified notebook must exist or program will exit.""",
)
@click.option(
"-t",
"--token",
"token",
required=False,
help="""Specify the Joplin API token.""",
)
@click.option(
"--tag",
"add_tag",
required=False,
help="""Specify Tags to add to the note. Comma separated for multiple tags.""",
)
@click.option(
"-p",
"--path",
"path",
required=True,
help="""Specify the folder for monitoring.""",
)
@click.option(
"--as-plain",
"plain",
required=False,
help="""Specify additional file extensions comma separated for input as text.""",
)
@click.option(
"-u",
"--url",
"url",
required=False,
default="http://localhost:41184",
show_default=True,
help="""Specify the Joplin web clipper URL.""",
)
@click.option(
"--preview/--no-preview",
"preview",
required=False,
default=False,
show_default=True,
help="""Create a preview of the first site from an PDF file.""",
)
def Main(path, notebook, token, url, plain, add_tag, preview):
if not os.path.exists(path):
print("Path does not exist")
if token is not None:
joplinapi.SetEndpoint(url, token)
elif joplinapi.LoadEndpoint() == False:
joplinapi.SetEndpoint(url, token)
while joplinapi.Ping() == False:
print("Wait for Joplin")
time.sleep(10)
if plain is not None:
plain = plain.replace(", ", ",")
plain = plain.split(",")
notebook_id = joplinapi.GetNotebookID(notebook)
if notebook_id == False:
print("Notebook not found")
sys.exit(1)
if add_tag is not None:
add_tag = add_tag.replace(", ", ",")
add_tag = add_tag.split(",")
WatchFolder(path, notebook_id, plain, add_tag, preview)
def WatchFolder(path, notebook_id, plain, add_tags, preview):
files = dict()
while 1:
while joplinapi.Ping() == False:
print("Wait for Joplin")
time.sleep(10)
# Add files and process
try:
file_list = os.listdir(path)
except:
print("Path not found")
time.sleep(10)
continue
for file in file_list:
file_path = os.path.join(path, file)
if file.find(".lock") > 0:
continue
if not file in files:
files[file] = os.path.getsize(file_path)
elif os.path.getsize(file_path) == files[file] and not os.path.exists(file_path + ".lock"):
f = open(file_path + ".lock", 'w')
f.close()
print("Add to Joplin: " + file)
note_id = joplinapi.CreateNoteWithFile(
os.path.join(path, file), notebook_id, plain, preview)
if note_id != False:
if add_tags is not None:
for tag in add_tags:
joplinapi.AddTagToNote(tag, note_id, True)
print("Joplin upload completed")
try:
os.remove(file_path)
os.remove(file_path + ".lock")
files.pop(file)
except:
print("File remove failed: " + file)
files[file] = -2
else:
files[file] = -1
print("")
elif files[file] >= 0:
files[file] = os.path.getsize(file_path)
# Remove orphan entrys
check = files.copy()
for file in check:
if not os.path.exists(os.path.join(path, file)):
print("Removed orphan: " + file)
files.pop(file)
time.sleep(10)
if __name__ == "__main__":
Main()