Handle PDF attachments without parent item
Browse files- functions.py +25 -25
functions.py
CHANGED
@@ -108,28 +108,28 @@ def select_collection(collection, messages):
|
|
108 |
)
|
109 |
|
110 |
|
111 |
-
def search_attachments(id, type, key, collection, queries=[], limit=10):
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
|
134 |
|
135 |
def download_attachment(id, type, key, attachment):
|
@@ -211,7 +211,7 @@ def handle_submit(zot, collection_name, collections, question, messages):
|
|
211 |
attachments = [x for x in {
|
212 |
item['key']: item for search in searches for item in search if item['data']['contentType'] == 'application/pdf'}.values()][:10]
|
213 |
|
214 |
-
parents = set([a['data']['parentItem'] for a in attachments])
|
215 |
if len(attachments) > 0:
|
216 |
messages.append(Message(
|
217 |
Icons.SUCCESS, f"Found {len(attachments)} PDF {'attachments' if len(attachments) > 1 else 'attachment'} from {len(parents)} {'articles' if len(parents) > 1 else 'article'}."))
|
@@ -251,13 +251,13 @@ def handle_submit(zot, collection_name, collections, question, messages):
|
|
251 |
None,
|
252 |
)
|
253 |
for attachment in attachments:
|
254 |
-
parent_id = attachment["data"]["parentItem"]
|
255 |
try:
|
256 |
if parent_id in parents:
|
257 |
citation_dict[attachment["key"]] = parents[parent_id]
|
258 |
else:
|
259 |
parent = zot.item(
|
260 |
-
|
261 |
bib = f"""
|
262 |
{html.fragment_fromstring(parent).xpath("normalize-space(div[2])")}
|
263 |
<a href="{attachment['links']['alternate']['href']}" target="_blank" class="zotero-link">Open in Zotero</a>
|
|
|
108 |
)
|
109 |
|
110 |
|
111 |
+
# def search_attachments(id, type, key, collection, queries=[], limit=10):
|
112 |
+
# try:
|
113 |
+
# zot = zotero.Zotero(int(id), type.lower(), key)
|
114 |
+
# searches = [zot.collection_items(
|
115 |
+
# collection['key'],
|
116 |
+
# q=q,
|
117 |
+
# limit=limit,
|
118 |
+
# itemType='attachment',
|
119 |
+
# qmode='everything'
|
120 |
+
# ) for q in queries]
|
121 |
+
# attachments = [x for x in {item['key']: item for search in searches for item in search if item['data']
|
122 |
+
# ['contentType'] == 'application/pdf'}.values()][:limit]
|
123 |
+
|
124 |
+
# parents = set([a['data']['parentItem'] for a in attachments])
|
125 |
+
|
126 |
+
# message = f"<div>β
Found {len(attachments)} PDF {'attachments' if len(attachments) > 1 else 'attachment'} from {len(parents)} {'articles' if len(parents) > 1 else 'article'}.</div>" if len(
|
127 |
+
# attachments) else "<div>β No results. Make sure to index your PDF attachments in Zotero.</div>"
|
128 |
+
# return parents, attachments, message
|
129 |
+
|
130 |
+
# except Exception as e:
|
131 |
+
# message = f"<div>β οΈ Error occurred when searching in Zotero: {e}</div>"
|
132 |
+
# return [], [], message
|
133 |
|
134 |
|
135 |
def download_attachment(id, type, key, attachment):
|
|
|
211 |
attachments = [x for x in {
|
212 |
item['key']: item for search in searches for item in search if item['data']['contentType'] == 'application/pdf'}.values()][:10]
|
213 |
|
214 |
+
parents = set([a['data']['parentItem'] if 'parentItem' in a['data'] else a['key'] for a in attachments ])
|
215 |
if len(attachments) > 0:
|
216 |
messages.append(Message(
|
217 |
Icons.SUCCESS, f"Found {len(attachments)} PDF {'attachments' if len(attachments) > 1 else 'attachment'} from {len(parents)} {'articles' if len(parents) > 1 else 'article'}."))
|
|
|
251 |
None,
|
252 |
)
|
253 |
for attachment in attachments:
|
254 |
+
parent_id = attachment["data"]["parentItem"] if "parentItem" in attachment["data"] else attachment["key"]
|
255 |
try:
|
256 |
if parent_id in parents:
|
257 |
citation_dict[attachment["key"]] = parents[parent_id]
|
258 |
else:
|
259 |
parent = zot.item(
|
260 |
+
parent_id, content="bib", style="nature")[0]
|
261 |
bib = f"""
|
262 |
{html.fragment_fromstring(parent).xpath("normalize-space(div[2])")}
|
263 |
<a href="{attachment['links']['alternate']['href']}" target="_blank" class="zotero-link">Open in Zotero</a>
|