def _parse_response(self, response_json): self._logger.info('Creating objects to insert to database') solution = CalendarSolution(engine=self.engine, origin=self.origin, destination=self.destination, depart_date=self.depart_date, return_date=self.return_date) prices = [] for month in response_json['result']['calendar']['months']: for week in month['weeks']: for day in week['days']: if day['solutionCount'] == 0: continue for sol in day['tripDuration']['options']: dep_time = datetime.datetime.strptime(sol['solution']['slices'][0]['departure'][:10], "%Y-%m-%d").date() arr_time = datetime.datetime.strptime(sol['solution']['slices'][1]['departure'][:10], "%Y-%m-%d").date() price = sol['minPrice'] trip = TripMinimumPrice(dep_city=self.origin, arr_city=self.destination, dep_time=dep_time, arr_time=arr_time, price=price) prices.append(float(price.replace('USD', ''))) #FIXME: Can't assume USD solution.trip_prices.append(trip) solution.min_price = str(min(prices)) solution.save() return solution
def minimum_trips(self): """ Returns a CalendarSolution object from MongoDB """ return CalendarSolution.objects(origin=self.origin, destination=self.destination, depart_date=self.depart_date, return_date=self.return_date)
def _parse_response(self, response_json): self._logger.info('Creating objects to insert to database') solution = CalendarSolution(engine=self.engine, origin=self.origin, destination=self.destination, depart_date=self.depart_date, return_date=self.return_date) prices = [] for month in response_json['result']['calendar']['months']: for week in month['weeks']: for day in week['days']: if day['solutionCount'] == 0: continue for sol in day['tripDuration']['options']: dep_time = datetime.datetime.strptime( sol['solution']['slices'][0]['departure'][:10], "%Y-%m-%d").date() arr_time = datetime.datetime.strptime( sol['solution']['slices'][1]['departure'][:10], "%Y-%m-%d").date() price = sol['minPrice'] trip = TripMinimumPrice(dep_city=self.origin, arr_city=self.destination, dep_time=dep_time, arr_time=arr_time, price=price) prices.append(float(price.replace( 'USD', ''))) #FIXME: Can't assume USD solution.trip_prices.append(trip) solution.min_price = str(min(prices)) solution.save() return solution