summaryrefslogtreecommitdiff
path: root/metar.sh
blob: 14b71e056de722ee369231e67ae13512658998ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

read ICAO

if ! echo "$ICAO" | grep -Eq "^([A-Z]|[a-z]){4}$" ; then
  echo "error: invalid ICAO code!"
  exit 1
fi

ICAO="$(echo "$ICAO" | tr "a-z" "A-Z")"

# aviationweather.gov
result1="$(curl -s https://aviationweather.gov/cgi-bin/data/metar.php?ids=$ICAO)"

# metar-taf.com
result2="$(curl -s "https://metar-taf.com/history/$ICAO" | grep -E "METAR $ICAO [0-9]{6}Z" | head -n1 | sed "s/^\s*METAR // ; s|</td>|| ; s/\s*$//")"

if [[ "$result1" > "$result2" ]] ; then
  echo "$result1"
else
  echo "$result2"
fi