i3blocks-pkfront.pl

#!/usr/bin/env perl
#
# i3blocks-pkfront.pl - written by the Iris System <iris@iris.ac.nz>,
# released into the public domain (or licensed under CC0, as required)
#
# a script for i3blocks / waybar to output the current fronters for a 
# system (where a system ID is given in ARGV), using PluralKit APIv2.
#
# known issues:
# - has no token support, requires current front to be public
#
# dependencies: (as Fedora package names)
# - perl-JSON
# - perl-LWP-UserAgent-Determined
# - perl-LWP-Protocol-https

use utf8;
use strict;
use warnings;
use open qw( :std :encoding(UTF-8) );

use JSON;
use LWP::UserAgent;

# constants
my $api_base = "https://api.pluralkit.me";

# get system ID from cmdline
my ($system_id) = @ARGV;
die "no system ID given" unless defined $system_id;

# create user-agent
my $ua = LWP::UserAgent->new;
$ua->agent("i3blocks-pkfront/0.1 (https://irys.cc/misc/i3blocks-pkfront.html) LWP/$LWP::VERSION ");

# build request
my $front_req = HTTP::Request->new(GET => "$api_base/v2/systems/$system_id/fronters");
$front_req->header(Accept => 'application/json');

# make request, bail on error
my $front_res = $ua->request($front_req);
die "PK API response not successful" unless $front_res->is_success;

# decode JSON
my $front_data = decode_json($front_res->content);

# collect member info
my (@member_basenames, @member_info);
foreach (@{ $front_data->{members} }) {
	my %tmember = %$_;

	my $tmember_n = ($tmember{name} || $tmember{display_name}) =~ y/\n/ /r;
	my $tmember_dn = ($tmember{display_name} || $tmember{name}) =~ y/\n/ /r;
	my $tmember_prns = ($tmember{pronouns} || 'pronouns not set') =~ y/\n/ /r;

	push @member_basenames, $tmember_n;
	push @member_info, sprintf("%s (%s)", $tmember_dn, $tmember_prns);
}

# display
printf "%s: %s\n%s\npkapi-system-%s", $system_id, join(', ', @member_basenames), join(', ', @member_info), $system_id;