Search code examples
rapitwitterrtweet

GET friendships/show with rtweet


I tried to find a way to use the rtweet package to perform a request to GET friendships/show from the Twitter REST API, in order to analyse the relationship between two users. However, I could not make it work (I did find lookup_friendships(), but it does not do what I want). Is it possible to do that with rtweet, and if so, how?


Solution

  • This feature has been added to rtweet, so there should be at least somewhat reasonable access to the "friendships/show" API via lookup_friendships()–and, bonus, it just got accepted in version 0.6.0 now 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
    

    There hasn't been a lot of time to test this yet. If you run into problems, try setting parse = FALSE. You'll be left to wrangling the returned data by yourself, but it might help avoid errors in internal parsing.

    fds <- lookup_friendships(
          source = usrs, 
          target = usrs,
          parse = FALSE
        )
    > 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