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

# 全球飞机实时轨迹采集--数据源：planemapper.com #
class AirplaneLive4Spider(scrapy.Spider):
    name = 'planemapper_airplane_live'
    allowed_domains = ['planemapper.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
        }
    }

    live_url = 'https://www.planemapper.com/php_ctr/flightmap_ctr.php'  # post请求所有飞行中的飞机
    # post请求指定飞机的实时位置和速度、高度信息
    plane_live_url = 'https://www.planemapper.com/php_ctr/flight_ctr.php'
    plane_base_url = 'https://www.planemapper.com/aircrafts/'  # 使用注册号查询飞机的基本信息，需要解析网页提取数据
    airport_url = 'https://www.planemapper.com/flights/'  # 使用航班号查询机场等信息，需要解析网页提取数据

    def __init__(self, params=None, *args, **kwargs):
        super(AirplaneLive4Spider, 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):
        yield scrapy.FormRequest(url=self.live_url, formdata={'command': '5'}, callback=self.parse)
        # yield scrapy.Request(url=self.live_url, method='POST', body=json.dumps({'command': '5'}), headers={'Content-Type': 'application/json'}, callback=self.parse)

    def parse(self, response):
        try:
            live_data = json.loads(response.text)
            for data in live_data:
                item = AirPlaneLiveItem()
                item['FlightID'] = '4#%s' % str(data[0])
                item['PlaneREG'] = data[2]
                item['source'] = 'planemapper'
                item['UpdateTime'] = str(int(time.time() * 1000))
                item['LastTime'] = str(int(time.time() * 1000))

                form_data = {'cmd': '2', 'Icao': data[0], 'TrackSelOld': str(
                    int(time.time() * 1000))}
                yield scrapy.FormRequest(url=self.plane_live_url, formdata=form_data,
                                         callback=self.parse_plane_live_info, meta={'item': deepcopy(item)})
                # yield scrapy.Request(url=self.plane_live_url, callback=self.parsePlaneLiveInfo, method='POST', body=json.dumps(formdata), meta={'item', item})
        except Exception as e:
            self.logger.error("来自数据源planemapper的飞机实时数据解析失败，原因是：", e)

    def parse_plane_live_info(self, response):
        item = response.request.meta['item']
        try:
            data = json.loads(response.text)[0]
            if data == 'error':
                self.logger.error("网络原因导致来自数据源planemapper的飞机实时位置信息解析失败")
            else:
                item['Latitude'] = data[2]
                item['Longitude'] = data[3]
                if data[4] != '':
                    item['Height'] = str(int(int(data[4]) * 0.3048))  # 英尺转米
                item['Speed'] = data[5]
                yield item
                # formdata = {'command': '2', 'Icao': item['FlightID']}
                # yield scrapy.FormRequest(url=self.live_url, formdata=formdata, callback=self.parseAirportInfo, meta={'item': deepcopy(item)})
        except Exception as e:
            self.logger.error("来自数据源planemapper的飞机实时位置信息解析失败，原因是：", e)

    def parse_plane_base_info(self, response):
        try:
            pass
        except Exception as e:
            self.logger.error("来自数据源planemapper的飞机基础信息解析失败， 原因是：", e)

    def parse_airport_info(self, response):
        item = response.request.meta['item']
        try:
            base_data = json.loads(response.text)[0]
            flight_no = base_data.split('|')[0]  # 提取航班号用于查询航班机场等信息
            if flight_no == '':
                yield item
            else:
                yield scrapy.Request(url=self.airport_url + flight_no, meta={'item': deepcopy(item)},
                                     callback=self.parse_airport_info_2)
        except Exception as e:
            self.logger.error("来自数据源planemapper的机场信息解析失败， 原因是：", e)

    def parse_airport_info_2(self, response):
        item = response.meta['item']
        try:
            selector = etree.HTML(response.text)
            item['origAirport'] = \
            selector.xpath("(//div[@class='container']//div[contains(@class, 'panel')]//div/h3[1]/b)[1]")[0].text
            item['destAirport'] = \
            selector.xpath("(//div[@class='container']//div[contains(@class, 'panel')]//div/h3[1]/b)[2]")[0].text

            yield item
        except Exception as selector:
            self.logger.error("来自数据源planemapper的机场信息解析失败， 原因是：", selector)
