-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_item_by_bibtex.py
More file actions
93 lines (77 loc) · 2.93 KB
/
find_item_by_bibtex.py
File metadata and controls
93 lines (77 loc) · 2.93 KB
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
#!/usr/bin/env python3
"""
Find Zotero item by BibTeX key.
This example script demonstrates how to locate a Zotero item in your local library
by searching for its Better BibTeX citation key using the standard pyzotero library.
"""
import os
import sys
try:
from pyzotero import zotero
except ImportError:
print("Please install pyzotero: pip install pyzotero")
sys.exit(1)
def get_local_zotero_client() -> zotero.Zotero:
"""
Get authenticated Zotero client using the local API.
"""
# Use environment variables if available, otherwise assume local user library
library_id = os.getenv("ZOTERO_LIBRARY_ID", "0")
library_type = os.getenv("ZOTERO_LIBRARY_TYPE", "user")
return zotero.Zotero(
library_id=library_id,
library_type=library_type,
api_key=None, # Not needed for local API
local=True, # Force local mode
)
def main():
if len(sys.argv) < 2:
print("Usage: python find_item_by_bibtex.py <bibtex_key>")
print("Example: python find_item_by_bibtex.py Ale22")
sys.exit(1)
target_key = sys.argv[1]
try:
client = get_local_zotero_client()
except Exception as e:
print(f"Failed to connect to local Zotero API: {e}")
print("Make sure Zotero is running and the local API is enabled.")
sys.exit(1)
print(f"Searching for BibTeX key: {target_key}")
# Fetch items in batches (limit=100)
items = client.items(limit=100)
found = False
def check_items(item_list):
for item in item_list:
data = item.get("data", {})
# Check citation key field (where Better BibTeX keys are typically stored)
citation_key = data.get("citationKey", "")
if citation_key == target_key:
print(f"\nFound item with BibTeX key '{target_key}':")
print(f" Item Key: {data.get('key')}")
print(f" Title: {data.get('title', 'No title')}")
authors = ", ".join(
[
f"{c.get('firstName', '')} {c.get('lastName', '')}".strip()
for c in data.get("creators", [])
]
)
print(f" Authors: {authors}")
print(f" Year: {data.get('date', 'Unknown')}")
print(f" Item Type: {data.get('itemType')}")
return True
return False
# Check first batch
if check_items(items):
found = True
if not found:
print("Not found in first 100 items, falling back to searching all items...")
try:
all_items = client.everything(client.items())
if check_items(all_items):
found = True
except Exception as e:
print(f"Error fetching all items: {e}")
if not found:
print(f"\nNo item found with BibTeX key '{target_key}'")
if __name__ == "__main__":
main()