Created
February 15, 2023 21:46
-
-
Save max-alletsee/b113fa3af5d83e662a01044e91f4fd4d to your computer and use it in GitHub Desktop.
Replacing ReadRuler with an R function (as the site will shut down on May 1st)
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
# Replacement for Read Ruler | |
# which is going to be deactivated on May 1st, 2023 | |
# make sure to set up an app and the authentication in Pocket's interface: https://github.com/CorrelAid/pocketapi#authentication | |
# parameters: | |
# words_per_minute: avg. reading speed in words per minute | |
# minute_groups: vector indicating which limits to use for the reading time groups | |
read_ruler <- function(words_per_minute = 500, minute_groups = c(1, 2, 5, 10, 15, 20, 30, 45, 60, 75)) { | |
library(pocketapi) | |
# get queue | |
data_unread <- pocket_get(state = "unread") | |
# subset: which articles have no "min" tag? | |
data_unread_new <- data_unread[!grepl(pattern="min", x = data_unread$tags, fixed = TRUE), ] | |
# round it on the conservative side | |
data_unread_new$reading_time_minutes <- ceiling(data_unread_new$word_count / words_per_minute) | |
data_unread_new$reading_time_grouped <- cut(x = data_unread_new$reading_time_minutes, | |
breaks = c(-Inf, minute_groups, Inf), | |
labels = c( | |
paste0(minute_groups, "min"), | |
paste0(">", minute_groups[length(minute_groups)]) | |
), | |
right = TRUE, | |
include.lowest = TRUE) | |
# creating a list, separated by minute group | |
data_unread_new_split <- split(data_unread_new, data_unread_new$reading_time_grouped) | |
# loop over all minute groups and set the corresponding tag | |
for (group in 1:length(data_unread_new_split)) { | |
# check that we actually have articles in the group | |
if(nrow(data_unread_new_split[[group]]) > 0) { | |
# then add the tag for the corresponding IDs with the corresponding estimated reading time | |
pocket_tag(action_name = "tags_add", | |
item_ids = data_unread_new_split[[group]]$item_id, | |
tags = unique(data_unread_new_split[[group]]$reading_time_grouped)) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment