コード例 #1
0
ファイル: ITADao.py プロジェクト: muerl/flight_finder
    def get_trip_data(self):
        #breaks up the date in range into sections of 30 days each.
        #this is done because ITA doesn't handle chunks larger than 30 days each that well
        amt_of_days = (self.end_date-self.start_date).days
        searches_needed = amt_of_days/30
        months = []
        threads = []
        start = self.start_date
        end = self.start_date + datetime.timedelta(days=29)

        #creates a thread that requests a calendar for each 30 day chunk
        for x in range(0, searches_needed):
            t = CalendarRequestThread(self.dest, start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'), self.min_length, self.max_length, months)
            t.start()
            threads.append(t)
            start = end + datetime.timedelta(days=1)
            end = start + datetime.timedelta(days=29)

        #creates a thread for the remainder if the date range is not divisible by 30
        rem = amt_of_days % 30
        if rem != 0:
            end = start + datetime.timedelta(days=rem-1)
            t = CalendarRequestThread(self.dest, start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'), self.min_length, self.max_length, months)
            t.start()
            threads.append(t)

        #blocks until the threads finish
        for t in threads:
            t.join()

        threads = []
        trips = []
        #goes through each calendar retrieved
        for calendar in months:
            #only gets the cheapest dates in the calendar since those most likely have the best deal and to limit the
            #request count to appear more human
            cheapest_dates = calendar.find_cheapest_rt()
            for dates in cheapest_dates:
                #create a separate thread that requests the trips for the cheapest dates
                t = TripRequestThread(self.dest, dates[0], dates[1], self.start_date.strftime('%Y-%m-%d'), self.end_date.strftime('%Y-%m-%d'), self.min_length, self.max_length, calendar.get_session_id(), trips)
                t.start()
                threads.append(t)

        for t in threads:
            t.join()

        #combine all the returned trip data from all the threads
        ita_trips = ITATrips()
        for trip_data in trips:
            ita_trips._add_trips(trip_data)

        return ita_trips
コード例 #2
0
ファイル: ITADao.py プロジェクト: NickRuiz/flight_finder
    def get_specific_trip_data(self):

        threads = []
        trips = []

        print self.flight_requests.to_json()
        t = MultiDestinationRequestThread(self.flight_requests, trips)
        t.start()
        threads.append(t)

        for t in threads:
            t.join()

        #combine all the returned trip data from all the threads
        ita_trips = ITATrips()
        for trip_data in trips:
            ita_trips._add_trips(trip_data)

        return ita_trips
コード例 #3
0
ファイル: ITADao.py プロジェクト: NickRuiz/flight_finder
    def get_calendar_trip_data(self):
        #breaks up the date in range into sections of 30 days each.
        #this is done because ITA doesn't handle chunks larger than 30 days each that well
        amt_of_days = (self.end_date - self.start_date).days
        searches_needed = amt_of_days / 30  # TODO: if the search is less than 30 days, this doesn't work.
        months = []
        threads = []
        start = self.start_date
        end = self.start_date + datetime.timedelta(days=29)

        #creates a thread that requests a calendar for each 30 day chunk
        for x in range(0, searches_needed):
            t = CalendarRequestThread(self.origin, self.dest,
                                      start.strftime('%Y-%m-%d'),
                                      end.strftime('%Y-%m-%d'),
                                      self.min_length, self.max_length, months)
            t.start()
            threads.append(t)
            start = end + datetime.timedelta(days=1)
            end = start + datetime.timedelta(days=29)

        #creates a thread for the remainder if the date range is not divisible by 30
        rem = amt_of_days % 30
        if rem != 0:
            end = start + datetime.timedelta(days=rem - 1)
            t = CalendarRequestThread(self.origin, self.dest,
                                      start.strftime('%Y-%m-%d'),
                                      end.strftime('%Y-%m-%d'),
                                      self.min_length, self.max_length, months)
            t.start()
            threads.append(t)

        #blocks until the threads finish
        for t in threads:
            t.join()

        threads = []
        trips = []
        #goes through each calendar retrieved
        for calendar in months:
            #only gets the cheapest dates in the calendar since those most likely have the best deal and to limit the
            #request count to appear more human
            cheapest_dates = calendar.find_cheapest_rt()
            for dates in cheapest_dates:
                #create a separate thread that requests the trips for the cheapest dates
                t = TripRequestThread(self.origin, self.dest, dates[0],
                                      dates[1],
                                      self.start_date.strftime('%Y-%m-%d'),
                                      self.end_date.strftime('%Y-%m-%d'),
                                      self.min_length, self.max_length,
                                      calendar.get_session_id(), trips)
                t.start()
                print "Add trip thread"
                threads.append(t)

        for t in threads:
            t.join()

        #combine all the returned trip data from all the threads
        ita_trips = ITATrips()
        for trip_data in trips:
            print trip_data
            ita_trips._add_trips(trip_data)

        return ita_trips