Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #163 from hrbrmstr/master
Browse files Browse the repository at this point in the history
tweet shot + other cleanup
  • Loading branch information
mkearney authored Jan 1, 2018
2 parents 7f3a756 + f67d9b9 commit c6cbb75
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 14 deletions.
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ Suggests:
ggplot2,
knitr,
rmarkdown,
testthat
testthat,
glue,
magick,
webshot,
scales
VignetteBuilder: knitr
LazyData: yes
RoxygenNote: 6.0.1.9000
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export(suggested_users)
export(trends_available)
export(ts_data)
export(ts_plot)
export(tweet_shot)
export(tweets_data)
export(tweets_with_users)
export(uq_rtweet)
Expand Down
7 changes: 1 addition & 6 deletions R/get-my-timeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
#' Must be of length 1 or equal to length of user.
#' @param max_id Character, status_id from which returned tweets
#' should be older than.
#' @param home Logical, indicating whether to return a user-timeline
#' or home-timeline. By default, home is set to FALSE, which means
#' \code{get_timeline} returns tweets posted by the given user. To
#' return a user's home timeline feed, that is, the tweets posted by
#' accounts followed by a user, set the home to false.
#' @param parse Logical, indicating whether to return parsed
#' (data.frames) or nested list object. By default, \code{parse =
#' TRUE} saves users from the time [and frustrations] associated
#' TRUE} saves users from the time (and frustrations) associated
#' with disentangling the Twitter API return objects.
#' @param check Logical indicating whether to remove check available
#' rate limit. Ensures the request does not exceed the maximum
Expand Down
1 change: 1 addition & 0 deletions R/next_cursor.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ get_max_id <- function(x) {


#' @rdname next_cursor
#' @param .x id
#' @export
max_id <- function(.x) UseMethod("max_id")

Expand Down
84 changes: 84 additions & 0 deletions R/tweet_shot.R
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

}
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ obs2string <- function(x, sep) {
paste(x, collapse = sep)
}

# Enables loading packages when necessary vs import

try_require <- function(pkg, f) {

if (requireNamespace(pkg, quietly = TRUE)) {
library(pkg, character.only = TRUE)
return(invisible())
}

stop("Package `", pkg, "` required for `", f , "`.\n",
"Please install and try again.", call. = FALSE)
}

is.valid.username <- function(username) {
!grepl(' ', username);
}

8 changes: 1 addition & 7 deletions man/get_my_timeline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/next_cursor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions man/tweet_shot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c6cbb75

Please sign in to comment.