Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Fetch Mastodon instances and users counts, generate a plot and toot
- # the image
- #
- # @milvus@mastodon.cloud - 2017-04-10
- # Setup -------------------------------------------------------------------
- # Packages (to be installed before use)
- library(tidyverse)
- library(httr)
- library(jsonlite)
- library(scales)
- library(ggthemes)
- # data source
- url <- "https://instances.mastodon.xyz/"
- # Number of instances to display
- ntop = 20
- # Your instance, login and password
- instance <- "https://mastodon.cloud/"
- user <- ""
- pass <- ""
- # Registration, uncomment below,
- # run this part once and write down client_id and client_secret,
- # comment again
- # r <- POST(paste0(instance , "api/v1/apps"),
- # body = list(client_name = "mastodon_count_plot_v2",
- # redirect_uris = "urn:ietf:wg:oauth:2.0:oob",
- # scopes = "write"))
- # stop_for_status(r)
- # apps <- content(r)
- #
- # paste("client_id :", apps[["client_id"]])
- # paste("client_secret", apps[["client_secret"]])
- client_id <- ""
- client_secret <- ""
- # Get instance and users data ---------------------------------------------
- mastodon <- GET(paste0(url, "instances.json")) %>%
- content(as = "text") %>%
- fromJSON()
- download_time <- format(Sys.time(), tz = "UTC", usetz = TRUE)
- nbu <- sum(mastodon$users)
- nbi <- nrow(mastodon)
- # Plot data ---------------------------------------------------------------
- mastodon %>%
- filter(dense_rank(desc(users)) <= ntop) %>%
- ggplot(aes(reorder(name, users), users, fill = unlist(openRegistrations))) +
- geom_bar(stat = "identity") +
- scale_y_continuous(labels = comma) +
- scale_fill_discrete(labels = c("No", "Yes"), name = "Open registration") +
- labs(title = "Mastodon instances and users",
- subtitle = paste("Top", ntop, "instances (among", nbi, "serving", nbu, "users) -", download_time),
- x = "Instance",
- y = "Users",
- caption = paste("@milvus@mastodon.cloud\ndata from", url)) +
- coord_flip() +
- theme_fivethirtyeight() +
- theme(plot.caption = element_text(size = 7),
- axis.ticks.y = element_line(size = 0),
- panel.grid.major.y = element_line(size = 0)
- )
- plot_file <- paste0("mastodon_", download_time, ".png")
- ggsave(plot_file, width = 15, height = 10, units = "cm", dpi = 100, scale = 1.4)
- # Login -------------------------------------------------------------------
- r <- POST(paste0(instance , "oauth/token"),
- body = list(client_id = client_id,
- client_secret = client_secret,
- grant_type = "password",
- username = user,
- password = pass,
- scope = "write"))
- stop_for_status(r)
- token <- content(r)
- # Post image and status ---------------------------------------------------
- # post image
- r <- POST(paste0(instance , "api/v1/media"),
- add_headers(Authorization = paste("Bearer", token[["access_token"]])),
- body = list(file = upload_file(plot_file)))
- stop_for_status(r)
- media <- content(r)
- # post status
- r <- POST(paste0(instance , "/api/v1/statuses"),
- add_headers(Authorization = paste("Bearer", token[["access_token"]])),
- body = list(status = paste0("Mastodon instances and users\n",
- media[["text_url"]]),
- "media_ids[]" = media[["id"]]))
- stop_for_status(r)
- statuses <- content(r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement