Skip to content

Commit

Permalink
Post to bluesky. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek authored Dec 1, 2024
1 parent 215dbf6 commit e1f5d86
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 17 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/bsky.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: bsky

on:
schedule:
- cron: '12 13 * * 1'
workflow_dispatch:


jobs:
post:
runs-on: windows-latest
env:
BSKY_APP_PASS: ${{ secrets.BSKY_APP_PASS}}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
with:
r-version: 'renv'
- uses: r-lib/actions/setup-renv@v2
with:
cache-version: 1
- name: Post to Bluesky.
run: Rscript runner-bsky.R
- name: Save skeet result.
uses: actions/upload-artifact@v4
with:
name: bsky_result
path: bsky_result.rds
69 changes: 69 additions & 0 deletions .github/workflows/reskeet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: reskeet

permissions:
actions: read
contents: read

on:
schedule:
- cron: '30 12 * * 2'
workflow_dispatch:


jobs:
post:
runs-on: windows-latest
env:
BSKY_DSLC: ${{ secrets.BSKY_DSLC}}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: Fetch the latest successful run ID from bsky workflow
shell: pwsh
run: |
$headers = @{
"Authorization" = "token ${{ secrets.GITHUB_TOKEN }}"
}
$uri = "https://api.github.com/repos/rfordatascience/ttpost/actions/workflows/bsky.yml/runs?status=success&branch=main&per_page=1"
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
$run_id = $response.workflow_runs[0].id.ToString().Trim()
echo "Latest run ID: $run_id"
echo "RUN_ID=$run_id" | Out-File -FilePath $env:GITHUB_ENV -Append
- name: Grab the artifact ID from that run
shell: pwsh
run: |
$headers = @{
"Authorization" = "Bearer ${{ secrets.GITHUB_TOKEN }}"
"Accept" = "application/vnd.github.v3+json"
}
$uri = "https://api.github.com/repos/rfordatascience/ttpost/actions/runs/${{ env.RUN_ID }}/artifacts"
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
Write-Output "Artifacts available:"
$response.artifacts | ForEach-Object {
Write-Output "Artifact Name: $($_.name), Artifact ID: $($_.id)"
echo "ARTIFACT_ID=$($_.id)" | Out-File -FilePath $env:GITHUB_ENV -Append
}
- name: Download post ID artifact
shell: pwsh
run: |
$headers = @{
"Authorization" = "Bearer ${{ secrets.GITHUB_TOKEN }}"
"Accept" = "application/vnd.github.v3+json"
}
$artifactId = $env:ARTIFACT_ID
$artifactUrl = "https://api.github.com/repos/rfordatascience/ttpost/actions/artifacts/$artifactId/zip"
Write-Host "Downloading artifact ID: $artifactId from URL: $artifactUrl"
Invoke-RestMethod -Uri $artifactUrl -Method Get -Headers $headers -OutFile "li_post_id.zip"
Write-Host "Artifact downloaded successfully to 'li_post_id.zip'."
Expand-Archive -LiteralPath "li_post_id.zip" -DestinationPath "." -Force
- uses: r-lib/actions/setup-r@v2
with:
r-version: 'renv'
- uses: r-lib/actions/setup-renv@v2
with:
cache-version: 1
- name: Repost on Bluesky.
run: Rscript dslc-reskeet.R
28 changes: 28 additions & 0 deletions dslc-reskeet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
source("helpers-bsky.R", local = TRUE)

bsky_result <- readRDS("bsky_result.rds")

if (attr(bsky_result, "week") != lubridate::week(lubridate::now())) {
stop("The bsky result was not generated this week. Stopping.")
}

attr(bsky_result, "week") <- NULL

post_uri <- bsky_result$uri

bskyr::set_bluesky_user("dslc.io")
bskyr::set_bluesky_pass(Sys.getenv("BSKY_DSLC"))
bskyr::bs_auth(bskyr::get_bluesky_user(), bskyr::get_bluesky_pass(), NULL)

bskyr::bs_like(post_uri)

reskeet_msg <- paste(
"It's #TidyTuesday y'all! Show us what you made on our Slack at https://dslc.io!",
"#RStats #PyData #JuliaLang #RustLang #DataViz #DataScience #DataAnalytics #data #tidyverse #DataBS",
sep = "\n"
)

result <- bskyr::bs_post(
reskeet_msg,
quote = post_uri
)
12 changes: 12 additions & 0 deletions helpers-bsky.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tt_skeet <- function(status_msg, img_paths, alt_text) {
result <- bskyr::bs_post(
text = status_msg,
images = img_paths,
images_alt = alt_text
)
if (result$validation_status[[1]] != "valid") {
stop("Bluesky broke!")
}

return(result)
}
27 changes: 27 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@
],
"Hash": "e84984bf5f12a18628d9a02322128dfd"
},
"bskyr": {
"Package": "bskyr",
"Version": "0.2.0",
"Source": "GitHub",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteUsername": "christopherkenny",
"RemoteRepo": "bskyr",
"RemoteRef": "main",
"RemoteSha": "bf8cfcb69a481271179881689197cd6e403f76b9",
"Requirements": [
"R",
"cli",
"dplyr",
"fs",
"httr2",
"lubridate",
"mime",
"purrr",
"rlang",
"stringi",
"stringr",
"tibble",
"tidyr"
],
"Hash": "2c70021b4889b7557e66b3bad5694a8d"
},
"cli": {
"Package": "cli",
"Version": "3.6.3",
Expand Down
42 changes: 42 additions & 0 deletions runner-bsky.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Do the common tasks.
source("runner-shared.R", local = TRUE)

# Skeet.
source("helpers-bsky.R", local = TRUE)

bsky_msg_start <- stringr::str_replace(
status_msg_start,
"https://DSLC.io welcomes you",
"@dslc.io welcomes you"
)

bsky_msg <- paste(bsky_msg_start, status_msg_end, sep = "\n")

if (nchar(article_msg) + nchar(bsky_msg) < 300) {
bsky_msg_start <- paste(bsky_msg_start, article_msg, sep = "\n")
bsky_msg <- paste(bsky_msg_start, status_msg_end, sep = "\n")
}

if (length(metadata$credit$bluesky)) {
bsky_credit <- stringr::str_replace(
metadata$credit$bluesky,
"https://bsky.app/profile/",
"@"
)
credit <- glue::glue("Curator: {bsky_credit}")
maybe_msg <- paste(
credit, bsky_msg, sep = "\n"
)
if (length(credit) && (nchar(maybe_msg) <= 300)) {
bsky_msg <- maybe_msg
}
}

bskyr::set_bluesky_user("jonthegeek.com")
bskyr::set_bluesky_pass(Sys.getenv("BSKY_APP_PASS"))
bskyr::bs_auth(bskyr::get_bluesky_user(), bskyr::get_bluesky_pass(), NULL)

result <- tt_skeet(bsky_msg, img_paths, alt_text)

attr(result, "week") <- lubridate::week(lubridate::now())
saveRDS(result, "bsky_result.rds")
40 changes: 23 additions & 17 deletions runner-shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ source("helpers-generic.R")
week_num <- next_week_num()
week_year <- next_year()

call_to_action <- "Submit a dataset! https://github.com/rfordatascience/tidytuesday/blob/main/.github/CONTRIBUTING.md"
call_to_action <- paste0(
"\n",
"Submit a dataset! https://github.com/rfordatascience/tidytuesday/blob/main/.github/CONTRIBUTING.md"
)

# Week 1 is "bring your own data", let's deal with that specifically.
if (week_num == 1) {
status_msg <- glue::glue(
status_msg_start <- glue::glue(
"It's week 1 of {week_year}, which means it's Bring Your Own Data Week!\n",
"Ideas:",
"{emoji::emoji('rewind')} A previous #TidyTuesday dataset (https://tidytues.day/{week_year - 1})",
"{emoji::emoji('index pointing at the viewer')} Personal metadata (TV shows watched, music listened, #RStats written, etc)",
"{emoji::emoji('question')} Whatever else you want to use!",
paste0("\n", call_to_action),
"\n#r4ds #tidyverse #DataViz",
.sep = "\n"
)
status_msg_end <- "\n#r4ds #tidyverse #DataViz"
img_paths <- NULL
alt_text <- NULL
} else {
Expand Down Expand Up @@ -76,31 +78,35 @@ if (week_num == 1) {
)
)

status_msg <- glue::glue(
status_msg_start <- glue::glue(
"https://DSLC.io welcomes you to week {week_num} of #TidyTuesday!",
" We're exploring {data_title}!\n\n",
"{emoji::emoji('folder')} https://tidytues.day/{week_year}/{week_date}"
)
status_msg_end <- paste(
"\n",
call_to_action,
"\n#RStats #PyData #JuliaLang #DataViz #tidyverse #r4ds",
sep = "\n"
)
status_msg_end <- "\n#RStats #PyData #JuliaLang #DataViz #tidyverse #r4ds"

status_msg_start_not_bsky <- status_msg_start
if (length(metadata)) {
article_msg <- glue::glue(
"{emoji::emoji('news')} {metadata$article$url}"
)
long_msg <- glue::glue(
status_msg,
"\n{emoji::emoji('news')} {metadata$article$url}"
status_msg_start_not_bsky,
article_msg
)
if (nchar(long_msg) + nchar(status_msg_end) <= 500) {
status_msg <- long_msg
if (nchar(long_msg) + nchar(call_to_action) + nchar(status_msg_end) + 1 < 500) {
status_msg_start_not_bsky <- long_msg
}
alt_text <- c(
alt_text,
img_alts
)
}

status_msg <- glue::glue(status_msg, status_msg_end)
}

status_msg <- glue::glue(
status_msg_start_not_bsky,
call_to_action,
status_msg_end,
.sep = "\n"
)

0 comments on commit e1f5d86

Please sign in to comment.