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


# 全球飞机基础轨迹采集--数据源：planeradar.ru #
class AirplaneBase3Spider(scrapy.Spider):
    name = 'planeradar_airplane_base'
    allowed_domains = ['planeradar.ru', 'airport-data.com']

    start_urls = [r'http://planeradar.ru/VirtualRadar/-.~DT.F-BrdUp%20%20Rt']
    pic_url = 'http://planeradar.ru/VirtualRadar/AirportDataThumbnails.json?icao=%s&numThumbs=2'

    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(AirplaneBase3Spider, 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:
            res_data = json.loads(response.text)
            for data in res_data['acList']:
                item = AirPlaneBaseItem()
                # item['ID'] = data['Id']
                # item['MSN'] = data['']
                if 'Reg' in data and data['Reg'] is not None:
                    item['Reg'] = data['Reg']
                item['ICAO'] = data['Icao']
                if 'Call' in data and data['Call'] is not None:
                    item['Callsign'] = data['Call']
                if 'Op' in data and data['Op'] is not None:
                    item['Owner'] = data['Op']
                if 'Cou' in data and data['Cou'] is not None:
                    item['Nation'] = data['Cou']
                if 'Type' in data and data['Type'] is not None:
                    item['Model'] = data['Type']
                if 'Year' in data and data['Year'] is not None:
                    item['Age'] = str(
                        int(datetime.now().year) - int(data['Year']))
                # item['Age'] = data[]
                item['UpdateTime'] = str(int(time.time() * 1000))
                item['DataSource'] = 'planeradar.ru'
                if 'PosTime' in data and data['PosTime'] is not None:
                    item['LastTime'] = data['PosTime']
                else:
                    item['LastTime'] = res_data['stm']
                if data['HasPic']:
                    yield scrapy.Request(url=self.pic_url % data['Icao'], callback=self.parse_pic, meta={'item': item})
                else:
                    yield item
        except Exception as e:
            self.logger.error("来自数据源 planeradar.ru 的飞机基础信息解析错误，原因是：", e)

    def parse_pic(self, response):
        item = response.meta['item']
        try:
            data = json.loads(response.text)
            if data['status'] == 200:
                item['image_urls'] = []
                for i in data['data']:
                    item['image_urls'].append(i['image'])
            yield item
        except Exception as e:
            self.logger.error("来自数据源 planeradar.ru 的飞机基础信息中的图片解析错误，原因是：", e)
