def __init__(self, flight_id=1, departure_date=datetime.datetime.today(), monthly=0):
     self.monthly = monthly
     try:
         # first of all we'll take cookies from step one
         db_cookies = Cookie.objects.order_by('-when_created')[0]
         for cookie_string in json.loads(db_cookies.cookie_keys):
             cookie_values = safe_list_get(cookie_string.split(u';'), 0)
             cookie_key = safe_list_get(cookie_values.split(u'='), 0)
             cookie_value = u'='.join(cookie_values.split(u'=')[1:])
             self.cookies[cookie_key] = cookie_value
             # print cookie_key, cookie_value
     except Cookie.DoesNotExist:
         self.log_message("ERROR: There's no cookies from step one!")
     try:
         self.flight = Flight.objects.get(pk=flight_id)
     except Flight.DoesNotExist:
         self.log_message("ERROR: There's no flight with such id!")
     # then change parameters, if needed
     today = datetime.datetime.strptime(departure_date, "%Y-%m-%d").date()
     nextmonth = today + relativedelta(days=7)
     self.PARSE_SETTINGS = {
         'originAirport': self.flight.origin,
         'destinationAirport': self.flight.destination,
         'travelMonth': str(today.month),
         'travelDay': str(today.day),
         'nextMonth': str(nextmonth.month),
         'nextMonthDay': str(nextmonth.day),
         'tripType': 'oneWay',
         'adultPassengerCount': '1',
         'childPassengerCount': '0',
         'flightNumber': self.flight.flight_number,
         'departureDate': today.strftime('%m/%d/%Y'),  # THIS IS REALLY IMPORTANT!!!!
     }
 def parse_seat(self, seat, flight, cabin):
     available = False
     extra = 0
     name = safe_list_get(seat.xpath('.//img[contains(@class,"seat-img")]/@alt').extract(), 0) or 'EXIT'
     seat_cabin = Seat.SEAT_CLASS_DICT.get(cabin, 0)
     available_image_src = safe_list_get(seat.xpath('.//img[contains(@class,"seat-img")]/@src').extract(), 0)
     if available_image_src == u'/content/images/seatMap/seat-available.png':
         available = True
         import re
         extra_string = safe_list_get(seat.xpath('.//img[contains(@class,"seat-mpp-sym")]/@src').extract(), 0, '')
         extra = re.sub('[^0-9]', '', extra_string) or 0
     seat = Seat.objects.create(
         flight=flight,
         name=name,
         cabin=seat_cabin,
         extra=extra,
         available=available
     )
     seat.save()
 def parse_seats(self, flight, selector, cabin):
     # first off lets delete old seats
     flight.seat_set.filter(cabin=Seat.SEAT_CLASS_DICT.get(cabin, 0)).delete()
     if not self.seat_legend:
         # if the seat legend is empty we'll try to fill it with data
         self.parse_legend(selector)
     if self.seat_legend:
         # saving legend to db
         try:
             flight.one_star = self.seat_legend.get('1', 0)
             flight.two_stars = self.seat_legend.get('2', 0)
             flight.three_stars = self.seat_legend.get('3', 0)
             flight.four_stars = self.seat_legend.get('4', 0)
         except IndexError:
             self.log_message('ERROR: saving legend')
     #saving seats as html
     html = safe_list_get(selector.xpath('//div[@id="seatMapContainer"]').extract(), 0)
     setattr(flight, cabin+'_html', html)
     #saving seats to db as objects
     seat_list = selector.xpath('//div[contains(@class,"seat-wrapper")]')
     for seat in seat_list:
         self.parse_seat(seat, flight, cabin)
     flight.save()