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

Feature request: add function for "GET friendships/show" #138

Closed
fschaffner opened this issue Nov 15, 2017 · 3 comments
Closed

Feature request: add function for "GET friendships/show" #138

fschaffner opened this issue Nov 15, 2017 · 3 comments

Comments

@fschaffner
Copy link

May I ask if you could add a function to get the information about the relationship between two users, as available trough the GET friendships/show query?

@mkearney
Copy link
Collaborator

So, @fschaff, your post made me realize that when I experimented with what I thought was this API in the past, I was actually using the GET friendships/lookup API...which explains why I was so disappointed with the results.

Anyway, I was able to throw something together in time for a CRAN release. So there should be at least somewhat reasonable access to friendships/show via the lookup_friendships() function–and, bonus, it's even on CRAN now!

Required inputs are source and target (can be screen names or user IDs). If only one user is provided to either source or target, then any number of users–save Twitter API rate limits–can be passed in the other parameter.

fds <- lookup_friendships(
  source = "realDonaldTrump",
  target = c("DRUDGE_REPORT", "seanhannity", "HuffPost", "maddow", "cnn")
)
> fds
# A tibble: 60 x 4
   relationship            user       variable           value
          <chr>           <chr>          <chr>           <chr>
 1       source   DRUDGE_REPORT             id        25073877
 2       source   DRUDGE_REPORT         id_str        25073877
 3       source   DRUDGE_REPORT    screen_name realDonaldTrump
 4       source   DRUDGE_REPORT      following            TRUE
 5       source   DRUDGE_REPORT    followed_by           FALSE
 6       source   DRUDGE_REPORT live_following           FALSE
 7       source   DRUDGE_REPORT         can_dm           FALSE
 8       target realDonaldTrump             id        14669951
 9       target realDonaldTrump         id_str        14669951
10       target realDonaldTrump    screen_name   DRUDGE_REPORT
# ... with 50 more rows
> 

It's also possible to specify more than one user in both source and target parameters. However, if the length of both parameters is greater than 1, then the vectors must be of equal length.

## vector of multiple users
usrs <- c(
  "realDonaldTrump", "DRUDGE_REPORT", "seanhannity", "HuffPost", "maddow", "cnn"
)
fds <- lookup_friendships(
  source = usrs, 
  target = usrs
)
> fds
# A tibble: 72 x 4
   relationship            user       variable           value
          <chr>           <chr>          <chr>           <chr>
 1       source realDonaldTrump             id        25073877
 2       source realDonaldTrump         id_str        25073877
 3       source realDonaldTrump    screen_name realDonaldTrump
 4       source realDonaldTrump      following           FALSE
 5       source realDonaldTrump    followed_by           FALSE
 6       source realDonaldTrump live_following           FALSE
 7       source realDonaldTrump         can_dm            TRUE
 8       target realDonaldTrump             id        25073877
 9       target realDonaldTrump         id_str        25073877
10       target realDonaldTrump    screen_name realDonaldTrump
# ... with 62 more rows

I haven't had a lot of time to test this. If you run into problems, try setting parse = FALSE. You'll be left to wrangle the returned data yourself, but it might help avoid errors in internal parsing.

> str(fds, 3)
List of 6
 $ realDonaldTrump:List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ DRUDGE_REPORT  :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ seanhannity    :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ HuffPost       :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ maddow         :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ cnn            :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7

@fschaffner
Copy link
Author

Thank you for your quick update on this @mkearney! The examples above work for me. Two small suggestions:

  • change the name of the function to show_friendships(), in order to avoid confusion on the part of other users as to which API this function uses (lookup_friendships sounds too much like it uses "GET friendships/lookup")
  • perhaps find a way to make the output data frame a bit more tidy

Cheers!

@mkearney
Copy link
Collaborator

@fschaff I realized the name issue shortly after I submitted the pkg to CRAN. Oopsies! As for tidying up the data...I agree. It wouldn't be a bad place to jump in if you're looking for an excuse to make a contribution!

@llrs llrs mentioned this issue Feb 15, 2021
@llrs llrs closed this as completed Apr 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants