Skip to content

Commit

Permalink
fix: add a zotero cache and fix wrong url field
Browse files Browse the repository at this point in the history
resolves #98
  • Loading branch information
sennierer committed May 22, 2024
1 parent 7353001 commit 42fc12b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion viecpro_typesense/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Config:
"bibtex", handler=BibtexShortTitleHandler, options=O(facet=True, optional=True))
kind = StringField("bibtex", handler=BibtexTypeHandler,
options=O(facet=True, optional=True))
tag = StringField("url", handler=ZoteroTagHandler,
tag = StringField("bibs_url", handler=ZoteroTagHandler,
options=O(facet=True, optional=False))
related_doc = RelatedReferenceDocField(
("content_type", "object_id"), options=O(facet=True, optional=True))
Expand Down
10 changes: 8 additions & 2 deletions viecpro_typesense/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"start_date": el.start_date_written.split("<")[0] if el.start_date_written else "",
"end_date": el.end_date_written.split("<")[0] if el.end_date_written else "", }) for el in PersonInstitution.objects.filter(relation_type__name="hatte den Hofstaat")]

zotero_cache = {}


def fixstring(string):
if isinstance(string, str):
Expand Down Expand Up @@ -155,15 +157,19 @@ def func(bibtex):
return bib.get("type", "")

class ZoteroTagHandler(Handler):
def func(zotero_url):
def func(zotero_url):
if zotero_url in zotero_cache:
return zotero_cache[zotero_url]
json = requests.get(
zotero_url, params={"key": os.environ.get("ZOTERO_API_KEY")}
).json()
tags = json["data"].get("tags")
tags = json["data"].get("tags", [])
tag_group = [tag["tag"][2:] for tag in tags if tag["tag"].startswith("1_")]
if len(tag_group) == 1:
zotero_cache[zotero_url] = tag_group[0]
return tag_group[0]
else:
zotero_cache[zotero_url] = "Allgemein"
return "Allgemein"


Expand Down

0 comments on commit 42fc12b

Please sign in to comment.