Skip to content

Commit

Permalink
Fix social cards
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilwoodruff committed Nov 4, 2023
1 parent 1967a0a commit efaa5c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gcp/social_card_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ def get_image(path: str, query_params: dict, social_cards: dict = {}):
if slug not in post_by_slug:
slug = f"{country.lower()}-{slug}"
post = post_by_slug[slug]
image_folder = Path("./build/static/media")
filename = post["image"].split(".")[0]
if len(list(image_folder.glob(f"{filename}_with_title.*"))) > 0:
filename = f"{filename}_with_title"
# React builds the image filename as 'original_name.hash.extension'. Find it by searching for the original name
image_folder = Path("./build/static/media")

image_files = list(image_folder.glob(f"{filename}.*"))
if len(image_files) > 0:
# In order of preference: file ending with .png, then jpeg, then jpg
for extension in ["_with_title.png", ".png", ".jpeg", ".jpg"]: # Twitter doesn't show the title so we include alternative versions.
for extension in [".png", ".jpeg", ".jpg"]: # Twitter doesn't show the title so we include alternative versions.
for image_file in image_files:
if image_file.name.endswith(extension):
return f"https://policyengine.org/static/media/{image_file.name}"
Expand Down Expand Up @@ -112,6 +115,7 @@ def add_social_card_tags(
title = get_title(path, query_params)
description = get_description(path, query_params)
image_url = get_image(path, query_params, social_cards)
print('Image URL:', image_url)

# Use beautiful soup to add the tags for Twitter and Facebook
soup = BeautifulSoup(html_file, "html.parser")
Expand Down
11 changes: 11 additions & 0 deletions src/posts/posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,16 @@
"tags": ["uk"],
"image": "two_child_limit.png",
"authors": ["nikhil-woodruff"]
},
{
"title": "New King's College London research uses PolicyEngine",
"description":
"A ‘Citizens’ Economic Council’ explores PolicyEngine-scored tax-benefit impacts.",
"date": "2023-11-04 09:00:00",
"filename": "citizens-economic-council.md",
"tags": ["uk", "impact", "featured"],
"image": "citizens-economic-council.png",
"authors": ["nikhil-woodruff"]
}

]

0 comments on commit efaa5c5

Please sign in to comment.