# -*- coding: utf-8 -*-
import scrapy
import time
import json
import redis
from AirplaneSpiders.items import FlightBaseItem
from AirplaneSpiders.scrapy_selenium import SeleniumRequest
from scrapy_splash import SplashRequest


# 高敏飞机历史轨迹下载#
class SensitiveFligthBaseSpider(scrapy.Spider):
    name = 'radarbox_flight_base_sensitive'
    allowed_domains = ['rb24.com', 'radarbox.com']
    custom_settings = {
        '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_',
        'DOWNLOAD_DELAY': 6
    }

    def __init__(self, params=None, *args, **kwargs):
        super(SensitiveFligthBaseSpider, 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')
        self.redis_client.close()
        for reg in regs:
            yield SplashRequest('https://www.radarbox.com/data/registration/%s' % reg.decode(), callback=self.fid_parse)
            # yield SeleniumRequest(url='https://www.radarbox.com/data/registration/%s'%reg.decode(), callback= self.fid_parse)

    def fid_parse(self, response):
        try:
            if response.text is None or len(response.text) == 0:
                return
            fids = response.xpath('//tbody/tr/@id').getall()
            for fid in fids:
                if fid.find('_') < 0:
                    continue
                fid = fid.split('_')[0]
                yield scrapy.Request('https://www.radarbox.com/data/get-replay?fid=%s' % fid,
                                     callback=self.live_data_parse, headers=self.settings['HEADERS'])
        except Exception as error:
            print('解析航班id出错,原因是：', error)

    def live_data_parse(self, response):
        try:
            rsp = json.loads(response.text)
            if 'flight' not in rsp:
                return
            base_data = rsp['flight']

            item = FlightBaseItem()
            if 'fid' in base_data:
                item['FlightID'] = '1#%s' % str(base_data['fid'])
            if 'cs' in base_data and base_data['cs'] is not None:
                item['ICAO'] = base_data['cs']
            if 'fnia' in base_data and base_data['fnia'] is not None:
                item['IATA'] = base_data['fnia']
            if 'acr' in base_data and base_data['acr'] is not None:
                item['PlaneReg'] = base_data['acr']
            if 'aporgna' in base_data and base_data['aporgna'] is not None:
                item['TakeoffBase'] = base_data['aporgna']
            if 'aporgic' in base_data and base_data['aporgic'] is not None:
                item['TakeoffBaseICAO'] = base_data['aporgic']
            if 'aporgia' in base_data and base_data['aporgia'] is not None:
                item['TakeoffBaseIATA'] = base_data['aporgia']
            if 'apdstna' in base_data and base_data['apdstna'] is not None:
                item['LandBase'] = base_data['apdstna']
            if 'apdstic' in base_data and base_data['apdstic'] is not None:
                item['LandBaseICAO'] = base_data['apdstic']
            if 'apdstia' in base_data and base_data['apdstia'] is not None:
                item['LandBaseIATA'] = base_data['apdstia']
            if 'depsts' in base_data and base_data['depsts'] is not None:
                item['ScheduleBegTime'] = base_data['depsts'] * 1000
            if 'depc' in base_data and base_data['depc'] is not None:
                item['ActualBegTime'] = base_data['depc'] * 1000
            if 'arrsts' in base_data and base_data['arrsts'] is not None:
                item['ScheduleArrTime'] = base_data['arrsts'] * 1000
            if 'arrc' in base_data and base_data['arrc'] is not None:
                item['ActualArrTime'] = base_data['arrc'] * 1000
            if 'svd' in base_data and base_data['svd'] is not None:
                item['LastTime'] = base_data['svd']
            item['UpdateTime'] = str(int(time.time() * 1000))

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