Ejemplo n.º 1
0
 def __init__(self):
     self.flight_requests = FlightRequestCollection()
Ejemplo n.º 2
0
class ITADao:

    headers = \
    {
        "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie" : '__utma=269716137.1963755597.1368591497.1368846168.1369958808.3; __utma=241137183.308417330.1368591501.1403974868.1403991047.140; __utmz=241137183.1402970608.133.6.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); searchFormState=%7B%22version%22%3A%220.7.20120825.1%22%2C%22json%22%3A%22%7B%5C%22mode%5C%22%3A%7B%5C%22date%5C%22%3A%5C%22calendar%5C%22%2C%5C%22flightSelection%5C%22%3A%5C%22slice%5C%22%2C%5C%22flightView%5C%22%3A%5C%22slice%5C%22%2C%5C%22pageFlow%5C%22%3A%5C%22calendar%5C%22%2C%5C%22trip%5C%22%3A%5C%22rt%5C%22%2C%5C%22calendarRange%5C%22%3A%5C%2230day%5C%22%7D%2C%5C%22searchForm%5C%22%3A%7B%5C%22mode%5C%22%3A%5C%22advanced%5C%22%2C%5C%22defaults%5C%22%3A%7B%5C%22multiCityRows%5C%22%3A2%7D%2C%5C%22awards%5C%22%3A%5C%22noawards%5C%22%2C%5C%22options%5C%22%3A%7B%5C%22showRoutingCodes%5C%22%3Atrue%2C%5C%22pax%5C%22%3A%5C%22simple%5C%22%7D%7D%7D%22%7D; pricePerMile=%7B%22showControl%22%3Atrue%2C%22showPrice%22%3Atrue%7D; mode.flight=%7B%22view%22%3A%22trip%22%2C%22selection%22%3A%22trip%22%7D; PREF="ID=ddab770e84ffbd166c060d8402d1da5ced52210c7cc1eebfec1ba7ab84841a404416a0bfbf08cb7facfb49969cb56fbd912fb4fc80412f15b2456abb34a0e50f:TM=1403972269:S=052ph5Y8jci3Qu3t"; __utmc=241137183; __utmb=241137183.11.10.1403991047',
        "Cache-Control" : 'no-cache',
        "Content-Length" : '0',
    }

    host = "http://matrix.itasoftware.com"

    path = "/xhr/shop/search"

    url = host + path

    def __init__(self):
        self.flight_requests = FlightRequestCollection()

    def add_specific_flight(self,
                            origins,
                            destinations,
                            depart_date,
                            date_minus=0,
                            date_plus=0,
                            route_language=None,
                            command_line=None):
        self.flight_requests.add_single_flight(origins, destinations,
                                               depart_date, date_minus,
                                               date_plus, route_language,
                                               command_line)
        self.search_type = 'specific'

    def set_calendar_query(self, origin, dest, start, end, min_length,
                           max_length):
        self.origin = origin
        self.dest = dest
        self.start_date = start
        self.end_date = end
        self.min_length = min_length
        self.max_length = max_length
        self.search_type = 'calendar'

    def get_trip_data(self):
        if self.search_type == 'calendar':
            return self.get_calendar_trip_data()
        elif self.search_type == 'specific':
            return self.get_specific_trip_data()
        else:
            print 'Whoops'
            return None

    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

    """
    Gets the trip data from ITA for the destination and date range
    """

    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