Created
December 1, 2024 14:16
-
-
Save jeremy-allen/4b52b5a3ad462f90181cafc97a053839 to your computer and use it in GitHub Desktop.
Rank latest rstats posts from Bluesky and print to console
This file contains 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
library(bskyr) | |
library(tidyverse) | |
library(glue) | |
# Rank latest rstats posts from Bluesky, accrding to likes, and | |
# print to console | |
# authenticate to bsky | |
auth <- bs_auth(user = bs_get_user(), pass = bs_get_pass()) | |
# experiement with different values for n_posts. After 100, try with 50 | |
n_posts <- 100 | |
query <- "#rstats" | |
# search for posts | |
rstats_posts_raw <- bs_search_posts( | |
query = query, | |
limit = n_posts, | |
sort = "latest" | |
) |> | |
arrange(desc(like_count)) | |
# define a function to construct a URL to a post on a user profile | |
post_url <- function(did, uri) { | |
# did should be the author's did | |
# the uri should be the uri of the post | |
# from the uri we will extract the rkey and combine it | |
# with the author's did to make a link to the post | |
# on the author's profile | |
base <- "https://bsky.app/profile/" | |
user <- paste0(did, "/post/") | |
rkey <- str_extract(uri, "[A-Za-z0-9]+(?=\\/?$)") | |
paste0( | |
base, user, rkey | |
) | |
} | |
# just the 50 most liked | |
posts <- rstats_posts_raw |> | |
slice(1:50) |> | |
select(uri, like_count, repost_count, author, record) |> | |
unnest_wider(c(author, record), names_sep = "_") |> | |
select(uri, author_did, like_count, repost_count, author_displayName, | |
record_text, author_handle) |> | |
mutate( | |
post_url = post_url(did = author_did, uri = uri), | |
rank = row_number() | |
) |> | |
arrange(desc(rank)) | |
# print in console | |
cat( | |
paste0( | |
"\n", posts[["rank"]], ". ", posts[["author_displayName"]], "\n", | |
posts[["like_count"]], " likes:", "\n", | |
posts[["record_text"]], "\n", | |
posts[["post_url"]], "\n", | |
"-----", "\n", | |
sep = "" | |
), | |
sep = "" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment