-
Notifications
You must be signed in to change notification settings - Fork 25
add draft scripts for blog-airtable automation #459
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
Draft
cecibaldoni
wants to merge
5
commits into
main
Choose a base branch
from
blog-airtable-automation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c100c1
add initial scripts for blog-aitable automation
cecibaldoni 781c950
refactor scripts based on code review feedback
cecibaldoni 143b9a3
fixed blog_function missing parenthesis
cecibaldoni 4320c7d
Merge branch 'main' into blog-airtable-automation
drmowinckels eaf5a0c
fixed images handling and added github workflow
cecibaldoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| library(airtabler) | ||
| library(tidyverse) | ||
| library(glue) | ||
| library(readr) | ||
|
|
||
| source("scripts/blog_functions.R") | ||
|
|
||
| # Get data and draft blogposts---- | ||
| process_events <- function(base_id) { | ||
|
|
||
| # Connect and get data | ||
| base <- connect_to_airtable(base_id, tables = "Event Reports") | ||
| events_data <- base$`Event Reports`$select() | ||
|
|
||
| # Filter ready-to-draft events | ||
| events_to_draft <- filter_ready_to_draft(events_data, title_col = "title") | ||
|
|
||
| if (nrow(events_to_draft) == 0) { | ||
| message("No new event reports to draft.") | ||
| return(0) | ||
| } | ||
|
|
||
| message("Found ", nrow(events_to_draft), " new event report(s) to draft.") | ||
|
|
||
| # Process each event | ||
| for (i in 1:nrow(events_to_draft)) { | ||
| report <- events_to_draft[i, ] | ||
|
|
||
| tryCatch({ | ||
| # Validate required fields | ||
| if (!validate_required_fields(report, c("title", "createdTime"))) { | ||
| next | ||
| } | ||
|
|
||
| # Create slug and paths | ||
| slug <- create_slug(report$title) | ||
| post_date <- ymd_hms(report$createdTime) | ||
| year <- year(post_date) | ||
| month_day_slug <- paste0(format(post_date, "%m-%d"), "-", slug) | ||
|
|
||
| # Create folder | ||
| folder_path <- create_post_folder(year, month_day_slug) | ||
|
|
||
| # Download images if they exist | ||
| image_markdown <- "" | ||
| if (!is.na(report$media) && report$media != "") { | ||
| message("Downloading images for event: ", report$title) | ||
| downloaded_images <- download_images(report$media, folder_path) | ||
|
|
||
| # Create markdown for images | ||
| if (!is.null(downloaded_images) && length(downloaded_images) > 0) { | ||
| image_markdown <- create_image_markdown(downloaded_images) | ||
| } | ||
| } | ||
| # Markdown front-matter ---- | ||
|
|
||
| front_matter <- glue( | ||
| '--- | ||
| title: "Event Report: {report$Title}" | ||
| author: "{report$Author}" | ||
| date: "{format(post_date, "%Y-%m-%d")}" | ||
| tags: [{report$Keywords}] | ||
| --- | ||
| ' | ||
| ) | ||
| # standardised blogpost | ||
|
|
||
| post_body <- glue( | ||
| 'On {report$Date}, {report$Chapter} hosted the event "{report$Title}". | ||
| The session was led by {report$Speakers} and was attended by {report$Participants} participants. | ||
|
|
||
| ## Event Summary | ||
|
|
||
| Here are the key topics that were covered: | ||
|
|
||
| {report$Summary} | ||
|
|
||
| ## Attendee Feedback | ||
|
|
||
| > {report$`Quotes or Reactions (optional)`} | ||
|
|
||
| ## Resources | ||
|
|
||
| A big thank you to the speakers and everyone who attended!' | ||
| ) | ||
|
|
||
| # Write file | ||
| content <- paste0(front_matter, post_body) | ||
| filepath <- write_markdown_file(content, folder_path) | ||
| message("✓ Successfully created: ", filepath) | ||
|
|
||
| }, error = function(e) { | ||
| warning("Error processing event ", i, ": ", e$message) | ||
| }) | ||
| } | ||
|
|
||
| return(nrow(events_to_draft)) | ||
| } | ||
|
|
||
| # After successfully creating the markdown file: | ||
| # Update the Airtable record to: | ||
| # - Status = "Post drafted" | ||
| # - Add PR URL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| library(tidyverse) | ||
| library(glue) | ||
| library(httr) | ||
| library(airtabler) | ||
|
|
||
| # Connect to Airtable base | ||
| connect_to_airtable <- function(base_id, tables) { | ||
| message("Connecting to Airtable...") | ||
| base <- airtable(base = base_id, tables = tables) | ||
| return(base) | ||
| } | ||
|
|
||
| # Filter data to get records ready for drafting | ||
| # Removes the dont-delete row and filters for In progress + Accept status | ||
| filter_ready_to_draft <- function(data, title_col = "Title") { | ||
| data %>% | ||
| filter( | ||
| .data[[title_col]] != "dont-delete", | ||
| Status == "In progress", | ||
| Decision == "Accept" | ||
| ) | ||
| } | ||
|
|
||
| # Create a URL-friendly slug from text | ||
| create_slug <- function(text) { | ||
| text %>% | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider switching to base pipe? |
||
| str_to_lower() %>% | ||
| str_replace_all("[^a-z0-9\\s-]", "") %>% | ||
| str_replace_all("\\s+", "-") | ||
| } | ||
|
|
||
| # Create blog post folder structure (content/blog/YYYY/mm-dd-slug/) | ||
| create_post_folder <- function(year, month_day_slug) { | ||
| folder_path <- file.path("content", "blog", as.character(year), month_day_slug) | ||
| dir.create(folder_path, recursive = TRUE, showWarnings = FALSE) | ||
| return(folder_path) | ||
| } | ||
|
|
||
| # Escape special characters for YAML frontmatter | ||
| escape_yaml <- function(text) { | ||
| if (is.null(text) || is.na(text) || text == "") return('""') | ||
| # Escape quotes and wrap in quotes if text contains special YAML characters | ||
| if (str_detect(text, '[":\\n]')) { | ||
| text <- str_replace_all(text, '"', '\\\\"') | ||
| text <- paste0('"', text, '"') | ||
| } | ||
| return(text) | ||
| } | ||
|
|
||
| # Download images from URLs and save to folder | ||
| # Returns vector of local filenames | ||
| download_images <- function(image_urls, folder_path) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it returns nothing when there are images to download? |
||
|
|
||
| # Handle NULL or empty input | ||
| if (is.null(image_urls) || is.na(image_urls) || length(image_urls) == 0 || image_urls == "") { | ||
| return(NULL) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| library(airtabler) | ||
| library(tidyverse) | ||
| library(glue) | ||
| library(lubridate) | ||
|
|
||
| source("scripts/blog_functions.R") | ||
|
|
||
| # Get data and create draft blogpost ---- | ||
| process_proposals <- function(base_id) { | ||
|
|
||
| # Connect and get data | ||
| base <- connect_to_airtable(base_id, tables = "Proposals") | ||
| proposals_data <- base$Proposals$select() | ||
|
|
||
| # Filter ready-to-draft proposals | ||
| proposals_to_draft <- filter_ready_to_draft(proposals_data, title_col = "Title") | ||
|
|
||
| if (nrow(proposals_to_draft) == 0) { | ||
| message("No new proposals to draft.") | ||
| return(0) | ||
| } | ||
|
|
||
| message("Found ", nrow(proposals_to_draft), " new proposal(s) to draft.") | ||
|
|
||
| # Process each proposal | ||
| for (i in 1:nrow(proposals_to_draft)) { | ||
| proposal <- proposals_to_draft[i, ] | ||
|
|
||
| tryCatch({ | ||
| # Validate required fields | ||
| if (!validate_required_fields(proposal, c("Title", "Post date"))) { | ||
| next | ||
| } | ||
|
|
||
| # Create slug and paths | ||
| slug <- create_slug(proposal$Title) | ||
| post_date_obj <- ymd(proposal$`Post date`) | ||
| year <- year(post_date_obj) | ||
| month_day_slug <- paste0(format(post_date_obj, "%m-%d"), "-", slug) | ||
|
|
||
| # Create folder | ||
| folder_path <- create_post_folder(year, month_day_slug) | ||
|
|
||
| # Build content | ||
| front_matter <- glue( | ||
| '--- | ||
| title: {escape_yaml(proposal$Title)} | ||
| author: {escape_yaml(proposal$Author)} | ||
| date: "{proposal$`Post date`}" | ||
| slug: {slug} | ||
| --- | ||
|
|
||
| ' | ||
| ) | ||
|
|
||
| content <- paste0(front_matter, proposal$Description) | ||
|
|
||
| # Write file | ||
| filepath <- write_markdown_file(content, folder_path) | ||
| message("✓ Successfully created: ", filepath) | ||
|
|
||
| }, error = function(e) { | ||
| warning("Error processing proposal ", i, ": ", e$message) | ||
| }) | ||
| } | ||
|
|
||
| return(nrow(proposals_to_draft)) | ||
| } | ||
|
|
||
|
|
||
| # After successfully creating the markdown file: | ||
| # Update the Airtable record to: | ||
| # - Status = "Post drafted" | ||
| # - Add PR URL (once you know it) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you load airtabler and glue in the functions script, so you wouldnt need to load it again here.