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 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from hrbrmstr/master
tweet shot + other cleanup
- Loading branch information
Showing
9 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#' 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. | ||
#' | ||
#' 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. | ||
#' | ||
#' @md | ||
#' @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 | ||
#' @examples \dontrun{ | ||
#' tweet_shot("947082036019388416") | ||
#' tweet_shot("https://twitter.com/jhollist/status/947082036019388416") | ||
#' } | ||
tweet_shot <- function(statusid_or_url, zoom=3, scale=TRUE) { | ||
|
||
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) | ||
|
||
# can we do it? | ||
try_require("glue", "glue_data") | ||
try_require("magick", "image_read") | ||
try_require("webshot", "webshot") | ||
try_require("scales", "percent") | ||
|
||
# yes we can! (if the function got to this point) | ||
|
||
x <- statusid_or_url | ||
|
||
# first test if we have a Twitter URL | ||
is_url <- grepl("^http[s]://", x) | ||
|
||
if (is_url) { # mebbe, let's look further | ||
|
||
is_twitter <- grepl("twitter", x) # shld have "twitter" in it | ||
if (!is_twitter) stop("statusid_or_url must be a valid Twitter status id or URL", call.=FALSE) | ||
|
||
is_status <- grepl("status", x) # shld also have "status" in it | ||
if (!is_status) stop("statusid_or_url must be a valid Twitter status id or URL", call.=FALSE) | ||
|
||
already_mobile <- grepl("://mobile\\.", x) # if it's not a mobile status, make it one | ||
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) # nope | ||
|
||
# make a mobile URL | ||
x <- glue::glue_data(x, "https://mobile.twitter.com/{screen_name}/status/{status_id}") | ||
|
||
} | ||
|
||
# keep the filesystem clean | ||
tf <- tempfile(fileext = ".png") | ||
on.exit(unlink(tf), add=TRUE) # it'll clean up for us | ||
|
||
# capture the tweet | ||
webshot::webshot(url=x, file=tf, zoom=zoom) | ||
|
||
img <- magick::image_read(tf) # read the image in | ||
img <- magick::image_trim(img) # remove the extraneous border | ||
|
||
# scale if we want to | ||
if ((zoom > 1) && (scale)) img <- magick::image_scale(img, scales::percent(1/zoom)) | ||
|
||
img | ||
|
||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.