-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweet_toot.R
More file actions
112 lines (96 loc) · 2.41 KB
/
Copy pathtweet_toot.R
File metadata and controls
112 lines (96 loc) · 2.41 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
# Post vacancies to mastodon @bcchildcarebot@botsin.space
# Victor Yuan, June 2024
library(purrr)
library(dplyr)
library(readr)
library(lubridate)
library(tidyr)
library(glue)
library(forcats)
# parameters
max_nchar_per_toot <- 500 - 10 # 10 char buffer
max_toots <- 6
post_title <- 'New Vacancies:'
mastodon_token <- structure(
list(
bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN"),
type = "user",
instance = "botsin.space"
),
class = "rtoot_bearer"
)
# read in bc child care data ----
url <- 'https://catalogue.data.gov.bc.ca/dataset/4cc207cc-ff03-44f8-8c5f-415af5224646/resource/9a9f14e1-03ea-4a11-936a-6e77b15eeb39/download/childcare_locations.csv'
bccc <- readr::read_csv(url)
# identify vacancies ----
## set date --> data is in pst
.today <- lubridate::today(tzone = 'Canada/Pacific')
## vacancies for the last 7 days
bccc |>
filter(
VACANCY_LAST_UPDATE >= .today - 7,
VACANCY_SRVC_UNDER36 == 'Y'
) |>
count(VACANCY_LAST_UPDATE)
# craft message ----
## set dates, set timezone to pst, see OlsonNames()
.text <- bccc |>
# filter to yesterday, and group of interest
filter(
VACANCY_LAST_UPDATE >= .today - 1,
VACANCY_SRVC_UNDER36 == 'Y'
) |>
# order facilities by locations: Vancouver, Burnaby, Richmond, first
mutate(
CITY = fct(CITY) |>
fct_relevel(
'Vancouver',
'Burnaby',
'West Vancouver',
'North Vancouver',
'Richmond'
)
) |>
arrange(CITY) |>
# create message
## info to include
select(NAME, CITY, PHONE) |>
mutate(text = glue::glue("{CITY}, {NAME}, {PHONE}")) |>
select(text) %>%
## add post title
bind_rows(tibble(text = post_title), .) |>
## split by nchar limit
mutate(
n_char = nchar(text),
n_char_cumsum = cumsum(n_char),
n_char_cut = cut(
n_char_cumsum,
breaks = seq(0, 500 * max_toots, by = max_nchar_per_toot)
) |>
as.numeric()
) |>
## pull out text as character vector
group_nest(n_char_cut) |>
mutate(
text = purrr::map_chr(
data,
\(x) pull(x, text) |> paste0(collapse = '\n')
),
text = glue::glue("{n_char_cut}/{max(n_char_cut)}\n{text}")
) |>
pull(text)
if (length(.text) == 0) {
.text <- 'No new vacancies today.'
}
# Toot ----
message(glue('Sending {length(.text)} toots:'))
.text |> purrr::walk(message)
.text |>
purrr::walk(
\(x) {
rtoot::post_toot(
status = x,
token = mastodon_token
)
}
)