---
title: "Basic Usage"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Basic_Usage}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---




# Introduction


Welcome to the `atrrr` vignette! This guide will delve deeper into the functionalities offered by the package-

Before diving in, let's clarify some terminology. In the context of Blue Sky, a post is referred to as a "skeet." So, when we mention retrieving or working with "skeets," we are talking about individual posts made on Blue Sky.

Now, let's delve deeper into the functionalities that allow users to interact with skeets, followers, and their descriptions.

## Installation

If you haven't done so, you can install the development version of atrrr like so (`pak` works similar to `remotes` here, but it's what the cool kids use now :grin:):

``` r
# install.packages("pak")
pak::pak("JBGruber/atrrr")
```


## Load the package


```r
library(atrrr)
```

## Authentication

The first time you make a request, you will be prompted automatically to enter your user handle and an app password to authenticate `atrrr` to communicate with BlueSky for you.

![RStudio Popup](figures/password_popup.png)

The page to generate app passwords is also automatically opened for you.

![page to create new app passwords](figures/app_password.png)

However, you can also trigger this process manually:


```r
auth("jbgruber.bsky.social")
```

This can be useful if you want to replace an old token as it is permanently stored encrypted on disk.

# Searching Posts

The AT protocol defines a search function at `app.bsky.feed.searchPosts`, however, it is not currently active, as far as we could tell.

As soon as the functionality is added by BlueSky, we will implement it here.


# Blue Sky Users and Skeets


## Retrieve user info (`get_user_info`)

A good starting point is understanding a user's digital footprint on the platform. The get_user_info function becomes our handy tool, fetching an array of information about a user. All you need? Their handle!



```r
get_user_info(actor = "atpr.bsky.social")  |>
  dplyr::glimpse()
#> Rows: 1
#> Columns: 8
#> $ did               <chr> "did:plc:j42kj4qc5vfz344weyw…
#> $ indexed_at        <dttm> NA
#> $ actor_handle      <chr> "atpr.bsky.social"
#> $ actor_name        <chr> NA
#> $ actor_description <chr> NA
#> $ actor_avatar      <chr> NA
#> $ viewer_data       <list> [FALSE, FALSE, "at://did:pl…
#> $ labels_data       <list> []
```

This function gives us counts of followers, follows, posts for each user.

You can also retrieve information for multiple users by providing a vector of user handles:


```r
get_user_info(actor = c("benguinaudeau.bsky.social", "atpr.bsky.social"))  |>
  dplyr::glimpse()
#> Rows: 2
#> Columns: 8
#> $ did               <chr> "did:plc:vuvsifrusnjsys7mhkp…
#> $ indexed_at        <dttm> 2023-09-19 00:08:33, NA
#> $ actor_handle      <chr> "benguinaudeau.bsky.social"…
#> $ actor_name        <chr> "Benjamin Guinaudeau", NA
#> $ actor_description <chr> "Postdoc @CSMaP_NYU. \n\nPol…
#> $ actor_avatar      <chr> "https://cdn.bsky.app/img/av…
#> $ viewer_data       <list> [FALSE, FALSE, "at://did:plc…
#> $ labels_data       <list> [], []
```


## Retrieve Skeets (`get_skeets_authored_by`)

To fetch all the skeets by a specific user, use the `get_skeets_authored_by` function. *Note this also includes quote skeets and reskeets.* You can also opt not to parse the result by setting `parse = FALSE`, however it is recommended to use the default parse option which results in a (more) tidy tibble.


```r
get_skeets_authored_by(actor = "benguinaudeau.bsky.social", parse = TRUE) |>
  dplyr::glimpse()
#> Rows: 25
#> Columns: 16
#> $ uri           <chr> "at://did:plc:hwbpa3fiiv6mmqkrma…
#> $ cid           <chr> "bafyreidvg65cygp7h3dtyn64tiyic5…
#> $ author_handle <chr> "fortunato.bsky.social", "mathie…
#> $ author_name   <chr> "David Fortunato", "mathieu gall…
#> $ text          <chr> "Inspired by the monotony of my …
#> $ author_data   <list> ["did:plc:hwbpa3fiiv6mmqkrmaxck…
#> $ post_data     <list> ["Inspired by the monotony of m…
#> $ embed_data    <list> <NULL>, ["app.bsky.embed.images…
#> $ reply_count   <int> 5, 0, 1, 7, 0, 1, 0, 1, 0, 0, 0,…
#> $ repost_count  <int> 15, 2, 1, 366, 3, 1, 4, 28, 0, 0…
#> $ like_count    <int> 43, 6, 1, 498, 8, 3, 10, 34, 0, …
#> $ indexed_at    <dttm> 2023-12-23 22:08:11, 2023-12-24…
#> $ in_reply_to   <chr> NA, "at://did:plc:cy3nxnkuk6qvib…
#> $ in_reply_root <chr> NA, "at://did:plc:cy3nxnkuk6qvib…
#> $ quotes        <chr> NA, NA, "at://did:plc:3mcek454ld…
#> $ is_reskeet    <lgl> TRUE, TRUE, FALSE, TRUE, FALSE, …
```

Feeling adventurous? Increase the `limit` and explore more of their content.


```r
get_skeets_authored_by(actor = "favstats.bsky.social", limit = 30) |>
  dplyr::glimpse()
#> Rows: 30
#> Columns: 16
#> $ uri           <chr> "at://did:plc:ks244q2qaqc5r3u5uf…
#> $ cid           <chr> "bafyreif6y3lcz7vu25mnpeaw72qs2l…
#> $ author_handle <chr> "claesdevreese.bsky.social", "ju…
#> $ author_name   <chr> "Claes de Vreese", "Judith Moell…
#> $ text          <chr> "Training material matters. Both…
#> $ author_data   <list> ["did:plc:ks244q2qaqc5r3u5uftv4…
#> $ post_data     <list> ["Training material matters. Bo…
#> $ embed_data    <list> ["app.bsky.embed.external#view"…
#> $ reply_count   <int> 0, 0, 3, 0, 1, 1, 1, 0, 1, 1, 2,…
#> $ repost_count  <int> 9, 2, 5, 0, 0, 0, 0, 0, 7, 0, 0,…
#> $ like_count    <int> 23, 14, 22, 0, 4, 0, 0, 0, 14, 1…
#> $ indexed_at    <dttm> 2023-12-27 18:01:09, 2023-12-21…
#> $ in_reply_to   <chr> NA, NA, NA, "at://did:plc:46yyak…
#> $ in_reply_root <chr> NA, NA, NA, "at://did:plc:46yyak…
#> $ quotes        <chr> NA, NA, NA, "at://did:plc:46yyak…
#> $ is_reskeet    <lgl> TRUE, TRUE, TRUE, FALSE, FALSE, …
```


## Who liked your Skeets?

The get_likes function allows you to extract detailed information about users who liked a specific post. By simply providing the post's URL, you can get a list of users who expressed their appreciation for the content.




```r
get_likes("https://bsky.app/profile/ryanlcooper.com/post/3kb42gayda52t")  |>
  dplyr::glimpse()
#> Rows: 25
#> Columns: 5
#> $ created_at   <dttm> 2023-12-19 07:52:16, 2023-11-15 …
#> $ indexed_at   <dttm> 2023-12-19 07:52:17, 2023-11-15 …
#> $ actor_handle <chr> "vodkula.bsky.social", "giw.bsky.…
#> $ actor_name   <chr> "Oops", "Gareth Illmann-Walker", …
#> $ actor_data   <list> ["did:plc:q3fuk5znsrhw6twh3mdo3i…
```

## Who reposted your Skeets?

Reposts (or reskeets), akin to retweets on platforms like Twitter, amplify the reach of content by sharing it with a wider audience.

The `get_reposts` function is designed to retrieve information about users who reposted a specific piece of content.


```r
get_reposts(post_url = "https://bsky.app/profile/ryanlcooper.com/post/3kb42gayda52t")  |>
  dplyr::glimpse()
#> Rows: 25
#> Columns: 8
#> $ did               <chr> "did:plc:rkx7ioruvfkr7ure6ep…
#> $ indexed_at        <dttm> 2023-11-08 08:02:57, 2023-1…
#> $ actor_handle      <chr> "giw.bsky.social", "lawless5…
#> $ actor_name        <chr> "Gareth Illmann-Walker", "la…
#> $ actor_description <chr> "YamYam, White Hat open sour…
#> $ actor_avatar      <chr> "https://cdn.bsky.app/img/av…
#> $ viewer_data       <list> [FALSE, FALSE], [FALSE, FAL…
#> $ labels_data       <list> [], [["did:plc:s3or3peikxeb…
```



# Exploring Connections: Get Followers and Follows

You can retrieve a list of followers and those followed by a user through the `get_followers` and `get_follows` functions, respectively. This gives a glimpse of their social dynamics on the platform.

## `get_followers`


```r
get_followers(actor = "benguinaudeau.bsky.social", limit = 200)  |>
  dplyr::glimpse()
#> Rows: 220
#> Columns: 11
#> $ did                <chr> "did:plc:w2gsbanugiw67kud7s…
#> $ handle             <chr> "mattgraham.bsky.social", "…
#> $ display_name       <chr> "Matt Graham", "Jan P. Vogl…
#> $ description        <chr> "Political Scientist. Belie…
#> $ avatar             <chr> "https://cdn.bsky.app/img/a…
#> $ indexed_at         <chr> "2023-07-31T20:08:07.007Z",…
#> $ viewer_muted       <lgl> FALSE, FALSE, FALSE, FALSE,…
#> $ viewer_blocked_by  <lgl> FALSE, FALSE, FALSE, FALSE,…
#> $ labels             <named list> <NULL>, <NULL>, <NUL…
#> $ viewer_following   <chr> NA, NA, NA, NA, NA, NA, NA,…
#> $ viewer_followed_by <chr> NA, NA, NA, NA, NA, NA, NA,…
```


## `get_follows`


```r
atrrr:::get_follows(actor = "benguinaudeau.bsky.social", limit = 200)  |>
  dplyr::glimpse()
#> Rows: 187
#> Columns: 11
#> $ did                <chr> "did:plc:bgnw6liy2b3bto6tkw…
#> $ handle             <chr> "julienrobin.bsky.social", …
#> $ display_name       <chr> "Julien Robin", "Marc-Antoi…
#> $ description        <chr> "PhD candidate à l’Universi…
#> $ avatar             <chr> "https://cdn.bsky.app/img/a…
#> $ indexed_at         <chr> "2023-12-22T19:12:22.412Z",…
#> $ viewer_muted       <lgl> FALSE, FALSE, FALSE, FALSE,…
#> $ viewer_blocked_by  <lgl> FALSE, FALSE, FALSE, FALSE,…
#> $ viewer_following   <chr> NA, NA, NA, NA, NA, NA, NA,…
#> $ viewer_followed_by <chr> NA, NA, NA, NA, NA, NA, NA,…
#> $ labels             <named list> <NULL>, <NULL>, <NUL…
```

You'll notice each follower/following has a description (i.e. their bio). It's a nifty way to get to know a bit about them at a glance!


## Follower Network Visualization

This section guides you through the process of visualizing a follower network for a given user on Blue Sky. The primary focus is on the "skeets" (posts) and the interconnected relationships among followers.

The first step is to load some additional packages. Then we get the followers for the main user we are interested in. We will then delve deeper and fetch the followers for each of these followers.


```r
library(ggplot2)
library(dplyr)
library(purrr)
library(tidyr)
library(igraph)
library(ggraph)
library(tidygraph)

# Retrieve the followers for the main user
some_followers <- get_followers(actor = "benguinaudeau.bsky.social", limit = 10)$handle

# For each follower, retrieve their own set of followers.
# This provides a nested view of relationships.
followers_of_followers <- some_followers |>
  purrr::map_dfr(~{
    get_followers(actor = .x, limit = 200) |>
    mutate(from = .x)
  }) |>
  dplyr::rename(to = handle) |>
  dplyr::select(from, to)
```


## Building and Visualizing the Follower Network


```r
# Construct the network graph and plot
graph <- igraph::graph_from_data_frame(followers_of_followers, directed = TRUE)

# Use ggraph to visualize the network.
ggraph::ggraph(graph, layout = "kk") +
  ggraph::geom_edge_link() +
  ggraph::geom_node_point(aes(size = tidygraph::centrality_pagerank())) +
  ggplot2::theme_void()
#> Warning: Using the `size` aesthetic in this geom was deprecated
#> in ggplot2 3.4.0.
#> ℹ Please use `linewidth` in the `default_aes` field and
#>   elsewhere instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see
#> where this warning was generated.
```

<img src="figures/unnamed-chunk-12-1.png" alt="Follower Network" width="100%" />


## Find people to follow

You can use a similar approach to find new people you might want to follow.
First get all the people you already follow by quering your own account:


```r
my_follows <- get_follows(actor = "jbgruber.bsky.social",
                              limit = 10)$handle # limit only for demonstration

# For each account you follow, retrieve who they follow.
follows_of_follows <- my_follows |>
  purrr::map_dfr(~{
    get_followers(actor = .x, limit = 100) |>
      mutate(from = .x)
  })

# Now we can check which accounts are popular among the people you already follow
follows_of_follows |>
  dplyr::filter(!handle %in% my_follows) |> # exclude accounts you already follow
  dplyr::count(display_name, handle, sort = TRUE)
#> # A tibble: 591 × 3
#>    display_name         handle                         n
#>    <chr>                <chr>                      <int>
#>  1 "Chris MacQuarrie "  cmacquar.bsky.social          12
#>  2  <NA>                sedmaar.bsky.social           12
#>  3 "Johannes B. Gruber" jbgruber.bsky.social          10
#>  4 "Alan Jackson"       akjackson.bsky.social          6
#>  5 "Aleksandra Urman"   aurman21.bsky.social           6
#>  6 "Alexandra Boutros"  alexandraboutros.bsky.soc…     6
#>  7 "Andy Carr"          andycarr.bsky.social           6
#>  8 "Daniel Moccand"     moccand.bsky.social            6
#>  9 "Dominar Rygel XVI"  oaklandishdude.bsky.social     6
#> 10 "Dr. Ashok Karra"    akarra.bsky.social             6
#> # ℹ 581 more rows
```

The original account shows up in this overview, as do accounts that were excluded because of the limit set to do the demonstration.

But all of these accounts look intresting!




<!-- ### Eventual temporal structure -->
