· 8 years ago · Apr 10, 2017, 08:42 PM
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)
10library(tidyverse)
11library(httr)
12library(jsonlite)
13library(scales)
14library(ggthemes)
15
16# data source
17url <- "https://instances.mastodon.xyz/"
18
19# Number of instances to display
20ntop = 20
21
22# Your instance, login and password
23instance <- "https://mastodon.cloud/"
24user <- ""
25pass <- ""
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
41client_id <- ""
42client_secret <- ""
43
44
45# Get instance and users data ---------------------------------------------
46
47mastodon <- GET(paste0(url, "instances.json")) %>%
48 content(as = "text") %>%
49 fromJSON()
50
51download_time <- format(Sys.time(), tz = "UTC", usetz = TRUE)
52
53nbu <- sum(mastodon$users)
54nbi <- nrow(mastodon)
55
56
57# Plot data ---------------------------------------------------------------
58
59mastodon %>%
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
77plot_file <- paste0("mastodon_", download_time, ".png")
78ggsave(plot_file, width = 15, height = 10, units = "cm", dpi = 100, scale = 1.4)
79
80
81# Login -------------------------------------------------------------------
82
83r <- 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"))
90stop_for_status(r)
91token <- content(r)
92
93
94# Post image and status ---------------------------------------------------
95
96# post image
97r <- POST(paste0(instance , "api/v1/media"),
98 add_headers(Authorization = paste("Bearer", token[["access_token"]])),
99 body = list(file = upload_file(plot_file)))
100stop_for_status(r)
101media <- content(r)
102
103# post status
104r <- 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"]]))
109stop_for_status(r)
110statuses <- content(r)