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


# 全球飞机轨迹信息采集
# radarbox实时轨迹信息中的时间戳是和服务器时区一致的，所以后面部署时可手动修改服务器时区为utc+8
class AirplaneLiveSpider(scrapy.Spider):
    name = 'rb24_airplane_live'
    allowed_domains = ['rb24.com']
    custom_settings = {
        'PROTO_MODULE_PATH': 'AirplaneSpiders.proto.Plane_pb2',
        'PROTO_CLASS_NAME': 'FligthTrackpointInfoSets',
        'PROTO_FIELD_NAME': 'FligthTrackpointInfo',
        'PROTO_SAVE_FILE_PATH': r'/usr/local/spider_data',
        'PROTO_SAVE_FILE_NAME': r'track_data_plane_',
        'ITEM_PIPELINES': {
            'AirplaneSpiders.pipelines.ProtobufSavePipeline': 300,
            'AirplaneSpiders.pipelines.FlightIdSavePipeline': 301
        }
    }

    def __init__(self, params=None, *args, **kwargs):
        super(AirplaneLiveSpider, self).__init__(*args, **kwargs)
        json_params = json.loads(params)
        if 'job_id' in json_params:
            self.job_id = json_params['job_id']
        self.liveDataApi = 'https://data.rb24.com/live?airline=&station=&aircraft=&airport=&fn=&far=&fms=&zoom=3&flightid=&bounds=84.243,180,-81.238,-180&designator=iata&ff=false&os=web&adsb=true&adsbsat=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&blocked=false&class[]=?,A,B,C,G,H,M&'
        # self.liveDataApi = 'https://data.rb24.com/live?zoom=2&bounds=84.243,180,-81.238,-180&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&'

    def start_requests(self):
        yield scrapy.Request(self.liveDataApi + 'timestamp=%d' % (time.time() * 1000 - 30 * 1000),
                             headers={'Origin': 'https://www.radarbox.com'})  # 若时间戳为当前时间，则返回结果中有大量的空数据，所以后退十秒
        # yield SplashRequest(url = self.liveDataApi + 'timestamp=%d'%(time.time()*1000 - 30*1000), callback = self.parse_page)
        # yield SeleniumRequest(url = self.liveDataApi, callback= self.parse_page)

    def parse(self, response):
        try:
            live_data = json.loads(response.text)
            for key in live_data[0].keys():
                if live_data[0][key] == '':
                    continue
                item = AirPlaneLiveItem()
                item['FlightID'] = '1#%s' % str(key)
                item['FlightIATA'] = live_data[0][key][0]
                item['Latitude'] = live_data[0][key][1]
                item['Longitude'] = live_data[0][key][2]
                item['LastTime'] = live_data[0][key][3]
                if live_data[0][key][4] is not None:
                    item['Height'] = int(live_data[0][key][4]) * 0.3048  # 英尺转米
                item['model'] = live_data[0][key][5]
                item['Speed'] = live_data[0][key][6]
                item['Angle'] = live_data[0][key][7]
                item['source'] = 'radarbox'
                item['PlaneREG'] = live_data[0][key][9]
                item['origAirport'] = live_data[0][key][10]
                item['destAirport'] = live_data[0][key][11]
                item['FlightICAO'] = live_data[0][key][12]
                item['vs'] = live_data[0][key][13]
                item['UpdateTime'] = str(int(time.time() * 1000))
                yield item
        except Exception as e:
            self.logger.error('飞机实时信息解析错误，原因是：', e)

    def parse_page(self, response):
        # 通过selenium获取数据
        # broswer = response.request.meta['driver']
        # btn = broswer.find_element_by_xpath('//a[contains(text(),"Raw Data")]')
        # btn.click()
        # predata = broswer.find_element_by_xpath('//pre[contains(@class,"data")]')
        # jsonData = predata.text
        # 通过splash获取数据
        json_data = response.xpath('//pre/text()').get()
        if json_data is None or len(json_data) == 0:
            return
        points_obj = json.loads(json_data)[0]
        if points_obj is None:
            return
        for i in points_obj.keys():
            point = points_obj[i]
            if len(point) == 0:
                continue
            item = AirPlaneLiveItem()
            item['FlightID'] = i
            item['FlightIATA'] = point[0]
            item['Latitude'] = point[1]
            item['Longitude'] = point[2]
            item['LastTime'] = point[3]
            if point[4] is not None:
                item['Height'] = int(point[4]) * 0.3048  # 英尺转米
            item['model'] = point[5]
            item['Speed'] = point[6]
            item['Angle'] = point[7]
            item['source'] = 'radarbox'
            item['PlaneREG'] = point[9]
            item['origAirport'] = point[10]
            item['destAirport'] = point[11]
            item['FlightICAO'] = point[12]
            item['vs'] = point[13]
            item['UpdateTime'] = str(int(time.time() * 1000))
            yield item
