# -*- coding: utf-8 -*-
import scrapy
import json
import time
from AirplaneSpiders.items import FlightBaseItem

# 航班信息采集--数据源：planeradar.ru #
class FlightBase3Spider(scrapy.Spider):
    name = 'planeradar_flight_base'
    allowed_domains = ['planeradar.ru']
    start_urls = [r'http://planeradar.ru/VirtualRadar/-.~DT.F-BrdUp%20%20Rt']

    custom_settings = {
        'DOWNLOAD_DELAY': 0.2,
        'BATCH_SAVE_SIZE': 100,
        'PROTO_MODULE_PATH': 'AirplaneSpiders.proto.Plane_pb2',
        'PROTO_CLASS_NAME': 'FligthInfoSets',
        'PROTO_FIELD_NAME': 'FligthInfo',
        'PROTO_SAVE_FILE_PATH': r'/usr/local/spider_data',
        'PROTO_SAVE_FILE_NAME': r'track_data_flight_',
        'ITEM_PIPELINES': {
            'AirplaneSpiders.pipelines.ProtobufSavePipeline': 300
        }
    }

    def __init__(self, params=None, *args, **kwargs):
        super(FlightBase3Spider, self).__init__(*args, **kwargs)
        json_params = json.loads(params)
        if 'job_id' in json_params:
            self.job_id = json_params['job_id']

    def parse(self, response):
        try:
            res_data = json.loads(response.text)
            for data in res_data['acList']:
                item = FlightBaseItem()
                item['FlightID'] = data['Id']
                # item['ICAO'] = data['']
                # item['IATA'] = data['']
                if 'Reg' in data and data['Reg'] is not None:
                    item['PlaneReg'] = data['Reg']
                if 'From' in data and data['From'] is not None:
                    item['TakeoffBase'] = data['From']
                    item['TakeoffBaseICAO'] = data['From'].split(' ')[0]
                # item['TakeoffBaseIATA'] = data['']
                if 'To' in data and data['To'] is not None:
                    item['LandBase'] = data['To']
                    item['LandBaseICAO'] = data['To'].split(' ')[0]
                # item['LandBaseIATA'] = data['']
                # item['ScheduleBegTime'] = data['']
                # item['ActualBegTime'] = data['']
                # item['ScheduleArrTime'] = data['']
                # item['ActualArrTime'] = data['']
                item['UpdateTime'] = str(int(time.time() * 1000))
                if 'PosTime' in data and data['PosTime'] is not None:
                    item['LastTime'] = data['PosTime']
                else:
                    item['LastTime'] = res_data['stm']

                yield item
        except Exception as e:
            self.logger.error("来自数据源planeradar.ru的航班信息采集失败，原因是：", e)
