Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active January 18, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/996d066ead78acfaca47c0d37b966e8a to your computer and use it in GitHub Desktop.
Save matt-dray/996d066ead78acfaca47c0d37b966e8a to your computer and use it in GitHub Desktop.
Fetch Wordle results from Twitter with {rtweet} and put them in a little dataframe
# I tweeted about this:
# https://twitter.com/mattdray/status/1481071849891057668?s=20
# Then I blogged about it:
# https://www.rostrum.blog/2022/01/14/wordle/
# Function to extract Wordle results from tweet data
twirdle <- function(tweets) {
g <- "\U1F7E9"
o <- "\U1F7E7"
y <- "\U1F7E8"
blu <- "\U1F7E6"
bla <- "\U2B1B"
w <- "\U2B1C"
rx_all <- paste(g, o, y, blu, bla, w, sep = "|")
rx_right <- paste(g, o, sep = "|")
rx_place <- paste(y, blu, sep = "|")
rx_wrong <- paste(bla, w, sep = "|")
rx_color <- paste(o, blu, sep = "|")
tweets$meta <- regexpr(
"Wordle \\d{1,} [\\d{1}|X]/\\d{1}",
tweets$text,
perl = TRUE
)
tweets$meta <- setNames(
tweets$meta,
ifelse(tweets$meta < 0, FALSE, TRUE)
)
tweets$meta <- ifelse(
names(tweets$meta),
regmatches(tweets$text, tweets$meta),
NA_character_
)
tweets <- tweets[!is.na(tweets$meta), ]
tweets$edition <- as.numeric(
regmatches(
tweets$meta,
regexpr("\\d{1,}", tweets$meta)
)
)
tweets$attempts <- regmatches(
tweets$meta,
regexpr("[\\d{1}|X](?=/)", tweets$meta, perl = TRUE)
)
tweets$attempts <- ifelse(
tweets$attempts == "X",
NA_character_,
tweets$attempts
)
tweets$attempts <- as.numeric(tweets$attempts)
tweets$allowed <- as.numeric(
regmatches(
tweets$meta,
regexpr("(?<=/)\\d{1}", tweets$meta, perl = TRUE)
)
)
tweets$grid <- regmatches(
tweets$text,
gregexpr(rx_all, tweets$text)
)
tweets$grid <- lapply(
tweets$grid,
function(x) paste(x, collapse = "")
)
tweets$colorblind <- ifelse(
grepl(rx_color, tweets$grid), TRUE, FALSE
)
tweets$mode <- ifelse(
grepl(bla, tweets$grid), "dark",
ifelse(grepl(w, tweets$grid), "light", "unknown")
)
tweets$grid <- gsub(rx_right, "G", tweets$grid)
tweets$grid <- gsub(rx_place, "Y", tweets$grid)
tweets$grid <- gsub(rx_wrong, "-", tweets$grid)
tweets[, c("edition", "attempts", "allowed", "mode",
"colorblind", "grid", "status_id")]
}
# Fetch some tweets
tweets <- rtweet::search_tweets(
q = "Wordle \U2B1B OR \U2B1C",
n = 10, # return 10 tweets
include_rts = FALSE # no retweets
)
# Extract data
twirdle(tweets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment