def parse_page(content, price_dict): tickets = [] results = {} flights = {} try: json_temp = json.loads(content) except: results['ticket'] = tickets results['flight'] = flights return results if json_temp['Status'] == 'SUCCESS': for each_flight_json in json_temp['datalist']: roundflight = RoundFlight() #print '---------------' try: flight_no = each_flight_json['Key'] #print flight_no flight_no_a = flight_no.split('^')[0] flight_no_b = flight_no.split('^')[1] a_num = len(flight_no_a.split('_')) b_num = len(flight_no_b.split('_')) roundflight.stop_A = a_num - 1 roundflight.stop_B = b_num - 1 roundflight.flight_no_A = flight_no_a roundflight.flight_no_B = flight_no_b roundflight.dept_id = each_flight_json['ODO'][0]['OL'] roundflight.dest_id = each_flight_json['ODO'][a_num]['OL'] #print roundflight.dept_id, roundflight.dest_id roundflight.dept_time_A = each_flight_json['ODO'][0]['DD'] roundflight.dest_time_A = each_flight_json['ODO'][a_num - 1]['AD'] if len(roundflight.dept_time_A) < 17: roundflight.dept_time_A = roundflight.dept_time_A + ':00' if len(roundflight.dest_time_A) < 17: roundflight.dest_time_A = roundflight.dest_time_A + ':00' #print roundflight.dept_time_A,roundflight.dest_time_A roundflight.dept_time_B = each_flight_json['ODO'][a_num]['DD'] roundflight.dest_time_B = each_flight_json['ODO'][-1]['AD'] if len(roundflight.dept_time_B) < 17: roundflight.dept_time_B = roundflight.dept_time_B + ':00' if len(roundflight.dest_time_B) < 17: roundflight.dest_time_B = roundflight.dest_time_B + ':00' #print roundflight.dept_time_B, roundflight.dest_time_B roundflight.dept_day = roundflight.dept_time_A.split('T')[0] roundflight.dest_day = roundflight.dept_time_B.split('T')[0] #roundflight.price = each_flight_json['AIP'][0]['EA'] price_key = roundflight.flight_no_A + '^' + roundflight.flight_no_B roundflight.price = price_dict[price_key] roundflight.tax = each_flight_json['AIP'][0]['TX'] roundflight.source = 'jijitong::jijitong' roundflight.seat_type_A = '经济舱' roundflight.seat_type_B = '经济舱' roundflight.currency = 'CNY' #print a_num #print roundflight.seat_type_A plane_no_a = '' airline_a = '' for dept_flight in each_flight_json['ODO'][:a_num]: plane_no_a = plane_no_a + dept_flight['EQ'].encode( 'utf-8') + '_' airline_a = airline_a + dept_flight['COA'].encode( 'utf-8') + '_' #print plane_no_a,airline_a roundflight.plane_no_A = plane_no_a[:-1] roundflight.airline_A = airline_a[:-1] #print roundflight.airline_A,roundflight.plane_no_A plane_no_b = '' airline_b = '' for dest_flight in each_flight_json['ODO'][a_num:]: plane_no_b = plane_no_b + dest_flight['EQ'].encode( 'utf-8') + '_' airline_b = airline_b + dest_flight['COA'].encode( 'utf-8') + '_' roundflight.plane_no_B = plane_no_b[:-1] roundflight.airline_B = airline_b[:-1] #print roundflight.airline_B,roundflight.plane_no_B if a_num == 1: dur_A_temp = each_flight_json['ODO'][0]['ET'] roundflight.dur_A = int(dur_A_temp) * 60 else: dur_A_temp = 0 dur_A_temp2 = 0 for dept_content in each_flight_json['ODO'][:a_num]: dur_A_temp += int(dept_content['ET']) * 60 for x in range(1, a_num): #print x dept_time_str = each_flight_json['ODO'][x - 1]['AD'] #print dept_time_str dest_time_str = each_flight_json['ODO'][x]['DD'] #print dest_time_str dur_A_temp2 += durCal(dept_time_str, dest_time_str) #print dur_A_temp2 roundflight.dur_A = dur_A_temp + dur_A_temp2 if b_num == 1: dur_B_temp = each_flight_json['ODO'][a_num]['ET'] roundflight.dur_B = int(dur_B_temp) * 60 else: dur_B_temp = 0 dur_B_temp2 = 0 for dept_content in each_flight_json['ODO'][a_num:]: dur_B_temp += int(dept_content['ET']) * 60 for x in range(a_num + 1, a_num + b_num): dept_time_str = each_flight_json['ODO'][x - 1]['AD'] dest_time_str = each_flight_json['ODO'][x]['DD'] dur_B_temp2 += durCal(dept_time_str, dest_time_str) roundflight.dur_B = dur_B_temp + dur_B_temp2 for eachflight_content in each_flight_json['ODO']: eachflight = EachFlight() try: eachflight.flight_no = eachflight_content['MA'] eachflight.dept_id = eachflight_content['OL'] eachflight.dest_id = eachflight_content['DL'] eachflight.airline = eachflight_content['COA'] eachflight.plane_no = eachflight_content['EQ'] eachflight.dept_time = eachflight_content['DD'] + ':00' eachflight.dest_time = eachflight_content['AD'] + ':00' eachflight.dur = int(eachflight_content['ET']) * 60 eachflight.flight_key = eachflight.flight_no + '_' + eachflight.dept_id + '_' + eachflight.dest_id eachflight_tuple = (eachflight.flight_no, eachflight.airline, eachflight.plane_no, eachflight.dept_id, \ eachflight.dest_id, eachflight.dept_time, eachflight.dest_time, eachflight.dur) flights[eachflight.flight_key] = eachflight_tuple except Exception, e: #logger.info('Parse this flight failed with error :' + str(e)) continue roundflight_tuple = (roundflight.dept_id, roundflight.dest_id, roundflight.dept_day, roundflight.dest_day, \ roundflight.price, roundflight.tax, roundflight.surcharge, roundflight.currency, roundflight.source, \ roundflight.return_rule, roundflight.flight_no_A, roundflight.airline_A, roundflight.plane_no_A, \ roundflight.dept_time_A, roundflight.dest_time_A, roundflight.dur_A, roundflight.seat_type_A, \ roundflight.stop_A, roundflight.flight_no_B, roundflight.airline_B, roundflight.plane_no_B, \ roundflight.dept_time_B, roundflight.dest_time_B, roundflight.dur_B, roundflight.seat_type_B, \ roundflight.stop_B) tickets.append(roundflight_tuple) except Exception, e: logger.error('Can not parse flight info!' + str(e)) continue
if content == '' or content == None: return tickets, flights try: content_json = json.loads(content) all_flights = content_json['routes'] except Exception, e: return tickets, flights for flight_info in all_flights: roundflight = RoundFlight() roundflight.dept_id = flight_info['from']['routeStr'].split('-')[0] roundflight.dest_id = flight_info['to']['routeStr'].split('-')[0] roundflight.dept_day = flight_info['from']['fromDate'] roundflight.dest_day = flight_info['to']['fromDate'] roundflight.seat_type_A = '经济舱' roundflight.seat_type_B = '经济舱' roundflight.price = flight_info['totalFare'] roundflight.tax = flight_info['totalTax'] roundflight.source = 'lcairRound::lcairRound' segments = flight_info['segments'] goTripLen = int(flight_info['fromSegmentCount'])
return result for each_flight_json in flight_json[1]: roundflight = RoundFlight() try: roundflight.price = int(each_flight_json[0]) + 1 roundflight.tax = int(each_flight_json[3]) + 1 - roundflight.price outbound_info_list = each_flight_json[5][0] inbound_info_list = each_flight_json[5][1] roundflight.dur_A = int(outbound_info_list[5]) * 60 roundflight.dur_B = int(inbound_info_list[5]) * 60 roundflight.dept_id = outbound_info_list[1] roundflight.dest_id = outbound_info_list[3] roundflight.dept_day = day_calculator(outbound_info_list[4]) roundflight.dest_day = day_calculator(inbound_info_list[4]) #parse dept_time and dest_time of each_flight dept_time_A_day = outbound_info_list[4] dept_time_A_day = day_calculator(dept_time_A_day) dept_time_A_mins = int(outbound_info_list[7][0][5]) roundflight.dept_time_A = time_calculator(dept_time_A_day, dept_time_A_mins) dest_time_A_day = outbound_info_list[7][-1][6] dest_time_A_day = day_calculator(dest_time_A_day) dest_time_A_mins = int(outbound_info_list[7][-1][7]) roundflight.dest_time_A = time_calculator(dest_time_A_day, dest_time_A_mins)
roundflight_airport = '' roundflight_plane = '' roundflight_no = '' roundflight.price = int(float(flight_info['adultSalesPrice']) + 1)#解析出数据是小数,取int加1 roundflight.tax = int(float(flight_info['adultTax']) + 1) #roundflight.dur = int(flight_info['tripTime']) * 60 go_info = segments = flight_info['trips'][0] return_info = segments = flight_info['trips'][1] go_segments = go_info['segments'] return_segments = return_info['segments'] roundflight.dept_id = go_segments[0]['departureAirportCode'] roundflight.dest_id = return_segments[0]['departureAirportCode'] dept_time = timeshifter(go_segments[0]['departureTime']) dest_time = timeshifter(return_segments[0]['departureTime']) roundflight.dept_day = dept_time.split('T')[0] roundflight.dest_day = dest_time.split('T')[0] #roundflight.seat_type = '经济舱' roundflight.currency = currency roundflight.source = source flight_aircorp = '' flight_plane = '' flight_no = ''
def parse_page(content, price_dict): tickets = [] results = {} flights = {} try: json_temp = json.loads(content) except: results['ticket'] = tickets results['flight'] = flights return results if json_temp['Status'] == 'SUCCESS': for each_flight_json in json_temp['datalist']: roundflight = RoundFlight() #print '---------------' try: flight_no = each_flight_json['Key'] #print flight_no flight_no_a = flight_no.split('^')[0] flight_no_b = flight_no.split('^')[1] a_num = len(flight_no_a.split('_')) b_num = len(flight_no_b.split('_')) roundflight.stop_A = a_num - 1 roundflight.stop_B = b_num - 1 roundflight.flight_no_A = flight_no_a roundflight.flight_no_B = flight_no_b roundflight.dept_id = each_flight_json['ODO'][0]['OL'] roundflight.dest_id = each_flight_json['ODO'][a_num]['OL'] #print roundflight.dept_id, roundflight.dest_id roundflight.dept_time_A = each_flight_json['ODO'][0]['DD'] roundflight.dest_time_A = each_flight_json['ODO'][a_num-1]['AD'] if len(roundflight.dept_time_A) < 17: roundflight.dept_time_A = roundflight.dept_time_A + ':00' if len(roundflight.dest_time_A) < 17: roundflight.dest_time_A = roundflight.dest_time_A + ':00' #print roundflight.dept_time_A,roundflight.dest_time_A roundflight.dept_time_B = each_flight_json['ODO'][a_num]['DD'] roundflight.dest_time_B = each_flight_json['ODO'][-1]['AD'] if len(roundflight.dept_time_B) < 17: roundflight.dept_time_B = roundflight.dept_time_B + ':00' if len(roundflight.dest_time_B) < 17: roundflight.dest_time_B = roundflight.dest_time_B + ':00' #print roundflight.dept_time_B, roundflight.dest_time_B roundflight.dept_day = roundflight.dept_time_A.split('T')[0] roundflight.dest_day = roundflight.dept_time_B.split('T')[0] #roundflight.price = each_flight_json['AIP'][0]['EA'] price_key = roundflight.flight_no_A + '^' + roundflight.flight_no_B roundflight.price = price_dict[price_key] roundflight.tax = each_flight_json['AIP'][0]['TX'] roundflight.source = 'jijitong::jijitong' roundflight.seat_type_A = '经济舱' roundflight.seat_type_B = '经济舱' roundflight.currency = 'CNY' #print a_num #print roundflight.seat_type_A plane_no_a = '' airline_a = '' for dept_flight in each_flight_json['ODO'][:a_num]: plane_no_a = plane_no_a + dept_flight['EQ'].encode('utf-8') + '_' airline_a = airline_a + dept_flight['COA'].encode('utf-8') + '_' #print plane_no_a,airline_a roundflight.plane_no_A = plane_no_a[:-1] roundflight.airline_A = airline_a[:-1] #print roundflight.airline_A,roundflight.plane_no_A plane_no_b = '' airline_b = '' for dest_flight in each_flight_json['ODO'][a_num:]: plane_no_b = plane_no_b + dest_flight['EQ'].encode('utf-8') + '_' airline_b = airline_b + dest_flight['COA'].encode('utf-8') + '_' roundflight.plane_no_B = plane_no_b[:-1] roundflight.airline_B = airline_b[:-1] #print roundflight.airline_B,roundflight.plane_no_B if a_num == 1: dur_A_temp = each_flight_json['ODO'][0]['ET'] roundflight.dur_A = int(dur_A_temp) * 60 else: dur_A_temp = 0 dur_A_temp2 = 0 for dept_content in each_flight_json['ODO'][:a_num]: dur_A_temp += int(dept_content['ET']) * 60 for x in range(1,a_num): #print x dept_time_str = each_flight_json['ODO'][x-1]['AD'] #print dept_time_str dest_time_str = each_flight_json['ODO'][x]['DD'] #print dest_time_str dur_A_temp2 += durCal(dept_time_str, dest_time_str) #print dur_A_temp2 roundflight.dur_A = dur_A_temp + dur_A_temp2 if b_num == 1: dur_B_temp = each_flight_json['ODO'][a_num]['ET'] roundflight.dur_B = int(dur_B_temp) * 60 else: dur_B_temp = 0 dur_B_temp2 = 0 for dept_content in each_flight_json['ODO'][a_num:]: dur_B_temp += int(dept_content['ET']) * 60 for x in range(a_num+1,a_num+b_num): dept_time_str = each_flight_json['ODO'][x-1]['AD'] dest_time_str = each_flight_json['ODO'][x]['DD'] dur_B_temp2 += durCal(dept_time_str, dest_time_str) roundflight.dur_B = dur_B_temp + dur_B_temp2 for eachflight_content in each_flight_json['ODO']: eachflight = EachFlight() try: eachflight.flight_no = eachflight_content['MA'] eachflight.dept_id = eachflight_content['OL'] eachflight.dest_id = eachflight_content['DL'] eachflight.airline = eachflight_content['COA'] eachflight.plane_no = eachflight_content['EQ'] eachflight.dept_time = eachflight_content['DD'] + ':00' eachflight.dest_time = eachflight_content['AD'] + ':00' eachflight.dur = int(eachflight_content['ET']) * 60 eachflight.flight_key = eachflight.flight_no + '_' + eachflight.dept_id + '_' + eachflight.dest_id eachflight_tuple = (eachflight.flight_no, eachflight.airline, eachflight.plane_no, eachflight.dept_id, \ eachflight.dest_id, eachflight.dept_time, eachflight.dest_time, eachflight.dur) flights[eachflight.flight_key] = eachflight_tuple except Exception, e: #logger.info('Parse this flight failed with error :' + str(e)) continue roundflight_tuple = (roundflight.dept_id, roundflight.dest_id, roundflight.dept_day, roundflight.dest_day, \ roundflight.price, roundflight.tax, roundflight.surcharge, roundflight.currency, roundflight.source, \ roundflight.return_rule, roundflight.flight_no_A, roundflight.airline_A, roundflight.plane_no_A, \ roundflight.dept_time_A, roundflight.dest_time_A, roundflight.dur_A, roundflight.seat_type_A, \ roundflight.stop_A, roundflight.flight_no_B, roundflight.airline_B, roundflight.plane_no_B, \ roundflight.dept_time_B, roundflight.dest_time_B, roundflight.dur_B, roundflight.seat_type_B, \ roundflight.stop_B) tickets.append(roundflight_tuple) except Exception,e: logger.error('Can not parse flight info!' + str(e)) continue
return result for each_flight_json in flight_json[1]: roundflight = RoundFlight() try: roundflight.price = int(each_flight_json[0]) + 1 roundflight.tax = int(each_flight_json[3]) + 1 - roundflight.price outbound_info_list = each_flight_json[5][0] inbound_info_list = each_flight_json[5][1] roundflight.dur_A = int(outbound_info_list[5]) * 60 roundflight.dur_B = int(inbound_info_list[5]) * 60 roundflight.dept_id = outbound_info_list[1] roundflight.dest_id = outbound_info_list[3] roundflight.dept_day = day_calculator(outbound_info_list[4]) roundflight.dest_day = day_calculator(inbound_info_list[4]) #parse dept_time and dest_time of each_flight dept_time_A_day = outbound_info_list[4] dept_time_A_day = day_calculator(dept_time_A_day) dept_time_A_mins = int(outbound_info_list[7][0][5]) roundflight.dept_time_A = time_calculator(dept_time_A_day, dept_time_A_mins) dest_time_A_day = outbound_info_list[7][-1][6] dest_time_A_day = day_calculator(dest_time_A_day) dest_time_A_mins = int(outbound_info_list[7][-1][7]) roundflight.dest_time_A = time_calculator(dest_time_A_day,