Skip to content
Draft
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
2 changes: 1 addition & 1 deletion blogs/cararthompson.com.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"title": "Building Stories with Data",
"subtitle": "Pars pas les mains vides",
"type": "blog",
"url": "https://cararthompson.com/blog",
"url": "https://cararthompson.com/posts",
"rss_feed": "https://www.cararthompson.com/posts.xml",
"photo_url": "https://www.cararthompson.com/images/home/profile-pic.png",
"description": "My website and portfolio, which includes talks and a blog about the things I've picked up along the way in the weird and wonderful worlds of #rstats, datascience and dataviz.",
Expand Down
48 changes: 48 additions & 0 deletions scripts/get_og_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
library(httr2)
library(rvest)
library(jsonlite)

extract_opengraph_info <- function(url) {
# Fetch webpage content
response <- request(url) |>
req_perform()

# Parse the webpage content
content <- response |> resp_body_string()
html <- read_html(content)

# Extract Open Graph tags
og_tags <- html |>
html_nodes(xpath = '//meta[starts-with(@property, "og:")]')

# Extract the properties and their content
og_info <- lapply(og_tags, function(tag) {
html_attr(tag, "content")
})
names(og_info) <- sapply(og_tags, function(tag){
gsub("og:", "", html_attr(tag, "property"))
})
og_info
}

add_og_info <- function(path){
jsoncontent <- jsonlite::read_json(path)

if(jsoncontent$type != "youtube"){
jsoncontent$opengraph <- extract_opengraph_info(jsoncontent$url)
jsonlite::write_json(
jsoncontent,
path,
auto_unbox = TRUE,
pretty = TRUE
)
}
}

fileslist <- list.files(
here::here("blogs/"),
pattern = "json$",
full.names = TRUE
)

lapply(fileslist, add_og_info)