open zotero pdf
This commit is contained in:
@@ -56,38 +56,48 @@ class ZoteroResolver:
|
|||||||
|
|
||||||
def _get_item_by_citekey(self, cite_key: str) -> dict | None:
|
def _get_item_by_citekey(self, cite_key: str) -> dict | None:
|
||||||
"""Get Zotero item data by citation key."""
|
"""Get Zotero item data by citation key."""
|
||||||
# Better BibTeX method to search by citekey
|
# Better BibTeX advanced search syntax: [["field", "operator", "value"]]
|
||||||
result = self._rpc_call("item.search", [f"citekey:{cite_key}"])
|
result = self._rpc_call("item.search", [[["citationKey", "is", cite_key]]])
|
||||||
if result and isinstance(result, list) and len(result) > 0:
|
if result and isinstance(result, list) and len(result) > 0:
|
||||||
return result[0]
|
return result[0]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def _get_pdf_link(self, cite_key: str) -> str | None:
|
||||||
|
"""Get zotero://open-pdf link for the first PDF attachment."""
|
||||||
|
attachments = self._rpc_call("item.attachments", [cite_key])
|
||||||
|
if attachments and isinstance(attachments, list):
|
||||||
|
# Find first PDF attachment
|
||||||
|
for att in attachments:
|
||||||
|
if isinstance(att, dict):
|
||||||
|
open_url = att.get("open", "")
|
||||||
|
path = att.get("path", "")
|
||||||
|
# Check if it's a PDF (either by URL or path)
|
||||||
|
if "open-pdf" in open_url or path.lower().endswith(".pdf"):
|
||||||
|
return open_url
|
||||||
|
return None
|
||||||
|
|
||||||
def _extract_authors(self, item: dict) -> str:
|
def _extract_authors(self, item: dict) -> str:
|
||||||
"""Extract author string from Zotero item."""
|
"""Extract author string from CSL-JSON item."""
|
||||||
creators = item.get("creators", [])
|
# item.search returns CSL-JSON format with 'author' field
|
||||||
authors = [c for c in creators if c.get("creatorType") == "author"]
|
authors = item.get("author", [])
|
||||||
|
|
||||||
if not authors:
|
if not authors:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
if len(authors) == 1:
|
if len(authors) == 1:
|
||||||
return authors[0].get("lastName", "")
|
return authors[0].get("family", "")
|
||||||
elif len(authors) == 2:
|
elif len(authors) == 2:
|
||||||
return f"{authors[0].get('lastName', '')} & {authors[1].get('lastName', '')}"
|
return f"{authors[0].get('family', '')} & {authors[1].get('family', '')}"
|
||||||
else:
|
else:
|
||||||
return f"{authors[0].get('lastName', '')} et al."
|
return f"{authors[0].get('family', '')} et al."
|
||||||
|
|
||||||
def _extract_year(self, item: dict) -> str:
|
def _extract_year(self, item: dict) -> str:
|
||||||
"""Extract year from Zotero item."""
|
"""Extract year from CSL-JSON item."""
|
||||||
date = item.get("date", "")
|
# CSL-JSON has 'issued' field with date-parts array
|
||||||
# Try to extract year from date string
|
issued = item.get("issued", {})
|
||||||
if date:
|
date_parts = issued.get("date-parts", [[]])
|
||||||
# Common formats: "2024", "2024-01-15", "January 2024"
|
if date_parts and len(date_parts) > 0 and len(date_parts[0]) > 0:
|
||||||
import re
|
return str(date_parts[0][0])
|
||||||
|
|
||||||
match = re.search(r"(\d{4})", date)
|
|
||||||
if match:
|
|
||||||
return match.group(1)
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def try_resolve(self, cite_key: str) -> CitationInfo | None:
|
def try_resolve(self, cite_key: str) -> CitationInfo | None:
|
||||||
@@ -103,12 +113,13 @@ class ZoteroResolver:
|
|||||||
if item is None:
|
if item is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Build zotero:// select URL
|
# Try to get PDF attachment link, fallback to item selection
|
||||||
item_key = item.get("key", "")
|
pdf_link = self._get_pdf_link(cite_key)
|
||||||
library_id = item.get("libraryID", 1)
|
if pdf_link:
|
||||||
|
zotero_url = pdf_link
|
||||||
# Format: zotero://select/items/@citekey or zotero://select/library/items/ITEMKEY
|
else:
|
||||||
zotero_url = f"zotero://select/items/@{cite_key}"
|
# Fallback: zotero://select/items/@citekey
|
||||||
|
zotero_url = f"zotero://select/items/@{cite_key}"
|
||||||
|
|
||||||
info = CitationInfo(
|
info = CitationInfo(
|
||||||
key=cite_key,
|
key=cite_key,
|
||||||
|
|||||||
Reference in New Issue
Block a user