improving dr drangs one liner

2023-02-06

Dr. Drang made a post about using Mastodons API to get useful info, which included a handy one liner to get basic stats about an account.

Drangs original one liner:

curl -s https://fosstodon.org/api/v1/accounts/lookup?acct=drdrang |\
jq '.followers_count, .following_count, .statuses_count, .last_status_at' |\
sed 's/"//g' |\
paste -d ' ' <(printf "Followers:\nFollowing:\nStatuses:\nLast status:") -

Luckily I had been playing with a one liner to get youtube info recently, and this stood out as something similar I needed to do for it.

jq lets you manipulate json files (which is what most apis return), changing the key names among other things. This means all the work can be done with jq to maniplate the text, rather than having a pipe to sed, paste, printf etc.

A quick search found this JSON to plain text using jq on stack overflow showing how to output the json as plain text (append ... | to_entries[] | "\(.key): \(.value)")

Here’s a way to do the produce the same output with just curl and jq:

curl -s https://fosstodon.org/api/v1/accounts/lookup?acct=drdrang |\
jq -r '{
    "Followers": .followers_count,
    "Following": .following_count,
    "Statuses": .statuses_count,
    "Last status": .last_status_at
} | to_entries[] | "\(.key): \(.value)"'

which produces:

Followers: 4406
Following: 97
Statuses: 79
Last status: 2023-02-06