Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions catalog/dags/providers/provider_api_scripts/wordpress.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

import logging

import re
import lxml.html as html

from common import constants
Expand Down Expand Up @@ -172,9 +172,12 @@ def _get_title(image):
if title := image.get("content", {}).get("rendered"):
try:
title = html.fromstring(title).text_content()
except UnicodeDecodeError as e:
logger.warning(f"Can't save the image's title ('{title}') due to {e}")
return None
except UnicodeDecodeError:
# lxml's HTML parser can raise UnicodeDecodeError on titles
# containing certain emoji. Fall back to a regex-based tag
# strip, which operates on the original str and never
# re-encodes it, so emoji are preserved correctly.
title = re.sub(r"<[^<]+?>", "", title).strip()
return title

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def test_get_title(ingester):
expected_result = "Coffee Bean with bags"
assert actual_result == expected_result

def test_get_title_handles_emoji(ingester):
image_data = {"content": {"rendered": "<p>Tomato Basil \U0001F33F Soup</p>\n"}}
actual_result = ingester._get_title(image_data)
expected_result = "Tomato Basil \U0001F33F Soup"
assert actual_result == expected_result

def test_get_file_info(ingester):
image_details = (
Expand Down
Loading