Skip to content

Instantly share code, notes, and snippets.

@dieghernan
Last active February 22, 2021 16:10
Show Gist options
  • Save dieghernan/824425b814bbb9527db5085d6dacb157 to your computer and use it in GitHub Desktop.
Save dieghernan/824425b814bbb9527db5085d6dacb157 to your computer and use it in GitHub Desktop.
# Setup----
library(rtweet)
source("~/R/Projects/environment_vars.R") # secrets
api_key <- Sys.getenv("TWITTER_API_KEY")
api_secret_key <- Sys.getenv("TWITTER_API_SECRET")
access_token <- Sys.getenv("TWITTER_ACCESS_TOKEN")
access_token_secret <- Sys.getenv("TWITTER_ACCESS_TOKEN_SECRET")
## authenticate
token <- create_token(
app = "spainmunic",
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret
)
# Helper function - needed for checking chunks----
check_chunked_media_status = function(finalize_data, token, rurl) {
if(finalize_data$processing_info$state == "succeeded") {
return(finalize_data)
}
if(finalize_data$processing_info$state %in% c("pending", "in_progress")) {
Sys.sleep(finalize_data$processing_info$check_after_secs)
status <- httr::GET(rurl, query = list(command = "STATUS",
media_id = finalize_data$media_id_string), token)
status <- check_chunked_media_status(httr::content(status),token, rurl)
return(status)
}
}
# Main function----
upload_media_to_twitter <- function(media, token) {
media2upload <- httr::upload_file(media)
file_ext = function(x) {
pos <- regexpr("\\.([[:alnum:]]+)$", x)
ifelse(pos > -1L, substring(x, pos + 1L), "")
}
mediatype <- file_ext(media)
query <- "media/upload"
rurl <- paste0("https://upload.twitter.com/1.1/media/upload.json")
filesize <- file.size(media)
gif_no_chunked <- mediatype == "gif" & filesize <= 5*1024*1024
if(mediatype %in% c("jpg","jpeg", "png") | gif_no_chunked) {
rpost <- httr::POST(rurl, body = list(media = media2upload), token)
r <- httr::content(rpost)
return(r)
}
else if(mediatype %in% c("gif", "mp4")) {
if (mediatype == "gif") {
category <- "tweet_gif"
} else {
category <- "tweet_video"
}
init_r <- httr::POST(rurl, body = list(command = "INIT",
media_type = mediatype,
total_bytes = filesize,
media_category = category), token)
init_r_parsed <- httr::content(init_r)
media_id <- init_r_parsed$media_id_string
bytes_sent <- 0
videofile <- file(media, open = "rb")
segment_id <- 0
while (bytes_sent < filesize) {
chunk <- readBin(videofile, 4*1024*1024, what = "raw")
bytes_sent <- bytes_sent + 4*1024*1024
request_data <- list(command = "APPEND", media_id = media_id,
segment_index = segment_id, media = chunk)
r <- httr::POST(rurl, body = request_data, token)
segment_id <- segment_id + 1
}
close(videofile)
finalize_data <- httr::POST(rurl, body = list(command = "FINALIZE", media_id = media_id), token)
status_final = check_chunked_media_status(httr::content(finalize_data), token, rurl)
return(status_final)
} else {
stop("Media file `", media, "` extension not png, jpeg, jpg, gif, or mp4")
}
}
# Tests----
# Success if we get a reponse
## MP4 ----
### Sample mp4 - (>5mb) ----
temp <- tempfile(fileext = ".mp4")
url <-
"https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1280_10MG.mp4"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
### Sample mp4 - (<5mb) ----
temp <- tempfile(fileext = ".mp4")
url <-
"https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
## GIF----
### Sample animated gif (>5MB)----
temp <- tempfile(fileext = ".gif")
url <- "https://upload.wikimedia.org/wikipedia/commons/9/96/.Animation_of_Hayabusa2_orbit.gif"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
### Sample animated gif (<5Mb)----
temp <- tempfile(fileext = ".gif")
url <- "https://upload.wikimedia.org/wikipedia/commons/d/d0/01_Das_Sandberg-Modell.gif"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
### Sample unanimated gif (<5Mb)----
temp <- tempfile(fileext = ".gif")
url <- "https://file-examples-com.github.io/uploads/2017/10/file_example_PNG_500kB.png"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
### Sample unanimated gif (>5Mb) SHOULD ERROR-----
temp <- tempfile(fileext = ".gif")
url <- "https://sample-videos.com/img/Sample-png-image-5mb.png"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
## PNG----
temp <- tempfile(fileext = ".png")
url <- "https://file-examples-com.github.io/uploads/2017/10/file_example_PNG_500kB.png"
download.file(url, temp, mode = "wb")
upload_media_to_twitter(temp, token)
@dieghernan
Copy link
Author

Updated with test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment