diff --git a/viecpro_typesense/collections.py b/viecpro_typesense/collections.py index cb79f08..f6f31c8 100644 --- a/viecpro_typesense/collections.py +++ b/viecpro_typesense/collections.py @@ -1,4 +1,4 @@ -from .handlers import GenericDocIDHandler +from .handlers import GenericDocIDHandler, ZoteroTagHandler from viecpro_typesense import Collection, StaticField, CollectionConfig, O, Field from copy import deepcopy from .fields import StringField, FullNameField, TitlesNestedObjectField, RelationTypeHierarchyHandler, WrittenDateField, BibtexShortTitleHandler, BibtexTitleHandler, BibtexTypeHandler, RelatedReferenceDocField, ObjectIDField, DateObjectDateField, LabelsNestedObjectField, KindField, SourceField, TargetField, HofstaatsinhaberField, MainOwnerField, FunctionsArrayField, PersonInstitutionArrayField @@ -26,6 +26,8 @@ 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, + options=O(facet=True, optional=False)) related_doc = RelatedReferenceDocField( ("content_type", "object_id"), options=O(facet=True, optional=True)) diff --git a/viecpro_typesense/handlers.py b/viecpro_typesense/handlers.py index ad30cfe..7a15ac7 100644 --- a/viecpro_typesense/handlers.py +++ b/viecpro_typesense/handlers.py @@ -1,3 +1,5 @@ +import os +import requests from apis_core.apis_vocabularies.models import VocabsBaseClass from viecpro_typesense.classes import Handler import json @@ -152,6 +154,18 @@ def func(bibtex): bib = json.loads(bibtex) return bib.get("type", "") +class ZoteroTagHandler(Handler): + def func(zotero_url): + json = requests.get( + zotero_url, params={"key": os.environ.get("ZOTERO_API_KEY")} + ).json() + tags = json["data"].get("tags") + tag_group = [tag["tag"][2:] for tag in tags if tag["tag"].startswith("1_")] + if len(tag_group) == 1: + return tag_group[0] + else: + return "Allgemein" + class HofstaatsinhaberHandler(Handler):