01234567890123456789012345678901234567890123456789012345678901234567890123456789
192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973 | <----SKIPPED LINES----> last_query_time = 0 def GetFlightAwareJson(flight_number): """Scrapes the text json message from FlightAware for a given flight number. Given a flight number, loads the corresponding FlightAware webpage for that flight and extracts the relevant script that contains all the flight details from that page. But only queries at most once per fixed period of time so as to avoid being blocked. Args: flight_number: text flight number (i.e.: SWA1234) Returns: Two tuple: - Text representation of the json message from FlightAware. - Text string of error message, if any """ min_query_delay_seconds = 90 url = 'https://flightaware.com/live/flight/' + flight_number # It seems there are a lot of queries on flight aware that come at # about the same time, so let's see if we can track when and why they # happen LogFlightAwareQuery(flight_number) global last_query_time seconds_since_last_query = time.time() - last_query_time if last_query_time and seconds_since_last_query < min_query_delay_seconds: error_msg = ( 'Unable to query FA for URL since last query to FA was only %d seconds ' 'ago; min of %d seconds needed: %s' % ( seconds_since_last_query, min_query_delay_seconds, url)) LogFlightAwareQuery(flight_number, error_msg) return '', error_msg last_query_time = time.time() try: response = requests.get(url, timeout=5) except requests.exceptions.RequestException as e: error_msg = 'Unable to query FA for URL due to %s: %s' % (e, url) LogFlightAwareQuery(flight_number, error_msg) return '', error_msg LogFlightAwareQuery(flight_number) <----SKIPPED LINES----> |
01234567890123456789012345678901234567890123456789012345678901234567890123456789
19291930193119321933193419351936193719381939194019411942194319441945194619471948 19491950195119521953195419551956195719581959196019611962196319641965196619671968 | <----SKIPPED LINES----> last_query_time = 0 def GetFlightAwareJson(flight_number): """Scrapes the text json message from FlightAware for a given flight number. Given a flight number, loads the corresponding FlightAware webpage for that flight and extracts the relevant script that contains all the flight details from that page. But only queries at most once per fixed period of time so as to avoid being blocked. Args: flight_number: text flight number (i.e.: SWA1234) Returns: Two tuple: - Text representation of the json message from FlightAware. - Text string of error message, if any """ min_query_delay_seconds = 90 url = 'https://flightaware.com/live/flight/' + flight_number global last_query_time seconds_since_last_query = time.time() - last_query_time if last_query_time and seconds_since_last_query < min_query_delay_seconds: error_msg = ( 'Unable to query FA for URL since last query to FA was only %d seconds ' 'ago; min of %d seconds needed: %s' % ( seconds_since_last_query, min_query_delay_seconds, url)) LogFlightAwareQuery(flight_number, error_msg) return '', error_msg last_query_time = time.time() try: response = requests.get(url, timeout=5) except requests.exceptions.RequestException as e: error_msg = 'Unable to query FA for URL due to %s: %s' % (e, url) LogFlightAwareQuery(flight_number, error_msg) return '', error_msg LogFlightAwareQuery(flight_number) <----SKIPPED LINES----> |