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


# 全球飞机基础轨迹采集--数据源：flightradar24 #
class AirplaneBase2Spider(scrapy.Spider):
    name = 'flightradar24_airplane_base'
    allowed_domains = ['flightradar24.com']
    start_urls = [
        'https://data-live.flightradar24.com/zones/fcgi/feed.js?bounds=82.33,-84.66,-450.16,450.16&faa=1&satellite=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=14400&gliders=1&stats=1']
    base_api = 'https://data-live.flightradar24.com/clickhandler/?version=1.5&flight='

    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_plane_',
        'IMAGES_STORE': r'/usr/local/temp_image/plane',
        'IMAGES_RESULT_FIELD': 'Image',
        'ZIP_FILE_NAME': 'image_data_plane_',
        'BATCH_SAVE_SIZE': 100,
        'ITEM_PIPELINES': {
            'scrapy.pipelines.images.ImagesPipeline': 1,
            'AirplaneSpiders.pipelines.ProtobufSavePipeline': 300
        }
    }

    def __init__(self, params=None, *args, **kwargs):
        super(AirplaneBase2Spider, 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:
            live_data = json.loads(response.text)
            for key in live_data.keys():
                if key == 'stats' or key == 'full_count' or key == 'version':
                    continue
                # yield scrapy.Request(self.base_api + key, callback=self.airplane_base_parse, meta={'CUSTOM_FILTER_KEY':'AirplaneSpiders:AIR_PLANE_BASE_FILTER','CUSTOM_FILTER_VAL':liveData[key][9]})
                yield scrapy.Request(self.base_api + key, callback=self.airplane_base_parse)
        except Exception as e:
            self.logger.error("来自数据源 flightradar24 的飞机实时信息解析错误，原因是：", e)

    def airplane_base_parse(self, response):
        item = AirPlaneBaseItem()
        try:
            base_data = json.loads(response.text)
            if base_data is None:
                return
            if 'msn' in base_data['aircraft'] and base_data['aircraft']['msn'] is not None:
                item['MSN'] = base_data['aircraft']['msn']
            item['Reg'] = base_data['aircraft']['registration']
            item['ICAO'] = base_data['aircraft']['hex']
            item['Callsign'] = base_data['identification']['callsign']
            if 'owner' in base_data['aircraft'] and base_data['aircraft']['owner'] is not None:
                item['Owner'] = base_data['owner']
            # 国家代码
            item['Nation'] = base_data['aircraft']['countryId']
            item['Model'] = base_data['aircraft']['model']['code']
            item['image_urls'] = []
            if base_data['aircraft']['images'] is not None:
                # 图片资源有thumbnails,medium,large三种大小
                for i in base_data['aircraft']['images']['medium']:
                    item['image_urls'].append(i['src'])
            if 'age' in base_data['aircraft'] and base_data['aircraft']['age'] is not None:
                item['Age'] = base_data['aircraft']['age']
            item['UpdateTime'] = str(int(time.time() * 1000))
            item['DataSource'] = 'flightradar24'
            item['LastTime'] = str(base_data['firstTimestamp']) + '000'

            yield item

        except Exception as e:
            self.logger.error("来自数据源 flightradar24 的飞机基础信息解析错误，原因是：", e)
