Advertisement
Guest User

Mastodon API

a guest
Apr 10th, 2017
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.41 KB | None | 0 0
  1. # Fetch Mastodon instances and users counts, generate a plot and toot
  2. # the image
  3. #
  4. # @milvus@mastodon.cloud - 2017-04-10
  5.  
  6.  
  7. # Setup -------------------------------------------------------------------
  8.  
  9. # Packages (to be installed before use)
  10. library(tidyverse)
  11. library(httr)
  12. library(jsonlite)
  13. library(scales)
  14. library(ggthemes)
  15.  
  16. # data source
  17. url <- "https://instances.mastodon.xyz/"
  18.  
  19. # Number of instances to display
  20. ntop = 20
  21.  
  22. # Your instance, login and password
  23. instance <- "https://mastodon.cloud/"
  24. user <- ""
  25. pass <- ""
  26.  
  27. # Registration, uncomment below,
  28. # run this part once and write down client_id and client_secret,
  29. # comment again
  30.  
  31. # r <- POST(paste0(instance , "api/v1/apps"),
  32. #      body = list(client_name = "mastodon_count_plot_v2",
  33. #                  redirect_uris = "urn:ietf:wg:oauth:2.0:oob",
  34. #                  scopes = "write"))
  35. # stop_for_status(r)
  36. # apps <- content(r)
  37. #
  38. # paste("client_id :", apps[["client_id"]])
  39. # paste("client_secret", apps[["client_secret"]])
  40.  
  41. client_id <- ""
  42. client_secret <- ""
  43.  
  44.  
  45. # Get instance and users data ---------------------------------------------
  46.  
  47. mastodon <- GET(paste0(url, "instances.json")) %>%
  48.   content(as = "text") %>%
  49.   fromJSON()
  50.  
  51. download_time <- format(Sys.time(), tz = "UTC",  usetz = TRUE)
  52.  
  53. nbu <- sum(mastodon$users)
  54. nbi <- nrow(mastodon)
  55.  
  56.  
  57. # Plot data ---------------------------------------------------------------
  58.  
  59. mastodon %>%
  60.   filter(dense_rank(desc(users)) <= ntop) %>%
  61.   ggplot(aes(reorder(name, users), users, fill = unlist(openRegistrations))) +
  62.   geom_bar(stat = "identity") +
  63.   scale_y_continuous(labels = comma) +
  64.   scale_fill_discrete(labels = c("No", "Yes"), name = "Open registration") +
  65.   labs(title = "Mastodon instances and users",
  66.        subtitle = paste("Top", ntop, "instances (among", nbi, "serving", nbu, "users) -", download_time),
  67.        x = "Instance",
  68.        y = "Users",
  69.        caption = paste("@milvus@mastodon.cloud\ndata from", url)) +
  70.   coord_flip() +
  71.   theme_fivethirtyeight() +
  72.   theme(plot.caption = element_text(size = 7),
  73.         axis.ticks.y = element_line(size = 0),
  74.         panel.grid.major.y = element_line(size = 0)
  75.   )
  76.  
  77. plot_file <- paste0("mastodon_", download_time, ".png")
  78. ggsave(plot_file, width = 15, height = 10, units = "cm", dpi = 100, scale = 1.4)
  79.  
  80.  
  81. # Login -------------------------------------------------------------------
  82.  
  83. r <- POST(paste0(instance , "oauth/token"),
  84.           body = list(client_id = client_id,
  85.                       client_secret = client_secret,
  86.                       grant_type = "password",
  87.                       username = user,
  88.                       password = pass,
  89.                       scope = "write"))
  90. stop_for_status(r)
  91. token <- content(r)
  92.  
  93.  
  94. # Post image and status ---------------------------------------------------
  95.  
  96. # post image
  97. r <- POST(paste0(instance , "api/v1/media"),
  98.           add_headers(Authorization = paste("Bearer", token[["access_token"]])),
  99.           body = list(file = upload_file(plot_file)))
  100. stop_for_status(r)
  101. media <- content(r)
  102.  
  103. # post status
  104. r <- POST(paste0(instance , "/api/v1/statuses"),
  105.           add_headers(Authorization = paste("Bearer", token[["access_token"]])),
  106.           body = list(status = paste0("Mastodon instances and users\n",
  107.                                       media[["text_url"]]),
  108.                       "media_ids[]" = media[["id"]]))
  109. stop_for_status(r)
  110. statuses <- content(r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement