Fix KeyError in social_card_tags by skipping posts without filename #2781
+5
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a KeyError in gcp/social_card_tags.py that occurred when a post did not have a filename key. Previously, the script crashed if at least one post was missing this field.
The change ensures that posts without filename are safely skipped, preventing the social card tagging process from failing. This resolves issue #2727
Changes
Modified the dictionary comprehension in gcp/social_card_tags.py:
post_by_slug = {
post["filename"].split(".")[0]: post
for post in posts
if "filename" in post
}
Only posts with a filename key are now processed.
No other functionality was modified.
Screenshots
Previously, running python gcp/social_card_tags.py raised KeyError: 'filename'.
After this fix, the script runs successfully without crashing.
Tests
Tested locally in Python virtual environment:
Activated venv: source venv/bin/activate
Installed dependencies: pip install beautifulsoup4 requests pyyaml
Ran script: python gcp/social_card_tags.py
Verified that posts missing filename are skipped.
Verified that posts with valid filename are processed correctly.