# -*- coding: utf-8 -*-
import scrapy
import time
import json
from AirplaneSpiders.items import AirPlaneBaseItem
import requests
import random
import uuid
from AirplaneSpiders.utils import http_get
import os
from requests.exceptions import ProxyError


# 全球飞机基本信息采集引擎（包含普通和高敏）#
class AirplaneBaseSpider(scrapy.Spider):
    name = 'rb24_airplane_base'
    allowed_domains = ['rb24.com']
    start_urls = [
        'https://data.rb24.com/live?zoom=2&bounds=84.243,80,20,-40&designator=iata&ff=false&os=web&adsb=true&asdi=true&ocea=true&mlat=true&sate=true&uat=true&hfdl=true&esti=true&asdex=true&diverted=false&delayed=false&isga=false&ground=true&onair=true&timestamp=%d' % (
                time.time() * 1000 - 120 * 1000)]
    base_api = 'https://data.rb24.com/live-flight-info?fid='

    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(AirplaneBaseSpider, 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):
        for _ in self.start_urls:
            yield scrapy.Request(url=_, headers={'Origin': 'https://www.radarbox.com'}, callback=self.parse)

    def parse(self, response):
        try:
            live_data = json.loads(response.text)
            for key in live_data[0].keys():
                if live_data[0][key] == '':
                    continue
                yield scrapy.Request(self.base_api + key, callback=self.airplane_base_parse,
                                     headers={'Origin': 'https://www.radarbox.com'},
                                     meta={'CUSTOM_FILTER_KEY': 'AirplaneSpiders:AIR_PLANE_BASE_FILTER',
                                           'CUSTOM_FILTER_VAL': live_data[0][key][9]})
        except Exception as e:
            self.logger.error('飞机实时信息解析错误，原因是：', e)

    def airplane_base_parse(self, response):
        item = AirPlaneBaseItem()
        try:
            base_data = json.loads(response.text)
            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:
                item['Model'] = base_data['acd']
            item['image_urls'] = []
            if 'th1' in base_data:
                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)
