Last active
January 28, 2019 20:50
-
-
Save imolein/d878f37c817ce83125d2732e5e371072 to your computer and use it in GitHub Desktop.
This script returns the domains and what percentage of your followers belong to them. Updated version at https://git.kokolor.es/imo/follower-stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
-- This script returns the domains and what percentage of your followers belong to them. | |
-- Works with Pleroma and Mastodon | |
-- | |
-- Usage: | |
-- lua follower-domain-stats.lua INSTANCE USER_ID [ACCESS_TOKEN] | |
-- | |
-- Example: | |
-- lua follower-domain-stats.lua https://edolas.world 1 1234567abcdef | |
-- | |
-- Note: ACCESS_TOKEN ist only need on Mastodon, on Pleroma you get the followers without it | |
local requests = require('requests') | |
local url = arg[1] | |
local id = arg[2] | |
local access_token = arg[3] | |
local function percentage(stats) | |
local p_stats = { | |
followers = {}, | |
following = {} | |
} | |
for tname, tbl in pairs(stats) do | |
local total = stats[tname].total | |
for dom, val in pairs(tbl) do | |
if dom ~= 'total' then | |
table.insert(p_stats[tname], { dom, ( val * 100) / total }) | |
end | |
end | |
table.sort(p_stats[tname], function(a, b) return a[2] > b[2] end) | |
end | |
return p_stats | |
end | |
local function main(url, id, access_token) | |
local stats = { | |
followers = { | |
total = 0 | |
}, | |
following = { | |
total = 0 | |
} | |
} | |
local headers = {} | |
if access_token then | |
headers['Authorization'] = 'Bearer ' .. access_token | |
end | |
for tname in pairs(stats) do | |
local resp, err = requests.get({ string.format('%s/api/v1/accounts/%d/%s', url, id, tname), headers = headers}).json() | |
err = resp.error or err | |
if not resp or resp.error then error('The following error occured: ' .. err) end | |
for _, val in pairs(resp) do | |
local domain = val.acct:match('^.*@(.*)$') or 'local' | |
if stats[tname][domain] then | |
stats[tname][domain] = stats[tname][domain] + 1 | |
else | |
stats[tname][domain] = 1 | |
end | |
stats[tname].total = stats[tname].total + 1 | |
end | |
end | |
return percentage(stats) | |
end | |
local stuff = main(url, id, access_token) | |
for n, t in pairs(stuff) do | |
print(string.format('Percentage overview for %s:', n)) | |
for _, p in ipairs(t) do | |
print(string.format(' %s: %.2f%%', p[1], p[2])) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated version at https://git.kokolor.es/imo/follower-stats