From cab7a1572892514038a711bc080bddfa66f5bc5f Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Sun, 20 Nov 2022 08:25:03 +0000 Subject: [PATCH 1/2] implemented hypothesis metadata storage --- src/microcosm/__init__.py | 39 ++++++++++++++++++++++++++++++++++++++- src/microcosm/photo.py | 0 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/microcosm/photo.py diff --git a/src/microcosm/__init__.py b/src/microcosm/__init__.py index 52a1a94..8d2c9b0 100644 --- a/src/microcosm/__init__.py +++ b/src/microcosm/__init__.py @@ -34,6 +34,9 @@ ENTITY_TYPE_PLURAL_MAP = { core_bp = Blueprint("core", __name__) +class InvalidRequestException(Exception): + """Class of exception raised when the server receives an invalid request""" + def create_app(): app = Flask(__name__) @@ -52,6 +55,7 @@ def create_app(): app.register_blueprint(core_bp) app.register_blueprint(webhook_bp) + return app @@ -80,7 +84,8 @@ def authed_endpoint(f): _api_client = None - +class InvalidRequestException(Exception): + """Invalid Request""" def process_photo_url(created_at: datetime, doc: Dict[str, List[str]], suffix: str = ""): @@ -96,6 +101,13 @@ def process_photo_url(created_at: datetime, doc: Dict[str, List[str]], suffix: s for i, photo in enumerate(doc['photo']): + if isinstance(photo, dict): + photo_url = photo['value'] + elif isinstance(photo, str): + photo_url = photo + else: + raise InvalidRequestException() + if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy': # download the photo @@ -235,6 +247,12 @@ def capture_frontmatter_props(doc: Dict[str, Union[str, List[str]]], frontmatter else: frontmatter[key] = doc[key] + if 'hypothesis-link' in doc: + # get the hypothesis data and store it + r = requests.get(doc['hypothesis-link'][0]) + + frontmatter['hypothesis-meta'] = r.json() + if 'category' in doc: if isinstance(doc['category'], list): categories = doc['category'] @@ -311,6 +329,25 @@ def process_multipart_post(): return docstr, frontmatter, file_path +def process_image_alt_texts(doc): + + alts = [] + + if isinstance(doc['photo'], str): + doc['photo'] = [doc['photo']] + + + for i, photo in enumerate(doc['photo']): + + if isinstance(photo, dict): + alts.append(doc['alt']) + else: + alts.append("") + + return alts + + + def process_json_post(): """Process JSON POST submission""" diff --git a/src/microcosm/photo.py b/src/microcosm/photo.py new file mode 100644 index 0000000..e69de29 From db34ff3d37f84037f313aaa4d8e698b4e651a6e3 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Sat, 26 Nov 2022 06:45:29 +0000 Subject: [PATCH 2/2] add annotation type --- src/microcosm/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/microcosm/__init__.py b/src/microcosm/__init__.py index 8d2c9b0..f47a56b 100644 --- a/src/microcosm/__init__.py +++ b/src/microcosm/__init__.py @@ -200,7 +200,10 @@ def detect_entry_type(doc: dict) -> str: """Given a dictionary object from either form or json, detect type of post""" - if ('in-reply-to' in doc) or ('u-in-reply-to' in doc): + if 'hypothesis-link' in doc: + entry_type = "annotation" + + elif ('in-reply-to' in doc) or ('u-in-reply-to' in doc): entry_type = "reply" elif ('bookmark-of' in doc) or ('u-bookmark-of' in doc): @@ -220,6 +223,7 @@ def detect_entry_type(doc: dict) -> str: entry_type = "post" else: entry_type = "note" + return entry_type