Skip to content
This repository was archived by the owner on Feb 7, 2018. It is now read-only.

Commit 5d37ae2

Browse files
author
David Taieb
committed
fixed tz issue with getting flight schedule
1 parent 67453a0 commit 5d37ae2

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

pixiedust_flightpredict/pixiedust_flightpredict/running/flightAccess.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,39 @@ def buildUrl(path, **kwargs):
3737
def toUTC(sDateTime, timeZoneRegionName):
3838
return pytz.timezone( timeZoneRegionName ).localize(parse(sDateTime)).astimezone (pytz.utc)
3939

40-
def parseDate(date):
40+
airportCache={}
41+
def getAirportInfoFromFlightStats(code):
42+
if code in airportCache:
43+
return airportCache[code]
44+
45+
airportPath = "airports/rest/v1/json/{code}/today"
46+
url = buildUrl(airportPath, code=code)
47+
myLogger.debug("Calling airport api with url: " + url)
48+
response = requests.get(url)
49+
if response.status_code != 200:
50+
msg = "Error while trying to get airport info for {0}. Error is {1}".format(code, str(response.reason))
51+
myLogger.error(msg)
52+
raise requests.HTTPError(msg, response=response)
53+
54+
airportCache[code] = response.json()["airport"];
55+
return airportCache[code]
56+
57+
def parseDate(date, departureAirport = None):
4158
dt = pytz.utc.localize(datetime.datetime.utcfromtimestamp(date/1000))
59+
if departureAirport:
60+
info = getAirportInfoFromFlightStats( departureAirport )
61+
dt = dt.astimezone(pytz.timezone(info['timeZoneRegionName']))
4262
return (dt.year,dt.month,dt.day,dt.hour)
4363

44-
def getFlightSchedule(flight, date):
64+
def getFlightSchedule(flight, date, departureAirport):
4565
myLogger.debug("getFlightSchedule with args: {0}, {1}".format( flight, date ) )
4666
#parse the flight and date
4767
index = flight.rfind(" ")
4868
carrier = flight[:index]
4969
flightnumber = flight[index+1:]
5070

51-
(year,month,day,hour) = parseDate(date)
71+
(year,month,day,hour) = parseDate(date, departureAirport)
72+
(yearDT,monthDT,dayDT,hourDT) = parseDate(date)
5273

5374
schedulesPath = "schedules/rest/v1/json/flight/{carrier}/{flightnumber}/departing/{year}/{month}/{day}"
5475
url = buildUrl(schedulesPath, carrier=carrier, flightnumber=flightnumber, year=year, month=month, day=day)
@@ -89,8 +110,8 @@ def findAirline(code):
89110
airport = findAirport(scheduledFlight['departureAirportFsCode'])
90111
if airport is not None:
91112
utcDT = toUTC( scheduledFlight["departureTime"], airport['timeZoneRegionName'])
92-
myLogger.info("Comparing time for airport {0} between {1} and {2}".format( airport['name'], utcDT.hour, hour))
93-
if utcDT.hour == hour:
113+
myLogger.info("Comparing time for airport {0} between {1} and {2}".format( airport['name'], utcDT.hour, hourDT))
114+
if utcDT.hour == hourDT:
94115
thisFlight = scheduledFlight
95116
thisFlight['departureTimeUTC'] = str(utcDT)
96117
arrAirport = findAirport( scheduledFlight['arrivalAirportFsCode'])

pixiedust_flightpredict/pixiedust_flightpredict/running/runModel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def runModel(flight, date, departureAirport=None):
118118
if departureAirport is None:
119119
departureAirport = "LAS"
120120

121-
response = getFlightSchedule(flight, date)
121+
response = getFlightSchedule(flight, date, departureAirport)
122122
myLogger.debug("Schedule for flight {0} at date {1} : {2}".format(flight, date, response ))
123123
if "error" in response:
124124
return {"error": "Unable to access schedule {0}".format(response["error"])}

pixiedust_flightpredict/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup,find_packages
22

33
setup(name='pixiedust_flightpredict',
4-
version='0.3',
4+
version='0.6',
55
description='Flight delay predictor application with PixieDust',
66
url='https://github.com/ibm-cds-labs/simple-data-pipe-connector-flightstats/tree/master/pixiedust_flightpredict',
77
install_requires=['pixiedust'],

0 commit comments

Comments
 (0)