This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
tweet_shot.R
114 lines (95 loc) · 3.35 KB
/
tweet_shot.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
#' Capture an image of a tweet/thread
#'
#' Provide a status id or a full Twitter link to a tweet and this function
#' will capture an image of the tweet --- or tweet + thread (if there are
#' Twitter-linked replies) --- from the mobile version of said tweet/thread.
#' `r lifecycle::badge("deprecated")`
#'
#' For this to work, you will need to ensure the packages in `Suggests:` are
#' installed as they will be loaded upon the first invocation of this function.
#'
#' Use the `zoom` factor to get more pixels which may improve the text rendering
#' of the tweet/thread.
#'
#' @param statusid_or_url a valid Twitter status id (e.g. "`947082036019388416`") or
#' a valid Twitter status URL (e.g. "`https://twitter.com/jhollist/status/947082036019388416`").
#' @param zoom a positive number >= 1. See the help for `[webshot::webshot()]` for more information.
#' @param scale auto-scale the image back to 1:1? Default it `TRUE`, which means `magick`
#' will be used to return a "normal" sized tweet. Set it to `FALSE` to perform your
#' own image manipulation.
#' @return `magick` object
#' @export
#' @seealso [`rtweet-deprecated`]
#' @examples
#' \dontrun{
#' if (auth_has_default()) {
#' shot1 <- tweet_shot("947061504892919808")
#' plot(shot1)
#' shot2 <- tweet_shot("https://twitter.com/ma_salmon/status/947061504892919808")
#' plot(shot2)
#' }
#' }
tweet_shot <- function(statusid_or_url, zoom = 3, scale = TRUE) {
lifecycle::deprecate_warn("1.0.0", "tweet_shot()",
details = "The resulting image might not have a screenshot of the tweet")
check_installed(c("magick", "webshot"))
statusid_or_url <- statusid_or_url[1]
zoom <- zoom[1]
scale <- scale[1]
if (zoom <= 1) {
stop("zoom must be a positive number, >= 1", call. = FALSE)
}
if (!is_logical(scale)) {
stop("scale must be TRUE/FALSE", call. = FALSE)
}
x <- statusid_or_url
## first test if we have a Twitter URL
is_url <- grepl("^http[s]://", x)
if (is_url) {
## should have "twitter" in it
is_twitter <- grepl("twitter", x)
if (!is_twitter) {
stop("statusid_or_url must be a valid Twitter status id or URL",
call. = FALSE)
}
## shld also have "status" in it
is_status <- grepl("status", x)
if (!is_status) {
stop("statusid_or_url must be a valid Twitter status id or URL",
call. = FALSE)
}
## if it's not a mobile status, make it one
already_mobile <- grepl("://mobile\\.", x)
if (!already_mobile) {
x <- sub("://twi", "://mobile.twi", x)
}
} else {
## let's see if it's a status id
x <- rtweet::lookup_tweets(x)
if (!(nrow(x) > 0)) {
stop("Twitter status not found", call. = FALSE)
}
## make a mobile URL
x <- sprintf("https://mobile.twitter.com/%s/status/%s",
users_data(x)$screen_name, x$id_str)
}
## keep the filesystem clean
tf <- tempfile(fileext = ".png")
## it'll clean up for us
on.exit(unlink(tf), add = TRUE)
## capture the tweet
webshot::webshot(url = x, file = tf, zoom = zoom)
## read the image in
img <- magick::image_read(tf)
## remove the extraneous border
img <- magick::image_trim(img)
## scale if we want to
if ((zoom > 1) && (scale)) {
img <- magick::image_scale(img, as_percent(1/zoom))
}
## return img
img
}
as_percent <- function(x) {
paste0(round(x * 100), "%")
}