forked from juruen/rmapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zotrm.py
229 lines (200 loc) · 8.43 KB
/
zotrm.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
zotrm.py
Send Zotero papers to ReMarkable tablet.
@authors Samuel Yee, @github/narroo
"""
import os
import configparser
import glob
import re
import argparse
import subprocess
from pyzotero import zotero
def read_config():
'''
Read the configuration file and return a dictionary of parameters.
'''
config = configparser.ConfigParser()
config_file = os.path.expanduser('~/.config/rmapi/zotrm_config.ini')
if not os.path.exists(config_file):
print("Configuration file not found, exiting.")
return -1
config.read(config_file)
d = {}
d['zot_lib_id'] = config['Zotero']['LIBRARY_ID']
d['zot_api_key'] = config['Zotero']['API_KEY']
d['zot_storage_dir'] = os.path.expandvars(config['Zotero']['STORAGE_DIR'])
if 'ATTACHMENT_DIR' in config['Zotero']:
d['zot_attachment_dir'] = os.path.expandvars(config['Zotero']['ATTACHMENT_DIR'])
d['zot_send_tag'] = config['Zotero']['SEND_TAG']
d['zot_replace'] = 'REPLACE_TAG' in config['Zotero']
if d['zot_replace']:
d['zot_replace_tag'] = config['Zotero']['REPLACE_TAG']
d['rmapi_path'] = os.path.expandvars(config['RMAPI']['RMAPI_PATH'])
if 'BASE_DIR' in config['Remarkable']:
d['rm_base_dir'] = config['Remarkable']['BASE_DIR']
d['rm_default_dir'] = config['Remarkable']['DEFAULT_DIR']
return d
def get_attachment(papers, zot, config):
'''
Get the PDF attachment for the given paper.
Args:
-----
paper : list of dict
Item to get attachment for
zot : pyzotero.zotero.Zotero
Zotero library.
Returns:
--------
list of filenames
'''
return attachments
def main(verbose=False):
# Read configuration file
config = read_config()
rmapi_path = config['rmapi_path']
zot = zotero.Zotero(config['zot_lib_id'], 'user', config['zot_api_key'])
# Get list of papers with the appropriate tag
z = zot.top(tag=config['zot_send_tag'])
if verbose:
print("Found {:d} papers to send...".format(len(z)))
# Get list of files in Zotero storage dir
pdflist = glob.glob(os.path.join(config['zot_storage_dir'], '*/*.pdf'))
if pdflist == '':
print("No PDF files found.")
return
for paper in z:
if verbose:
print("Preparing paper {:s}".format(paper['data']['title']))
# Find PDF
attachments = []
# Recursively search for attachments
queue = [paper]
while queue:
item = queue.pop()
if item['data']['itemType'] != 'attachment':
# Note items don't have children to look through
if item['data']['itemType'] != 'note':
queue += zot.children(item['key'])
continue
# Get filename
if 'filename' in item['data']:
filename = item['data']['filename']
elif 'path' in item['data']:
filename = item['data']['path']
else:
# No file found in this attachment
continue
# Make sure file is a PDF
if '.pdf' not in filename.casefold():
continue
# Join base file name
if filename.startswith('attachments:'):
# For linked attachments, we have the whole filename already.
filename = filename[12:]
if 'zot_attachment_dir' not in config:
print("ERR: No attachment directory provided in config file.")
continue
filename = os.path.join(config['zot_attachment_dir'], filename)
if os.path.exists(filename):
attachments.append(filename)
else:
# Without linked attachments, PDF is in some subdirectory.
dirname = re.escape(config['zot_storage_dir'])
regexfilename = re.escape(filename)
regexfilename = os.path.join(dirname, '[A-Z0-9]*/' + regexfilename)
r = re.compile(regexfilename)
# Match files to list
foundfiles = list(filter(r.match, pdflist))
if len(foundfiles) < 1 :
print("ERR: Cannot find file \"{:s}\" in storage directory \"{:s}\", skipping...".format(filename,dirname))
continue
else:
for f in foundfiles:
if os.path.exists(f):
attachments.append(f)
# If no attachments were found, skip the upload
if not attachments:
print("\tNo attachments found, skipping upload")
continue
if verbose:
for f in attachments:
print("\tFound PDF attachment {:s}".format(os.path.basename(f)))
# Get collection(s)
collections = paper['data']['collections']
if len(collections) < 1:
# If not in a collection, use default dir
hierarchy = [config['rm_default_dir']]
else:
hierarchy = []
coll = collections[0]
coll_info = zot.collection(coll)
hierarchy.append(coll_info['data']['name'])
# Get full hierarchy
while coll_info['data']['parentCollection']:
coll = coll_info['data']['parentCollection']
coll_info = zot.collection(coll)
hierarchy.append(coll_info['data']['name'])
if 'rm_base_dir' in config and config['rm_base_dir'] != "/":
hierarchy.append(config['rm_base_dir'])
hierarchy.reverse()
if verbose:
print("\tPlacing attachments in folder {:s}".format('/'.join(hierarchy)))
# Upload to remarkable
dirstr = ""
direxists = True
try:
# Create folders recursively
for folder in hierarchy:
dirstr += "/" + folder
if len(dirstr) < 2:
break
# Check if directory exists if parent existed.
if direxists:
direxists = not subprocess.call([rmapi_path, "find", dirstr],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
# Create directory if it doesn't exist.
if not direxists:
status = subprocess.call([rmapi_path, "mkdir", dirstr],
stdout=subprocess.DEVNULL)
if status != 0:
raise Exception("Could not create directory "
+ dirstr + " on remarkable.")
if verbose:
print("\tCreated directory {:s} on remarkable".format(dirstr))
# Upload PDF
for attachment in attachments:
pdfname = os.path.basename(attachment)
fileexists = not subprocess.call([rmapi_path, "find", dirstr + "/" +
os.path.splitext(pdfname)[0]],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if fileexists:
if verbose:
print("\tFile {:s} already exists, skipping...".format(pdfname))
continue
status = subprocess.call([rmapi_path, "put", attachment, dirstr],
stdout=subprocess.DEVNULL)
if status != 0:
raise Exception("\tCould not upload file " + pdfname +
" to remarkable.")
if verbose:
print("\tUploaded {:s}.".format(pdfname))
except Exception as err:
print(err)
continue
# Remove tag
paper['data']['tags'] = [tag for tag in paper['data']['tags']
if tag['tag'] != config['zot_send_tag']]
if config['zot_replace']:
paper['data']['tags'].append({'tag': config['zot_replace_tag']})
# Update item
zot.update_item(paper)
if verbose:
print("\tUpdated tags.")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Send papers from Zotero to ReMarkable tablet.")
parser.add_argument('--verbose', '-v', action='store_true')
args = parser.parse_args()
main(args.verbose)