Changeset 963:35b2e35e9e07 for src


Ignore:
Timestamp:
05/14/19 18:02:30 (5 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
python3
Phase:
public
Message:

METAR downloading and processing is more robust

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r962 r963  
    10521052                                  ("hoursBeforeNow", "24"),
    10531053                                  ("mostRecentForEachStation", "constraint")])
    1054         url += data
    1055         f = urllib.request.urlopen(url, timeout = 10.0, cafile=certifi.where())
     1054        result = Result()
     1055        result.metars = {}
    10561056        try:
    1057             result = Result()
    1058             result.metars = {}
    1059             for line in f.readlines():
    1060                 line = str(line, "iso-8859-1")
    1061                 if len(line)>5 and line[4]==' ':
    1062                     icao = line[0:4]
    1063                     if icao in self._airports:
    1064                         result.metars[icao] = line.strip().split(",")[0]
    1065         finally:
    1066             f.close()
     1057            url += data
     1058            f = urllib.request.urlopen(url, timeout = 10.0, cafile = certifi.where())
     1059            try:
     1060                for line in f.readlines():
     1061                    line = str(line, "iso-8859-1")
     1062                    if len(line)>5 and line[4]==' ':
     1063                        icao = line[0:4]
     1064                        if icao in self._airports:
     1065                            result.metars[icao] = line.strip().split(",")[0]
     1066            finally:
     1067                f.close()
     1068        except Exception as e:
     1069            traceback.print_exc()
     1070            print("mlx.web.GetMETARs.run: failed to get METARs for %s: %s" % \
     1071                  (self._airports, str(e)))
    10671072
    10681073        return result
Note: See TracChangeset for help on using the changeset viewer.