What is Nostr?
dtonon / daniele
npub1000…vwqk
2025-01-13 22:36:47

Find the last people who have followed a pubkey

The following script try, using nak, to find out the last ten people who have followed a target_pubkey, sorted by the most recent. It’s possibile to shorten search_timerange to speed up the search.

#!/usr/bin/env fish

# Target pubkey we're looking for in the tags
set target_pubkey "6e468422dfb74a5738702a8823b9b28168abab8655faacb6853cd0ee15deee93"

set current_time (date +%s)
set search_timerange (math $current_time - 600)    # 24 hours = 86400 seconds

set pubkeys (nak req --kind 3 -s $search_timerange wss://relay.damus.io/ wss://nos.lol/ 2>/dev/null | \
    jq -r --arg target "$target_pubkey" '
    select(. != null and type == "object" and has("tags")) |
    select(.tags[] | select(.[0] == "p" and .[1] == $target)) |
    .pubkey
    ' | sort -u)

if test -z "$pubkeys"
    exit 1
end

set all_events ""
set extended_search_timerange (math $current_time - 31536000)   # One year

for pubkey in $pubkeys
    echo "Checking $pubkey"
    set events (nak req --author $pubkey -l 5 -k 3 -s $extended_search_timerange wss://relay.damus.io wss://nos.lol 2>/dev/null | \
        jq -c --arg target "$target_pubkey" '
        select(. != null and type == "object" and has("tags")) |
        select(.tags[][] == $target)
        ' 2>/dev/null)
    
    set count (echo "$events" | jq -s 'length')

    if test "$count" -eq 1
        set all_events $all_events $events
    end
end

if test -n "$all_events"
    echo -e "Last people following $target_pubkey:"
    echo -e ""

    set sorted_events (printf "%s\n" $all_events | jq -r -s '
    unique_by(.id) |
    sort_by(-.created_at) |
    .[] | @json
    ')

    for event in $sorted_events
        set npub (echo $event | jq -r '.pubkey' | nak encode npub)
        set created_at (echo $event | jq -r '.created_at')
        if test (uname) = "Darwin"
            set follow_date (date -r "$created_at" "+%Y-%m-%d %H:%M")
        else
            set follow_date (date -d @"$created_at" "+%Y-%m-%d %H:%M")
        end
        echo "$follow_date - $npub"
    end
end
Author Public Key
npub10000003zmk89narqpczy4ff6rnuht2wu05na7kpnh3mak7z2tqzsv8vwqk