-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner-shared.R
More file actions
153 lines (140 loc) · 4.98 KB
/
runner-shared.R
File metadata and controls
153 lines (140 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Load helper functions.
source("helpers-generic.R")
# Gather minimal data about this week.
week_num <- next_week_num()
week_year <- next_year()
call_to_action <- paste0(
"\n",
"Submit a dataset! https://github.com/rfordatascience/tidytuesday/blob/main/.github/CONTRIBUTING.md"
)
metadata <- NULL
article_msg <- NULL
# Week 1 is "bring your own data", let's deal with that specifically.
if (week_num == 1) {
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!",
.sep = "\n"
)
status_msg_start_not_bsky <- status_msg_start
status_msg_end <- "\n#r4ds #tidyverse #DataViz"
img_paths <- NULL
alt_text <- NULL
} else {
available_datasets <- tidytuesdayR::tt_datasets(week_year) |>
unclass() |>
tibble::as_tibble()
# This if/else is somewhat backward, because it's easier to stop early and get
# rid of the if.
if (!(week_num %in% available_datasets$Week)) {
stop("No data available!")
}
# Sort out the rest of the info that goes into social media posts.
metadata <- read_post_vars()
data_title <- metadata$title
week_date <- next_tuesday()
# Download the images to the action runner.
img_files <- metadata$images |> purrr::map_chr("file")
img_alts <- metadata$images |> purrr::map_chr("alt") |> stringr::str_trim()
max_bsky_size <- fs::fs_bytes("976.56KB")
max_mastodon_megapix <- 8.3
purrr::walk(
img_files,
\(image) {
download.file(next_file(image), image, mode = "wb")
img <- magick::image_read(image)
original_img_size <- magick::image_info(img)$filesize
if (original_img_size >= max_bsky_size) {
# Round down to make sure we're *under* 1MB. This isn't actually
# guaranteed to work because image size isn't directly proportional to
# file size, but it seems to err on the side of making things smaller
# than they need to be.
ratio <- floor(
as.integer(max_bsky_size) / as.integer(original_img_size) * 90
)
img <- magick::image_resize(
img,
magick::geometry_size_percent(ratio)
)
}
img_info <- magick::image_info(img)
megapix <- img_info$width * img_info$height / 1e6
if (megapix >= max_mastodon_megapix) {
ratio <- floor(
max_mastodon_megapix / megapix * 90
)
img <- magick::image_resize(
img,
magick::geometry_size_percent(ratio)
)
}
magick::image_write(img, image)
}
)
img_paths <- c(
"tt_logo.png",
"tt_rules.png",
img_files
)
alt_text <- c(
paste(
"Logo for the #TidyTuesday Project. The words TidyTuesday, A weekly data",
"project from the Data Science Learning Community (dslc.io) overlaying a",
"black paint splash."
),
paste(
"TidyTuesday is a weekly social data project. All are welcome to",
"participate! Please remember to share the code used to generate your",
"results!\nTidyTuesday is organized by the Data Science Learning",
"Community. Join our Slack for free online help with R and other",
"data-related topics, or to participate in a data-related book club!\n\n",
"How to Participate\nData is posted to social media every Monday",
"morning. Follow the instructions in the new post for how to download",
"the data.\nExplore the data, watching out for interesting",
"relationships. We would like to emphasize that you should not draw",
"conclusions about causation in the data.\nCreate a visualization, a",
"model, a shiny app, or some other piece of data-science-related output,",
"using R or another programming language.\nShare your output and the",
"code used to generate it on social media with the #TidyTuesday hashtag."
)
)
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 <- "\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_start_not_bsky,
article_msg,
.sep = "\n"
)
if (
nchar(long_msg, "bytes") +
nchar(call_to_action, "bytes") +
nchar(status_msg_end, "bytes") +
1 <
500
) {
status_msg_start_not_bsky <- long_msg
}
alt_text <- c(
alt_text,
img_alts
)
}
}
status_msg <- glue::glue(
status_msg_start_not_bsky,
call_to_action,
status_msg_end,
.sep = "\n"
)