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

Commit

Permalink
Fixed a logical length 1 in check_media.
Browse files Browse the repository at this point in the history
  • Loading branch information
llrs committed Jul 28, 2022
1 parent 1c81fcb commit 6879238
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rtweet
Title: Collecting Twitter Data
Version: 1.0.2.9000
Version: 1.0.2.9001
Authors@R: c(
person("Michael W.", "Kearney", , "[email protected]", role = c("aut"),
comment = c(ORCID = "0000-0002-0730-4694")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rtweet (development version)

- Fixed a logical length 1 in check_media.

# rtweet 1.0.2

- Exported again `tweets_with_users` and `users_with_tweets` because Twitmo depends on them.
Expand Down
252 changes: 252 additions & 0 deletions tests/fixtures/upload_multiple_media_to_twitter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
http_interactions:
- request:
method: post
uri: https://upload.twitter.com/1.1/media/upload.json
body:
encoding: ''
string: media=list(path = "/home/lluis/Documents/Projects/rtweet/tests/testthat/testing_plot.png",
type = "image/png")
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: My oauth token is safe
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-encoding: gzip
content-length: '146'
content-type: application/json;charset=utf-8
date: Thu, 28 Jul 2022 22:45:06 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Thu, 28 Jul 2022 22:45:06 GMT
pragma: no-cache
server: tsa_f
set-cookie:
- lang=en; Path=/
- guest_id=v1%3A165904830627017873; Max-Age=34214400; Expires=Mon, 28 Aug 2023
22:45:06 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
status: 200 OK
strict-transport-security: max-age=631138519
vary: Origin
x-access-level: read-write-directmessages
x-connection-hash: 63615f9f03b9966f627ebd4764c82603461f0f42b7c1bafdaf6d89dd153bb2ea
x-frame-options: SAMEORIGIN
x-rate-limit-limit: '415'
x-rate-limit-remaining: '414'
x-rate-limit-reset: '1659051906'
x-response-time: '765'
x-transaction: 7ef003017dd7e251
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block
body:
encoding: UTF-8
file: no
string: '{"media_id":1552787257689620480,"media_id_string":"1552787257689620480","size":48270,"expires_after_secs":86400,"image":{"image_type":"image\/png","w":2818,"h":1890}}'
recorded_at: 2022-07-28 22:45:09 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
- request:
method: post
uri: https://upload.twitter.com/1.1/media/metadata/create.json
body:
encoding: ''
string: '{"media_id":"1552787257689620480","alt_text":{"text":"mp4 for testing"}}'
headers:
Accept: application/json, text/xml, application/xml, */*
Content-Type: application/json
Authorization: My oauth token is safe
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
date: Thu, 28 Jul 2022 22:45:07 GMT
vary: Origin
pragma: no-cache
server: tsa_f
status: 200 OK
expires: Tue, 31 Mar 1981 05:00:00 GMT
content-type: text/html;charset=utf-8
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
last-modified: Thu, 28 Jul 2022 22:45:07 GMT
x-transaction: a0e308668d28b510
content-length: '0'
x-access-level: read-write-directmessages
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-rate-limit-limit: '2000'
x-rate-limit-reset: '1659051907'
x-rate-limit-remaining: '1999'
content-security-policy: default-src 'self'; connect-src 'self'; font-src 'self'
https://*.twimg.com https://twitter.com https://ton.twitter.com data:; frame-src
'self' https://*.twimg.com https://twitter.com https://ton.twitter.com; img-src
'self' https://*.twimg.com https://twitter.com https://ton.twitter.com data:;
media-src 'self' https://*.twimg.com https://twitter.com https://ton.twitter.com;
object-src 'none'; script-src 'self' https://*.twimg.com https://twitter.com
https://ton.twitter.com; style-src 'self' https://*.twimg.com https://twitter.com
https://ton.twitter.com; report-uri https://twitter.com/i/csp_report?a=OBZG6ZTJNRSWE2LSMQ%3D%3D%3D%3D%3D%3D&ro=false;
x-twitter-response-tags: BouncerCompliant
strict-transport-security: max-age=631138519
x-response-time: '141'
x-connection-hash: 63615f9f03b9966f627ebd4764c82603461f0f42b7c1bafdaf6d89dd153bb2ea
body:
encoding: UTF-8
file: no
string: ''
recorded_at: 2022-07-28 22:45:09 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
- request:
method: post
uri: https://upload.twitter.com/1.1/media/upload.json
body:
encoding: ''
string: media=list(path = "/home/lluis/Documents/Projects/rtweet/tests/testthat/testing_plot.jpg",
type = "image/jpeg")
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: My oauth token is safe
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
date: Thu, 28 Jul 2022 22:45:08 GMT
vary: Origin
pragma: no-cache
server: tsa_f
status: 200 OK
expires: Tue, 31 Mar 1981 05:00:00 GMT
content-type: application/json;charset=utf-8
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
last-modified: Thu, 28 Jul 2022 22:45:08 GMT
x-transaction: 07a02aade8661436
content-length: '148'
x-access-level: read-write-directmessages
x-frame-options: SAMEORIGIN
content-encoding: gzip
x-xss-protection: 1; mode=block
x-rate-limit-limit: '415'
x-rate-limit-reset: '1659051906'
content-disposition: attachment; filename=json.json
x-rate-limit-remaining: '413'
x-twitter-response-tags: BouncerCompliant
strict-transport-security: max-age=631138519
x-response-time: '818'
x-connection-hash: 63615f9f03b9966f627ebd4764c82603461f0f42b7c1bafdaf6d89dd153bb2ea
body:
encoding: UTF-8
file: no
string: '{"media_id":1552787263641247749,"media_id_string":"1552787263641247749","size":223180,"expires_after_secs":86400,"image":{"image_type":"image\/jpeg","w":2818,"h":1890}}'
recorded_at: 2022-07-28 22:45:09 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
- request:
method: post
uri: https://upload.twitter.com/1.1/media/metadata/create.json
body:
encoding: ''
string: '{"media_id":"1552787263641247749","alt_text":{"text":"gif for testing"}}'
headers:
Accept: application/json, text/xml, application/xml, */*
Content-Type: application/json
Authorization: My oauth token is safe
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
date: Thu, 28 Jul 2022 22:45:08 GMT
vary: Origin
pragma: no-cache
server: tsa_f
status: 200 OK
expires: Tue, 31 Mar 1981 05:00:00 GMT
content-type: text/html;charset=utf-8
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
last-modified: Thu, 28 Jul 2022 22:45:08 GMT
x-transaction: 73c4994bf16d58b4
content-length: '0'
x-access-level: read-write-directmessages
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-rate-limit-limit: '2000'
x-rate-limit-reset: '1659051907'
x-rate-limit-remaining: '1998'
content-security-policy: default-src 'self'; connect-src 'self'; font-src 'self'
https://*.twimg.com https://twitter.com https://ton.twitter.com data:; frame-src
'self' https://*.twimg.com https://twitter.com https://ton.twitter.com; img-src
'self' https://*.twimg.com https://twitter.com https://ton.twitter.com data:;
media-src 'self' https://*.twimg.com https://twitter.com https://ton.twitter.com;
object-src 'none'; script-src 'self' https://*.twimg.com https://twitter.com
https://ton.twitter.com; style-src 'self' https://*.twimg.com https://twitter.com
https://ton.twitter.com; report-uri https://twitter.com/i/csp_report?a=OBZG6ZTJNRSWE2LSMQ%3D%3D%3D%3D%3D%3D&ro=false;
x-twitter-response-tags: BouncerCompliant
strict-transport-security: max-age=631138519
x-response-time: '104'
x-connection-hash: 63615f9f03b9966f627ebd4764c82603461f0f42b7c1bafdaf6d89dd153bb2ea
body:
encoding: UTF-8
file: no
string: ''
recorded_at: 2022-07-28 22:45:09 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
- request:
method: post
uri: https://api.twitter.com/1.1/statuses/update.json?status=status message&media_ids=1552787257689620480%2C1552787263641247749
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Content-Type: ''
Authorization: My oauth token is safe
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-encoding: gzip
content-length: '1113'
content-type: application/json;charset=utf-8
date: Thu, 28 Jul 2022 22:45:09 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Thu, 28 Jul 2022 22:45:09 GMT
pragma: no-cache
server: tsa_f
set-cookie:
- lang=en; Path=/
- guest_id=v1%3A165904830883196603; Max-Age=34214400; Expires=Mon, 28 Aug 2023
22:45:09 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read-write-directmessages
x-connection-hash: cfd0f0e05d901b98d1267724a08b2b550bcbcedf2ebd1ead3c333efabf4005e9
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: '509'
x-transaction: f73ae97d5b9e627a
x-twitter-response-tags: BouncerCompliant
x-xss-protection: '0'
body:
encoding: UTF-8
file: no
string: '{"created_at":"Thu Jul 28 22:45:08 +0000 2022","id":1552787268439515136,"id_str":"1552787268439515136","text":"Testing
multiple media https:\/\/t.co\/mnG83IcZve","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[],"media":[{"id":1552787257689620480,"id_str":"1552787257689620480","indices":[23,46],"media_url":"http:\/\/pbs.twimg.com\/media\/FYybyKpXwAAphdo.png","media_url_https":"https:\/\/pbs.twimg.com\/media\/FYybyKpXwAAphdo.png","url":"https:\/\/t.co\/mnG83IcZve","display_url":"pic.twitter.com\/mnG83IcZve","expanded_url":"https:\/\/twitter.com\/rtweet_test\/status\/1552787268439515136\/photo\/1","type":"photo","sizes":{"medium":{"w":1200,"h":805,"resize":"fit"},"small":{"w":680,"h":456,"resize":"fit"},"large":{"w":2048,"h":1374,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"}}}]},"extended_entities":{"media":[{"id":1552787257689620480,"id_str":"1552787257689620480","indices":[23,46],"media_url":"http:\/\/pbs.twimg.com\/media\/FYybyKpXwAAphdo.png","media_url_https":"https:\/\/pbs.twimg.com\/media\/FYybyKpXwAAphdo.png","url":"https:\/\/t.co\/mnG83IcZve","display_url":"pic.twitter.com\/mnG83IcZve","expanded_url":"https:\/\/twitter.com\/rtweet_test\/status\/1552787268439515136\/photo\/1","type":"photo","sizes":{"medium":{"w":1200,"h":805,"resize":"fit"},"small":{"w":680,"h":456,"resize":"fit"},"large":{"w":2048,"h":1374,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"}}},{"id":1552787263641247749,"id_str":"1552787263641247749","indices":[23,46],"media_url":"http:\/\/pbs.twimg.com\/media\/FYybyg0WYAUblpH.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/FYybyg0WYAUblpH.jpg","url":"https:\/\/t.co\/mnG83IcZve","display_url":"pic.twitter.com\/mnG83IcZve","expanded_url":"https:\/\/twitter.com\/rtweet_test\/status\/1552787268439515136\/photo\/1","type":"photo","sizes":{"large":{"w":2048,"h":1374,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":456,"resize":"fit"},"medium":{"w":1200,"h":805,"resize":"fit"}}}]},"source":"\u003ca
href=\"https:\/\/github.com\/ropensci\/rtweet\" rel=\"nofollow\"\u003ertweet_test_ci\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1365671359137873920,"id_str":"1365671359137873920","name":"rtweet-test","screen_name":"rtweet_test","location":"","description":"Testing
bot for rtweet","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":2,"listed_count":0,"created_at":"Sat
Feb 27 14:33:58 +0000 2021","favourites_count":12,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":999,"lang":null,"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_normal.png","profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_normal.png","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":true,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none","withheld_in_countries":[]},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}'
recorded_at: 2022-07-28 22:45:09 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
40 changes: 27 additions & 13 deletions tests/testthat/test-post-tweet.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
test_that("upload_media_to_twitter() can handle small file", {

# gif - unchunk 1MB
vcr::use_cassette("upload_media_to_twitter1", {
id <- upload_media_to_twitter(test_path("tweet.gif"), chunk_size = 1024 * 1024)
})
expect_type(id, "character")

# doesn't appear to be any way to get info about a media object
# after it's been uploaded

# gif - chunk 5KB
vcr::use_cassette("upload_media_to_twitter2", {
id <- upload_media_to_twitter(test_path("tweet.gif"), chunk_size = 5 * 1024)
})
expect_type(id, "character")

# mp4 - test forced chunk / small file w/ high chunk sizes
vcr::use_cassette("upload_media_to_twitter3", {
id <- upload_media_to_twitter(test_path("tweet.mp4"), chunk_size = 1024 * 1024)
})
expect_type(id, "character")
})

test_that("check_media handles well multiple files", {
media <- c("image.png", "image.png")
expect_error(check_media(media, letters[1:2]), NA)
files <- c(test_path("testing_plot.png"), test_path("testing_plot.jpg"))
vcr::use_cassette("upload_multiple_media_to_twitter", {
id <- post_tweet("Testing multiple media", media = files,
media_alt_text = c("mp4 for testing", "gif for testing"))
})

})


test_that("can set alt text", {

vcr::use_cassette("upload_media_to_twitter4", {
id <- upload_media_to_twitter(test_path("tweet.gif"), alt_text = "A bird tweeting")
})
Expand All @@ -35,10 +46,10 @@ test_that("can set alt text", {
test_that("post_tweet works", {
skip_if_offline() # destroy id changes on each test
msg <- paste("test", Sys.time()) # To avoid having duplicated status

expect_message(pt <- post_tweet(msg), "Your tweet has been posted!")
#pt = post tweet
cpt <- httr::content(pt)
cpt <- httr::content(pt)
expect_equal(httr::status_code(pt), 200L)
expect_message(dt <- post_destroy(cpt$id_str), "Your tweet has been deleted!")
# dt = destroy tweet
Expand All @@ -47,22 +58,22 @@ test_that("post_tweet works", {

test_that("post_tweet geolocated works", {
skip_if_offline() # destroy id changes on each test

# Test geolocated tweet
msg <- paste("test geolocated", Sys.time()) # To avoid having duplicated status
expect_message(pt <- post_tweet(msg, lat = -36.811784, long = 174.792657),
expect_message(pt <- post_tweet(msg, lat = -36.811784, long = 174.792657),
"Your tweet has been posted!")
cpt <- httr::content(pt)
expect_message(dt <- post_destroy(cpt$id_str), "Your tweet has been deleted!")
expect_equal(httr::status_code(dt), 200L)

# Test display_coordinates param
msg <- paste("test geolocated", Sys.time()) # To avoid having duplicated status
expect_message(pt <- post_tweet(msg, lat = -36.811784, long = 174.792657,
display_coordinates = TRUE),
display_coordinates = TRUE),
"Your tweet has been posted!")
cpt <- httr::content(pt)

expect_message(dt <- post_destroy(cpt$id_str), "Your tweet has been deleted!")
expect_equal(httr::status_code(dt), 200L)
})
Expand All @@ -71,10 +82,13 @@ test_that("Check geo-related inputs for post_tweet", {
# All these post_tweets fail
expect_snapshot(error = TRUE, {
msg <- paste("test geolocated error", Sys.time()) # To avoid having duplicated status
post_tweet(msg, lat = "x", long = 0)
post_tweet(msg, lat = "x", long = 0)
post_tweet(msg, lat = 0, long = "x")
post_tweet(msg, lat = 91, long = 0)
post_tweet(msg, lat = 0, long = 181)
post_tweet(msg, lat = 0, long = 0, display_coordinates = "error")
})
})



0 comments on commit 6879238

Please sign in to comment.