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


# 高敏飞机基本信息采集引擎#
class SensitiveAirplaneBaseSpider(scrapy.Spider):
    name = 'radarbox_airplane_base_sensitive'
    allowed_domains = ['radarbox.com']
    custom_settings = {
        'PROTO_MODULE_PATH': 'AirplaneSpiders.proto.Plane_pb2',
        'PROTO_CLASS_NAME': 'PlaneInfoSets',
        'PROTO_FIELD_NAME': 'PlaneInfo',
        'PROTO_SAVE_FILE_PATH': r'/usr/local/spider_data',
        'PROTO_SAVE_FILE_NAME': r'target_data_sensitiveplane_',
        'IMAGES_STORE': r'/usr/local/temp_image/plane',
        'IMAGES_RESULT_FIELD': 'Image',
        'ZIP_FILE_NAME': 'image_data_plane_',
        'ITEM_PIPELINES': {
            'scrapy.pipelines.images.ImagesPipeline': 1,
            'AirplaneSpiders.pipelines.ProtobufSavePipeline': 300
        }
    }

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

    def start_requests(self):
        self.redis_client = redis.Redis(host=self.settings['REDIS_HOST'], port=int(self.settings['REDIS_PORT']),
                                        password=self.settings['REDIS_PWD'])
        regs = self.redis_client.smembers('AirplaneSpiders:sensitiveReg')
        # regs = self.redisClient.smembers('twPlaneReg')

        decode_reg = ''
        for reg in regs:
            decode_reg = reg.decode()
            # time.sleep(1)
            yield scrapy.Request('https://www.radarbox.com/search/advanced?q=%s' % decode_reg,
                                 callback=self.base_item_parse,
                                 meta={'reg': decode_reg, 'proxy': 'http://192.168.199.243:10809'})

    def base_item_parse(self, response):
        item = AirPlaneBaseItem()
        try:
            if response.text is None or len(response.text) == 0:
                return
            rsp = json.loads(response.text)
            base_data = None
            if rsp is not None:
                if 'flights' in rsp and len(rsp['flights']) > 0:
                    base_data = rsp['flights'][0]
                elif 'registrations' in rsp and len(rsp['registrations']) > 0:
                    base_data = rsp['registrations'][0]

            if base_data is None:
                return
            if 'acr' in base_data:
                item['Reg'] = base_data['acr']
            if 'ms' in base_data:
                item['ICAO'] = base_data['ms']
            if 'sq' in base_data:
                item['Callsign'] = base_data['sq']
            if 'alna' in base_data:
                item['Owner'] = base_data['alna']
            if 'acd' in base_data and base_data['acd'] is not None and base_data['acd'] != '':
                item['Model'] = base_data['acd']
            elif 'act' in base_data:
                item['Model'] = base_data['act']
            item['image_urls'] = []
            if 'th1' in base_data and base_data['th1'] is not None:
                item['image_urls'].append('https://cdn.radarbox.com/photo/' + base_data['th1'])
            if 'acffdate' in base_data:
                item['Age'] = base_data['acffdate']

            item['DataSource'] = 'radarbox'
            item['UpdateTime'] = str(int(time.time() * 1000))
            yield item

        except Exception as e:
            self.logger.error('飞机基础信息解析错误，原因是：', e)

    def closed(self, reason):
        self.redis_client.close()
