Files
docker-planefence/rootfs/usr/share/planefence/airlinename.sh.org
wiedehopf aa2726527a Improve planeheat script runtime and misc stuff (#85)
* add stderr redirection to all services

add error trapping to a bunch of important shell scripts for easier
debuging

* speed up planeheat.sh

* add --compressed to a bunch of curl invocations

* .gitignore .swp files

* make planefence run at a fixed interval regardless of runtime

* add shell script for building the current state of the directory

* make sure html directory exists before copying (for dev)

* improve shell script debugging message to show full path

* fix planeheat for bug introduced in speed improvement for it
2021-06-05 09:47:24 -04:00

7.9 KiB
Executable File

#!/bin/bash #set -x

#

#

#

#

#

#

#

#

#

# CACHEFILE="usr/share/planefence/persist.internal/planeownerscache.txt" #

-f "/usr/share/plane-alert/plane-alert.conf" && source "/usr/share/plane-alert/plane-alert.conf"

if [ -f "/usr/share/planefence/planefence.conf" ] then source "/usr/share/planefence/planefence.conf" else echo $/usr/share/planefence/planefence.conf is missing. We need it to run $0! exit 2 fi

if ! -f "$AIRLINECODES" then echo "Cannot stat $AIRLINECODES. $0 exiting." exit 2 fi

if "$1" == "" then echo Missing argument echo "Usage: $0 <flight_or_tail_number>" exit 2 fi

"$OWNERDBCACHE" == "" && OWNERDBCACHE=7 # time in days "$REMOTEMISSCACHE" == "" && REMOTEMISSCACHE=3600 # time in seconds MUSTCACHE=0

CLEANUP_CACHE () { "$2" -gt "0" && CACHETIME="$2" || CACHETIME=7 if -f "$1" then

awk -F ',' -v a="$(date -d "-$CACHETIME days" +%s)" -v b="$(date -d "-$REMOTEMISSCACHE seconds" +%s)" '{if ( ( $3 >= a && $2 != "#NOTFOUND") || ( $3 >= b && $2 == "#NOTFOUND")){print $1 "," $2 "," $3}}' $1 >/tmp/namecache 2>/dev/null mv -f /tmp/namecache $1 2>/dev/null

#something wrong

fi }

a="$1" # get the flight number or tail number from the command line argument a="${a#@}" # strip off any leading "@" signs - this is a Planefence feature

"$2" != "" && c="$2" || c="" # C is optional ICAO

echo $a | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null && b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 == a){print $2;exit;}}' $AIRLINECODES)" # Print flight number if we can find it

"$b" == "" && -f "$PLANEFILE" && b="$(awk -F ',' -v a="$a" '{IGNORECASE=1; if ($2 == a){print $3;exit;}}' $PLANEFILE)"

if "$b" == "" && -f "$CACHEFILE" && A-Za-z0-9]*' >/dev/null ; echo $?)" == "0" then CLEANUP_CACHE $CACHEFILE $OWNERDBCACHE b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 ~ "^"a){print $2;exit;}}' $CACHEFILE)" elif [[ "$b" = "" ]] && [[ "${a:0:4}" = "HMED" ]] && -f "$CACHEFILE" then CLEANUP_CACHE $CACHEFILE $OWNERDBCACHE b="$(awk -F ',' -v a="${a:0:4}" '{IGNORECASE=1; if ($1 ~ "^"a){print $2;exit;}}' $CACHEFILE)" fi

if "$b" == "" && "${a:0:1}" == "N" then b="$(timeout 3 curl --compressed -s https://registry.faa.gov/AircraftInquiry/Search/NNumberResult?nNumberTxt=$a | grep 'data-label=\"Name\"'|head -1 | sed 's|.*>\(.*\)<.*|\1|g')"

"$b" != "" && MUSTCACHE=1 fi

if "$CHECKREMOTEDB" == "ON" && "$b" == "" && A-Za-z0-9]*' >/dev/null ; echo $?)" == "0" then "$c" == "" && b="$(curl -L -s https://get-airline.planefence.com/?flight=$a)" || b="$(curl -L -s https://get-airline.planefence.com/?flight=$a&icao=$c)" [[ "${b:0:1}" = "#" ]] && b"#NOTFOUND" # results starting with # are errors or not-founds MUSTCACHE=2 # 2 means only cache the airline prefix elif "$CHECKREMOTEDB" == "ON" && "$b" == "" && "${a:0:4}" == "HMED" then "$c" == "" && b="$(curl -L -s https://get-airline.planefence.com/?flight=$a)" || b="$(curl -L -s https://get-airline.planefence.com/?flight=$a&icao=$c)" [[ "${b:0:1}" = "#" ]] && b"#NOTFOUND" # results starting with # are errors or not-founds MUSTCACHE=2 # 2 means only cache the airline prefix fi

if "$b" != "" then b="$(echo $b|xargs)" #clean up extra spaces b="${b% [A-Z0-9]}" #clean up single letters/numbers at the end, so "KENNEDY JOHN F" becomes "KENNEDY JOHN" b="${b% DBA}" #clean up some undesired suffices, mostly corporate entity names b="${b% TRUSTEE}" b="${b% OWNER}" b="${b% INC}" b="${b% LTD}" b="${b% PTY}" b="${b% \& CO KG}" b="${b% AG}" b="${b% AB}" b="${b% VOF}" b="${b% CO}" b="${b% CORP}" b="${b% LLC}" b="${b% GMBH}" b="${b% BV}" b="${b% NV}" b="${b/Government of/Govt}" b="${b/Ministry of Finance/MinFinance}"

fi

"$MUSTCACHE" == "1" && printf "%s,%s,%s\n" "$a" "$b" "$(date +%s)" >> "$CACHEFILE" "$MUSTCACHE" == "2" && printf "%s,%s,%s\n" "${a:0:4}" "$b" "$(date +%s)" >> "$CACHEFILE"

if "$MUSTCACHE" != "0" && "$(awk -F',' 'seen[$1]++' $CACHEFILE 2>/dev/null |wc -l)" != "0" then awk -F',' '!seen[$1]++' "$CACHEFILE" >/tmp/airlinecache mv -f /tmp/airlinecache "$CACHEFILE" fi

"$b" == "#NOTFOUND" && b=""

echo $b