Skip to content

Instantly share code, notes, and snippets.

@tyler-sommer
Created November 16, 2017 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyler-sommer/8e95aeab836ab935849baa675db5f284 to your computer and use it in GitHub Desktop.
Save tyler-sommer/8e95aeab836ab935849baa675db5f284 to your computer and use it in GitHub Desktop.
Fetches an entity's zkill history and displays number of kills/losses by hour in JSON format.
#!/bin/bash
entity_kind=""
entity_id=""
usage() {
echo "Usage: $0 [-h] [-a <allianceID> | -c <corporationID> | -p <characterID>]"
echo
echo "One option (-a, -c, -p) must be selected."
}
while getopts "ha:c:p:" opt; do
case "$opt" in
a ) entity_kind="alliance"
entity_id="$OPTARG"
;;
c ) entity_kind="corporation"
entity_id="$OPTARG"
;;
p ) entity_kind="character"
entity_id="$OPTARG"
;;
\?,h )
usage
exit
;;
esac
done
if [[ $entity_kind == "" || $entity_id == "" ]]; then
usage
exit 1
fi
kills_file="kills_${entity_kind}_${entity_id}.json"
if [ -f $file ] && test `find "$kills_file" -cmin -10`; then
echo "Too soon to fetch new data."
echo
echo "Last fetched $((`stat -c %Y "$kills_file"` - `date +%s`)) seconds ago. Waiting ten minutes; 600 seconds."
echo
else
echo > "$kills_file"
echo "Downloading latest kills..."
echo
for i in {1..10}; do
curl -L "https://zkillboard.com/api/no-items/no-attackers/limit/200/${entity_kind}ID/${entity_id}/page/${i}/" >> "$kills_file" 2>> kills_error.log
done
fi
jq -s 'flatten(1) | [.[].killmail_time
| capture("T(?<time>[0-9]+):")
| .time
| {"string": (. + ":00"), "number": (. | tonumber)}]
| {
"avg": ([.[].number] | add / length)
, "counts": ([.[].string] | [group_by(.) | .[] | {(first): length}]
| reduce .[] as $item ({}; . *= $item))
}' < "$kills_file" 2>> kills_error.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment