Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore invalid jsonld elements on the page source. #189

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
21 changes: 15 additions & 6 deletions extruct/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@ def extract_items(self, document, base_url=None):
if items for item in items if item
]

def _may_be_get_json(self, script):
try:
return json.loads(script, strict=False)
except Exception:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor, but to me it would make more sense to return None here, what do you think? Both None and False can be valid results of JSON parsing, but None looks like a more "neutral" value to use for error case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


def _extract_items(self, node):
script = node.xpath('string()')
try:
# TODO: `strict=False` can be configurable if needed
data = json.loads(script, strict=False)
except ValueError:
# sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not remove this comment, I think it's useful

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the comment back to the code.

data = jstyleson.loads(HTML_OR_JS_COMMENTLINE.sub('', script), strict=False)
data = self._may_be_get_json(script)
# check if valid json.
if not data:
script = jstyleson.dispose( HTML_OR_JS_COMMENTLINE.sub('', script))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick - let's remove a an extra space here

Suggested change
script = jstyleson.dispose( HTML_OR_JS_COMMENTLINE.sub('', script))
script = jstyleson.dispose(HTML_OR_JS_COMMENTLINE.sub('', script))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

data = self._may_be_get_json(script)
# After processing check if json is still valid.
if not data:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick - we don't use return value, as this is a generator, so return would make more sense

Suggested change
return False
return

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


if isinstance(data, list):
for item in data:
yield item
Expand Down
16 changes: 16 additions & 0 deletions tests/samples/custom.invalid/JSONLD_valid_and_invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<script type="application/ld+json">
{ foo: bar}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test! From what I understand, it would fail previously, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it should ignore this jsonld element since it is not valid.

</script>
<!-- only the below tag should be parsed -->
<script type="application/ld+json">
{"foo" : "bar"}
</script>
</head>

<body></body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"foo" : "bar"} ]
6 changes: 6 additions & 0 deletions tests/test_jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def test_songkick(self):
'Elysian Fields Brooklyn Tickets, The Owl Music Parlor, 31 Oct 2015'
)

def test_when_page_has_invalid_jsonld_elements_should_skip(self):
self.assertJsonLdCorrect(
folder='custom.invalid',
page='JSONLD_valid_and_invalid'
)

def test_jsonld_empty_item(self):
self.assertJsonLdCorrect(
folder='songkick',
Expand Down