@@ -37,18 +37,39 @@ def buildUrl(path, **kwargs):
37
37
def toUTC (sDateTime , timeZoneRegionName ):
38
38
return pytz .timezone ( timeZoneRegionName ).localize (parse (sDateTime )).astimezone (pytz .utc )
39
39
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 ):
41
58
dt = pytz .utc .localize (datetime .datetime .utcfromtimestamp (date / 1000 ))
59
+ if departureAirport :
60
+ info = getAirportInfoFromFlightStats ( departureAirport )
61
+ dt = dt .astimezone (pytz .timezone (info ['timeZoneRegionName' ]))
42
62
return (dt .year ,dt .month ,dt .day ,dt .hour )
43
63
44
- def getFlightSchedule (flight , date ):
64
+ def getFlightSchedule (flight , date , departureAirport ):
45
65
myLogger .debug ("getFlightSchedule with args: {0}, {1}" .format ( flight , date ) )
46
66
#parse the flight and date
47
67
index = flight .rfind (" " )
48
68
carrier = flight [:index ]
49
69
flightnumber = flight [index + 1 :]
50
70
51
- (year ,month ,day ,hour ) = parseDate (date )
71
+ (year ,month ,day ,hour ) = parseDate (date , departureAirport )
72
+ (yearDT ,monthDT ,dayDT ,hourDT ) = parseDate (date )
52
73
53
74
schedulesPath = "schedules/rest/v1/json/flight/{carrier}/{flightnumber}/departing/{year}/{month}/{day}"
54
75
url = buildUrl (schedulesPath , carrier = carrier , flightnumber = flightnumber , year = year , month = month , day = day )
@@ -89,8 +110,8 @@ def findAirline(code):
89
110
airport = findAirport (scheduledFlight ['departureAirportFsCode' ])
90
111
if airport is not None :
91
112
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 :
94
115
thisFlight = scheduledFlight
95
116
thisFlight ['departureTimeUTC' ] = str (utcDT )
96
117
arrAirport = findAirport ( scheduledFlight ['arrivalAirportFsCode' ])
0 commit comments