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

Commit

Permalink
get_friends return 2 columns even with protected accounts.
Browse files Browse the repository at this point in the history
Closes #341
  • Loading branch information
llrs committed May 5, 2022
1 parent 6dbc52c commit 5dd07d8
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ auth_as <- function(auth = NULL) {

find_auth <- function(auth = NULL) {
if (is.null(auth)) {
developing <- is_testing() || is_dev_mode() %||% is_rcmd_check()
developing <- is_testing() || (is_dev_mode() %||% is_rcmd_check())
if (developing) {
rtweet_test() %||% no_token()
} else{
Expand Down
6 changes: 6 additions & 0 deletions R/friends.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' Generally, you should not need to set `n` to more than 5,000 since Twitter
#' limits the number of people that you can follow (i.e. to follow more than
#' 5,000 people at least 5,000 people need to follow you).
#'
#' @note If a user is protected the API will omit all requests so you'll need
#' to find which user is protected. rtweet will warn you and the output will be `NA`.
#'
#' @inheritParams TWIT_paginate_cursor
#' @inheritParams get_followers
Expand Down Expand Up @@ -74,6 +77,9 @@ get_friends_user <- function(user, token, ..., parse = TRUE) {
to_id = unlist(lapply(results, function(x) x$ids),
recursive = FALSE, use.names = FALSE)
)
if (ncol(df) == 1) {
df$to_id <- NA
}
results <- copy_cursor(df, results)
}
results
Expand Down
4 changes: 4 additions & 0 deletions man/get_friends.Rd

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

97 changes: 97 additions & 0 deletions tests/fixtures/friends8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
http_interactions:
- request:
method: get
uri: https://api.twitter.com/1.1/friends/ids.json?stringify_ids=TRUE&screen_name=fdrennan&count=5000&cursor=-1
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: My oauth token is safe
response:
status:
status_code: 401
category: Client error
reason: Unauthorized
message: 'Client error: (401) Unauthorized'
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: '89'
content-type: application/json;charset=utf-8
date: Thu, 05 May 2022 18:52:19 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Thu, 05 May 2022 18:52:19 GMT
pragma: no-cache
server: tsa_f
set-cookie:
- lang=en; Path=/
- guest_id=v1%3A165177673902471158; Max-Age=34214400; Expires=Mon, 05 Jun 2023
18:52:19 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
status: 401 Unauthorized
strict-transport-security: max-age=631138519
www-authenticate: OAuth realm="https://api.twitter.com"
x-access-level: read-write-directmessages
x-connection-hash: 2b9a9d3098f94075c7e6162cff7ebca494d4a22128767de1a6194dcb7ba196cf
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-rate-limit-limit: '15'
x-rate-limit-remaining: '12'
x-rate-limit-reset: '1651777601'
x-response-time: '121'
x-transaction: 6f41784a09aa6c9f
x-twitter-response-tags: BouncerCompliant
x-xss-protection: '0'
body:
encoding: UTF-8
file: no
string: '{"request":"\/1.1\/friends\/ids.json","error":"Not authorized."}'
recorded_at: 2022-05-05 18:52:19 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
- request:
method: get
uri: https://api.twitter.com/1.1/friends/ids.json?stringify_ids=TRUE&screen_name=Lluis_Revilla&count=5000&cursor=-1
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: My oauth token is safe
response:
status:
status_code: 401
category: Client error
reason: Unauthorized
message: 'Client error: (401) Unauthorized'
headers:
date: Thu, 05 May 2022 18:52:19 GMT
pragma: no-cache
server: tsa_f
status: 401 Unauthorized
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, 05 May 2022 18:52:19 GMT
x-transaction: 7e4a452074537ad2
content-length: '89'
x-access-level: read-write-directmessages
x-frame-options: SAMEORIGIN
content-encoding: gzip
www-authenticate: OAuth realm="https://api.twitter.com"
x-xss-protection: '0'
x-rate-limit-limit: '15'
x-rate-limit-reset: '1651777601'
content-disposition: attachment; filename=json.json
x-content-type-options: nosniff
x-rate-limit-remaining: '11'
x-twitter-response-tags: BouncerCompliant
strict-transport-security: max-age=631138519
x-response-time: '118'
x-connection-hash: 2b9a9d3098f94075c7e6162cff7ebca494d4a22128767de1a6194dcb7ba196cf
body:
encoding: UTF-8
file: no
string: '{"request":"\/1.1\/friends\/ids.json","error":"Not authorized."}'
recorded_at: 2022-05-05 18:52:19 GMT
recorded_with: vcr/1.0.2, webmockr/0.8.0
230 changes: 230 additions & 0 deletions tests/fixtures/get_followers2.yml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/testthat/test-friends.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ test_that("n = Inf works", {
})
expect_s3_class(mf, "data.frame")
})

vcr::use_cassette("friends8", {
test_that("n = Inf works", {
expect_warning(
expect_warning(
gf <- rtweet::get_friends(c("fdrennan", "Lluis_Revilla")),
"Skipping unauthorized account: fdrennan")
)
expect_s3_class(gf, "data.frame")
expect_true(ncol(gf) == 2)
expect_true(all(is.na(gf$to_id)))
})
})

0 comments on commit 5dd07d8

Please sign in to comment.