Skip to content

Commit

Permalink
improve getting openskydb
Browse files Browse the repository at this point in the history
  • Loading branch information
kx1t committed Jan 21, 2025
1 parent dc23eeb commit f5b6b8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
22 changes: 14 additions & 8 deletions rootfs/etc/s6-overlay/scripts/50-get-openskydb
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,32 @@ else
savedfile_date="1970-01"
fi

if ! latestfile="$(curl -sL https://s3.opensky-network.org/data-samples/#metadata/ | grep -oP '(?<=\<Key\>metadata/)aircraft-database-complete.*?(?=\</Key\>)' | sort -ur | head -1)"; then
if ! latestfile="$(curl -sfL https://s3.opensky-network.org/data-samples/#metadata/ | grep -oP '(?<=\<Key\>metadata/)aircraft-database-complete.*?(?=\</Key\>)' | sort -ur | head -1)"; then
"${s6wrap[@]}" echo "Cannot reach the OpenSky web server to check if a newer OpenSky DB is available. Aborting update attempt."
if [[ ! -f /run/OpenSkyDB.csv ]] && [[ -f "/usr/share/planefence/persist/.internal/$savedfile" ]]; then
ln -sf "/usr/share/planefence/persist/.internal/$savedfile" "/run/OpenSkyDB.csv"
fi
exit
fi
latestfile_date="$(sed 's|aircraft-database-complete-\([0-9-]\+\).*$|\1|g' <<< "$latestfile")"

if (( ${savedfile_date//-/} < ${latestfile_date//-/} )); then
"${s6wrap[@]}" echo "Newer OpenSky database ($latestfile_date) available. Downloading it now"
if curl -sL "https://s3.opensky-network.org/data-samples/metadata/$latestfile" > "/tmp/$latestfile"; then
mv -f /tmp/"$latestfile" /usr/share/planefence/persist/.internal/
if curl -sfL "https://s3.opensky-network.org/data-samples/metadata/$latestfile" > "/tmp/$latestfile"; then
find /usr/share/planefence/stage/ -type f -name "aircraft-database-complete-*.csv" -delete
find /usr/share/planefence/persist/.internal/ -type f -name "aircraft-database-complete-*.csv" -delete
mv -f "/tmp/$latestfile" /usr/share/planefence/persist/.internal/
ln -sf "/usr/share/planefence/persist/.internal/$latestfile" "/run/OpenSkyDB.csv"
touch -d "31-Dec-2099" "/usr/share/planefence/persist/.internal/$latestfile" /run/OpenSkyDB.csv

# Cleanup all but the newest OpenSkyDB files
find /usr/share/planefence/stage/ -type f -name "aircraft-database-complete-*.csv" -printf '%T@ %p\n'| sort -g | head -n -1 | cut -d ' ' -f 2 | xargs rm -f
find /usr/share/planefence/persist/.internal/ -type f -name "aircraft-database-complete-*.csv" -printf '%T@ %p\n'| sort -g | head -n -1 | cut -d ' ' -f 2 | xargs rm -f
"${s6wrap[@]}" echo "Downloaded newest OpenSkyDB - $latestfile"
else
"${s6wrap[@]}" echo "Download of newest OpenSkyDB FAILED - $latestfile"
if [[ ! -f /run/OpenSkyDB.csv ]] && [[ -f "/usr/share/planefence/persist/.internal/$savedfile" ]]; then
ln -sf "/usr/share/planefence/persist/.internal/$savedfile" "/run/OpenSkyDB.csv"
fi
fi
else
"${s6wrap[@]}" echo "Current OpenSkyDB file ($savedfile) is up to date."
if [[ ! -f /run/OpenSkyDB.csv ]] && [[ -f "/usr/share/planefence/persist/.internal/$savedfile" ]]; then
ln -sf "/usr/share/planefence/persist/.internal/$savedfile" "/run/OpenSkyDB.csv"
fi
fi
8 changes: 6 additions & 2 deletions rootfs/usr/share/planefence/airlinename.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ if [[ -z "$b" ]]; then
for (( i=0; i < ${#header[@]}; i++ )); do
if [[ "${header[i]}" == "registration" ]]; then OSDB_reg="$((i + 1))"; fi
if [[ "${header[i]}" == "owner" ]]; then OSDB_owner="$((i + 1))"; fi
# not needed -- if [[ "${header[i]}" == "icao24" ]]; then OSDB_icao="$((i + 1))"; fi
if [[ "${header[i]}" == "icao24" ]]; then OSDB_icao="$((i + 1))"; fi
done
b="$(awk -F "," -v p="${a,,}" -v reg="$OSDB_reg" -v own="$OSDB_owner" '{IGNORECASE=1; gsub("-",""); gsub("\"",""); if(tolower($reg)==p) {print $own;exit}}' /run/OpenSkyDB.csv)"
b="$(awk -F "," -v p="${a,,}" -v reg="$OSDB_reg" -v own="$OSDB_owner" '{IGNORECASE=1; gsub("-",""); gsub('\'',""); if(tolower($reg)==p) {print $own;exit}}' /run/OpenSkyDB.csv)"
if [[ -z "$b" ]] && [[ -n "$c" ]]; then
# there's an ICAO, let's try that...
b="$(awk -F "," -v p="${c,,}" -v icao="$OSDB_icao" -v own="$OSDB_owner" '{IGNORECASE=1; gsub("-",""); gsub('\''",""); if(tolower($icao)==p) {print $own;exit}}' /run/OpenSkyDB.csv)"
fi
if [[ -n "$b" ]]; then MUSTCACHE=1; fi
if [[ -n "$b" ]] && [[ -z "$q" ]]; then q="OpenSky"; fi
fi
Expand Down

0 comments on commit f5b6b8c

Please sign in to comment.