-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdslc-helpers-mastodon.R
122 lines (109 loc) · 2.96 KB
/
dslc-helpers-mastodon.R
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
Sys.setenv(RTOOT_DEFAULT_TOKEN = Sys.getenv("RTOOT_DSLC_TOKEN"))
get_jon_toot <- function() {
jon_toot <- rtoot::get_account_statuses(
id = "109245649177941364",
hashtag = "#TidyTuesday",
exclude_reblogs = TRUE,
limit = 3
) |>
dplyr::filter(
stringr::str_detect(
content,
stringr::fixed("welcomes you to week")
)
) |>
head(1)
jon_content <- jon_toot$content |>
rvest::read_html() |>
rvest::html_text2()
return(
list(
content = jon_content,
url = jon_toot$url,
id = jon_toot$id
)
)
}
like_jon_toot <- function(id) {
rtoot::post_status(
id,
"favourite",
verbose = FALSE
)
rtoot::post_status(
id,
"reblog",
verbose = FALSE
)
return(invisible(TRUE))
}
# To get a user's ID, te easiest thing seems to be to
# rtoot::search_accounts("username"), and filter for the specific user.
#
# rtoot::search_accounts("DSLC") |> dplyr::filter(acct == "DSLC") |>
# dplyr::pull(id)
get_dslc_toots <- function() {
dslc_toots <- rtoot::get_account_statuses(
id = "112269446568103237",
hashtag = "#TidyTuesday",
exclude_reblogs = TRUE,
# Get a bunch in case I go on a promotion spree.
limit = 10
) |>
dplyr::filter(
stringr::str_starts(
content, stringr::fixed(
'<p>It's <a href=\"https://fosstodon.org/tags/TidyTuesday\"'
)
)
) |>
head(2) |>
dplyr::select(id, pinned) |>
dplyr::arrange(pinned)
return(
rlang::set_names(
dslc_toots$id,
c("unpinned", "pinned")
)
)
}
set_toot_content <- function(tt_toot_jon) {
toot_content <- glue::glue(
"It's #TidyTuesday y'all! Show us what you made on our Slack at https://dslc.io/join (find the #chat-tidytuesday channel)!",
"RT @jonthegeek {tt_toot_jon$url}",
"{tt_toot_jon$content}",
.sep = "\n\n"
)
if (nchar(toot_content) > 500) {
toot_content <- glue::glue(
"It's #TidyTuesday y'all! Show us what you made on our Slack at https://dslc.io/join (find the #chat-tidytuesday channel)!",
"RT @jonthegeek {tt_toot_jon$url}",
"#RStats #PyData #JuliaLang #DataViz #tidyverse #r4ds",
.sep = "\n\n"
)
}
return(toot_content)
}
rtoot_token_from_envvar <- function(envvar = "RTOOT_DSLC_TOKEN") {
rlang::set_names(
as.list(strsplit(x = Sys.getenv(envvar), split = ";")[[1]]),
c("bearer", "type", "instance")
)
}
pin_status <- function(id, unpin = FALSE) {
action <- dplyr::if_else(unpin, "unpin", "pin")
token <- rtoot_token_from_envvar()
path <- glue::glue(
"https://{token$instance}/api/v1/statuses/{id}/{action}"
)
response <- httr2::request(path) |>
httr2::req_auth_bearer_token(token$bearer) |>
httr2::req_user_agent("ttpost (https://github.com/rfordatascience/ttpost)") |>
httr2::req_method("post") |>
httr2::req_perform()
if (httr2::resp_status(response) == 200) {
return(invisible(NULL))
} else {
return(response)
}
}