Advertisement
Guest User

This is terrible...

a guest
Jul 24th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. FILTER=""
  6. INPUT=""
  7.  
  8. TIME_INTERVAL_DEFAULT="day"
  9. TIME_INTERVAL=$TIME_INTERVAL_DEFAULT
  10. DATE_DEFAULT=$(date +%Y%m%d --date="1 $TIME_INTERVAL_DEFAULT ago")
  11.  
  12. OUTPUT="shipTypeID characterName corporationName allianceName"
  13. DELIMITER=","
  14.  
  15. TYPEID=(3514 22852 23913 23917 23919 671 3764 11567 23773)
  16. NAME=("Revenant" "Hel" "Nyx" "Wyvern" "Aeon" "Erebus" "Leviathan" "Avatar" "Ragnarok")
  17.  
  18. API_SUPERS="https://zkillboard.com/api/kills/groupID/659/no-items/startTime/"$DATE_DEFAULT"0000"
  19. API_TITANS="https://zkillboard.com/api/kills/groupID/30/no-items/startTime/"$DATE_DEFAULT"0000"
  20.  
  21. TMP_SUPERS="/dev/shm/super-involved.txt"
  22. TMP_TITANS="/dev/shm/titan-involved.txt"
  23. TMP_ALL="/dev/shm/all-involved.txt"
  24.  
  25. function clean_tmp_files() {
  26.     if [[ -e $TMP_SUPERS || -e $TMP_TITANS || -e $TMP_ALL ]] ; then
  27.     rm $TMP_SUPERS $TMP_TITANS $TMP_ALL
  28.     exit 0
  29.     else
  30.     return
  31.     fi
  32. }
  33.  
  34. function set_time() {
  35.     case $TIME_INTERVAL in
  36.     day|week|month)
  37.         DATE=$(date +%Y%m%d --date="1 $TIME_INTERVAL ago")
  38.         ;;
  39.     first)
  40.         DATE="$(date +%Y%m)01"
  41.         ;;
  42.     *)
  43.     esac
  44.  
  45.     API_SUPERS="https://zkillboard.com/api/kills/groupID/659/no-items/startTime/"$DATE"0000"
  46.     API_TITANS="https://zkillboard.com/api/kills/groupID/30/no-items/startTime/"$DATE"0000"
  47. }
  48.  
  49. function get_data() {
  50.  
  51.     # Get necessary killmail data from zkill, using gzip because we are just
  52.     # that nice
  53.    
  54.     curl --show-error --silent --header "Accept-Encoding: gzip" --user-agent "email" $1 | gzip -d - > $2
  55. }
  56.  
  57. function check_data() {
  58.  
  59.     # Check whether the temporary files holding our killmail data exist and/or
  60.     # whether they are older than one hour.
  61.    
  62.     TMP_FILE=""
  63.  
  64.     case $1 in
  65.     --supers)
  66.         TMP_FILE=$TMP_SUPERS
  67.         API=$API_SUPERS
  68.         ;;
  69.     --titans)
  70.         TMP_FILE=$TMP_TITANS
  71.         API=$API_TITANS
  72.         ;;
  73.     *)
  74.         return 1
  75.     esac
  76.  
  77.     if [[ ! -e $TMP_FILE && ! -s $TMP_FILE || $(( $(stat -c %Y $TMP_FILE) + 3600 )) < $(date +%s) ]] ; then
  78.     get_data $API $TMP_FILE
  79.     fi
  80. }
  81.  
  82. function parse_input() {
  83.     json -ga attackers | json -ga $1 -d$2 | sed -n -e $3 | sort | uniq
  84. }
  85.  
  86. function combine_input() {
  87.  
  88.     # Combine separate API queries for supers and titans into one input file,
  89.     # since zkill limits the available amount of killmails severly if both are
  90.     # polled in the same request. This could even be done for each type of
  91.     # super individually, if necessary
  92.  
  93.     # In order for this combination to work, we have to make sure they are all
  94.     # part of the *same* json array, which is why we are stripping the closing
  95.     # bracket from the first file and join the second file by replacing its
  96.     # opening bracket with a comma, thus combining them into one array
  97.    
  98.     if [[ ! -e $TMP_ALL && ! -s $TMP_ALL || $TMP_SUPERS -nt $TMP_ALL || $TMP_TITANS -nt $TMP_ALL ]] ; then
  99.     sed 's/\]$//' $TMP_SUPERS > $TMP_ALL
  100.     sed 's/^\[/,/' $TMP_TITANS >> $TMP_ALL
  101.     fi
  102. }
  103.  
  104. while getopts :cdfimstwx OPT; do
  105.     case $OPT in
  106.     x|+x)
  107.         echo "Dummy argument for testing purposes"
  108.  
  109.         DATE=$(date +%Y%m)
  110.  
  111.         for ((i=0; i<${#TYPEID[@]}; i++)); do
  112.         get_data https://zkillboard.com/api/kills/shipTypeID/${TYPEID[i]}/no-items/startTime/201506120000 /dev/shm/${TYPEID[i]}.txt
  113.         done
  114.        
  115.         exit 0
  116.         ;;
  117.  
  118.     s|+s)
  119.         for ((i=0; i<5; i++)); do
  120.         FILTER+="s/${TYPEID[i]}/${NAME[i]}/p;"
  121.         done
  122.         ;;
  123.     t|+t)
  124.         for ((i=5; i<${#TYPEID[@]}; i++)); do
  125.         FILTER+="s/${TYPEID[i]}/${NAME[i]}/p;"
  126.         done
  127.         ;;
  128.     i|+i)
  129.         INPUT="interactive"
  130.         ;;
  131.     d|+d)
  132.         TIME_INTERVAL="day"
  133.         ;;
  134.     w|+w)
  135.         TIME_INTERVAL="week"
  136.         ;;
  137.     m|+m)
  138.         TIME_INTERVAL="month"
  139.         ;;
  140.     f|+f)
  141.         TIME_INTERVAL="first"
  142.         ;;
  143.     c|+c)
  144.         clean_tmp_files
  145.         ;;
  146.     *)
  147.         echo "usage: ${0##*/} [+-cdfimstwx} [--] ARGS..."
  148.         exit 2
  149.     esac
  150. done
  151. shift $(( OPTIND - 1 ))
  152. OPTIND=1
  153.  
  154. set_time
  155. check_data --supers
  156. check_data --titans
  157. combine_input
  158.  
  159. if [[ "$INPUT" == "interactive" ]]; then
  160.     parse_input "$OUTPUT" "$DELIMITER" "${FILTER[*]}"
  161.  
  162.     while read data; do
  163.     echo "INPUT = $INPUT"
  164.     done
  165. else
  166.     <$TMP_ALL parse_input "$OUTPUT" "$DELIMITER" "${FILTER[*]}"
  167. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement