Changeset 66:d288a492befe


Ignore:
Timestamp:
04/09/12 06:55:01 (13 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

The METARs can now be downloaded

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r63 r66  
    1010import urllib2
    1111import hashlib
     12import time
    1213import datetime
    1314import codecs
     15import re
    1416import xml.sax
    1517
     
    426428
    427429    def run(self):
    428         """Perform the plane update."""
     430        """Perform the retrieval of the NOTAMs."""
    429431        xmlParser = xml.sax.make_parser()
    430432        notamHandler = NOTAMHandler([self._departureICAO, self._arrivalICAO])
     
    447449#------------------------------------------------------------------------------
    448450
     451class GetMETARs(Request):
     452    """Get the METARs from the NOAA website for certain airport ICAOs."""   
     453
     454    # Regular expression matching a METAR line
     455    metarLine = re.compile("^[A-Z0-9]{4} ")
     456
     457    def __init__(self, callback, airports):
     458        """Construct the request for the given airports."""
     459        super(GetMETARs, self).__init__(callback)
     460        self._airports = airports
     461
     462    def run(self):
     463        """Perform the retrieval opf the METARs."""
     464        tm = time.gmtime()
     465        url = "http://weather.noaa.gov/pub/data/observations/metar/cycles/%02dZ.TXT" % \
     466              (tm.tm_hour,)
     467        f = urllib2.urlopen(url)
     468        try:
     469            result = Result()
     470            result.metars = {}
     471            for line in iter(f.readline, ""):
     472                if len(line)>5 and line[4]==' ':
     473                    icao = line[0:4]
     474                    if icao in self._airports:
     475                        result.metars[icao] = line.strip()
     476        finally:
     477            f.close()
     478
     479        return result
     480
     481#------------------------------------------------------------------------------
     482
    449483class Handler(threading.Thread):
    450484    """The handler for the web services.
     
    476510        """Get the NOTAMs for the given two airports."""
    477511        self._addRequest(GetNOTAMs(callback, departureICAO, arrivalICAO))
     512       
     513    def getMETARs(self, callback, airports):
     514        """Get the METARs for the given airports."""
     515        self._addRequest(GetMETARs(callback, airports))
    478516       
    479517    def run(self):
     
    513551    #time.sleep(3)
    514552
    515     handler.getNOTAMs(callback, "LHBP", "EPWA")
    516     time.sleep(3)
     553    #handler.getNOTAMs(callback, "LHBP", "EPWA")
     554    handler.getMETARs(callback, ["LHBP", "EPWA"])
     555    time.sleep(5)
    517556   
    518557
Note: See TracChangeset for help on using the changeset viewer.