-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-uploader.py
118 lines (105 loc) · 2.91 KB
/
file-uploader.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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
# Based on the "Hot Folder" implementation by @JackGruber
# https://github.com/JackGruber/Joplin-Tools
#
# This module enables uploading of a specified file as a note (with tags, etc) into Joplin via the API
import click
import sys
import os
import time
from joplin import joplinapi
@click.command()
@click.option(
"-n",
"--notebook",
"notebook",
default="@Inbox",
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(
"-f",
"--file",
"file",
required=False,
help="""Specify the file for uploading.""",
)
@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(file, notebook, token, url, plain, add_tag, preview):
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(",")
AddFile(file, notebook_id, plain, add_tag, preview)
def AddFile(file, notebook_id, plain, add_tags, preview):
file_path = file
splitpath = os.path.split(file_path)
filename = splitpath[1]
#if file.find(".lock") > 0:
f = open(file + ".lock", 'w')
f.close()
print("Add to Joplin: " + file)
note_id = joplinapi.CreateNoteWithFile(
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")
except:
print("File remove failed: " + file)
if __name__ == "__main__":
Main()