Skip to content

Pagination

David Schoch edited this page Dec 19, 2022 · 14 revisions

This page only applies for the CRAN version 0.1.0
The dev version on github 0.1.0.9000 supports pagination automatically


Unlike many packages, this package doesn't support auto-pagination (yet). It is because

  1. Auto-pagination can incur a lot of API calls to Mastodon instances (most of them are community-run).

However, if you really need to get more than one page of results, you can do it manually. Functions with max_id and limit support pagination.

For example, the following gets 4 pages from get_hashtag_timeline. The idea is to make the oldest status ID the max_id of the next call.

require(rtoot)
#> Loading required package: rtoot
## Most instances won't support `limit` > 40

max_id <- NULL
output <- tibble::tibble()
for (i in 1:4) {
    res <- get_timeline_hashtag(hashtag = "ichbinhanna", max_id = max_id,
                                limit = 40, instance = "mastodon.social")
    output <- rbind(res, output)
    max_id <- res$id[res$created_at == min(res$created_at)]
    Sys.sleep(5)
}
output
#> # A tibble: 160 × 29
#>    id          uri   created_at          content visib…¹ sensi…² spoil…³ reblo…⁴
#>    <chr>       <chr> <dttm>              <chr>   <chr>   <lgl>   <chr>     <int>
#>  1 1083760406… http… 2022-05-27 21:32:23 "<p>4 … public  FALSE   ""            0
#>  2 1083704851… http… 2022-05-26 21:59:32 "<p>On… public  FALSE   ""            0
#>  3 1083580751… http… 2022-05-24 17:22:51 "<p>Eb… public  FALSE   ""            0
#>  4 1083573640… http… 2022-05-24 14:22:41 "<p>No… public  FALSE   ""            0
#>  5 1083519226… http… 2022-05-23 15:18:51 "<p>An… public  FALSE   ""            3
#>  6 1083346039… http… 2022-05-20 13:53:56 "<p><s… public  FALSE   ""            0
#>  7 1083342732… http… 2022-05-20 12:30:22 "<p>Da… public  FALSE   ""            0
#>  8 1083336272… http… 2022-05-20 09:46:05 "<p>Di… public  FALSE   ""            0
#>  9 1083333308… http… 2022-05-20 08:30:43 "<p>Li… public  FALSE   ""            0
#> 10 1083176989… http… 2022-05-17 14:15:19 "<p>Ur… public  FALSE   ""            2
#> # … with 150 more rows, 21 more variables: favourites_count <int>,
#> #   replies_count <int>, url <chr>, in_reply_to_id <chr>,
#> #   in_reply_to_account_id <chr>, language <chr>, text <lgl>,
#> #   application <I<list>>, poll <I<list>>, card <I<list>>, account <list>,
#> #   reblog <I<list>>, media_attachments <I<list>>, mentions <I<list>>,
#> #   tags <list>, emojis <I<list>>, favourited <lgl>, reblogged <lgl>,
#> #   muted <lgl>, bookmarked <lgl>, pinned <lgl>, and abbreviated variable …

Created on 2022-11-10 by the reprex package (v2.0.1)

Followers

For some functions, the max_id is not as straightforward as for statuses. This mostly concerns calls to functions that involve account level calls, such as get_followers(). The max_id in this case is hidden in the header of the response of the API call. For now, these parameters are returned as an attribute of the output. The example below shows how to use it to paginate through the followers of a user.

library(rtoot)
id <- "109302436954721982"
followers <- tibble::tibble()
max_id <- NULL
for(i in 1:3){
  fol <- get_account_followers(id,max_id = max_id)
  max_id <- attr(fol,"headers")$max_id  
  followers <- dplyr::bind_rows(followers,fol)
}
followers
#> # A tibble: 120 × 21
#>    id       usern…¹ acct  displ…² locked bot   disco…³ group created_at         
#>    <chr>    <chr>   <chr> <chr>   <lgl>  <lgl> <lgl>   <lgl> <dttm>             
#>  1 1093122… meddhi… medd… "Moham… FALSE  FALSE TRUE    FALSE 2022-11-06 00:00:00
#>  2 1093120… wrah… wrah… "Subha… FALSE  FALSE FALSE   FALSE 2020-08-23 00:00:00
#>  3 1093113… jhu…   jhur… "Jan H… FALSE  FALSE FALSE   FALSE 2022-11-09 00:00:00
#>  4 1093075… federi… fede… ""      FALSE  FALSE FALSE   FALSE 2022-11-08 00:00:00
#>  5 1092980… ia4…   ia4eo "ia4eo" FALSE  FALSE FALSE   FALSE 2022-11-06 00:00:00
#>  6 1093013… neocar… neoc… "Nicol… FALSE  FALSE TRUE    FALSE 2022-11-05 00:00:00
#>  7 1093099… huckle… huck… "Huckl… FALSE  FALSE TRUE    FALSE 2022-04-18 00:00:00
#>  8 1092653… sam…   sami… "Samin… FALSE  FALSE TRUE    FALSE 2022-10-31 00:00:00
#>  9 1093089… migdal… migd… "migda… FALSE  FALSE FALSE   FALSE 2022-11-04 00:00:00
#> 10 1093086… Ju…    JuKo… "Ju…"  TRUE   FALSE TRUE    FALSE 2022-11-07 00:00:00
#> # … with 110 more rows, 12 more variables: note <chr>, url <chr>, avatar <chr>,
#> #   avatar_static <chr>, header <chr>, header_static <chr>,
#> #   followers_count <int>, following_count <int>, statuses_count <int>,
#> #   last_status_at <dttm>, fields <I<list>>, emojis <I<list>>, and abbreviated
#> #   variable names ¹​username, ²​display_name, ³​discoverable

Created on 2022-11-09 with reprex v2.0.2

Clone this wiki locally