import time
import json
import scrapy
from pigat.items import PigatItem_beian


class pigat_beian(scrapy.Spider):
	name = 'pigat_beian'

	def start_requests(self):
		headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'}
		url = self.url  # 待爬取 URL
		# url = 'teamssix.com'
		# 备案查询
		beian_url = 'https://www.98api.cn/api/icp.php?url={}'.format(url)
		yield scrapy.Request(beian_url, headers=headers, meta={'url': url}, callback=self.sub_beian)

	def sub_beian(self, response):
		url = response.meta['url']
		print('\n\033[1;33;40m[{}] 正在被动收集 {} 的备案信息……\033[0m'.format(time.strftime('%Y-%m-%d %H:%M:%S'), url))
		beian_json = json.loads(response.text)
		if 'error' not in beian_json:
			beian_name = beian_json['name']  # 备案网站名称
			beian_type = beian_json['type']  # 备案单位类型
			beian_cpy = beian_json['cpy']  # 备案单位名称
			beian_url = beian_json['url']  # 备案网站首页
			beian_license = beian_json['license']  # 备案许可证号
			beian_time = beian_json['time']  # 备案审核时间

			print('\033[1;32;40m[{}] 备案网站名称：{}\n'
			      '[{}] 备案单位类型：{}\n'
			      '[{}] 备案单位名称：{}\n'
			      '[{}] 备案网站首页：{}\n'
			      '[{}] 备案许可证号：{}\n'
			      '[{}] 备案审核时间：{} '
			      '\033[0m'.format(
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_name,
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_type,
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_cpy,
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_url,
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_license,
				time.strftime('%Y-%m-%d %H:%M:%S'), beian_time
			))
		else:
			print('\033[1;31;40m[{}] 未查询到 {} 的备案信息\033[0m'.format(time.strftime('%Y-%m-%d %H:%M:%S'), url))

		item = PigatItem_beian(
			url=url,
			beian_name=beian_name,
			beian_type=beian_type,
			beian_cpy=beian_cpy,
			beian_url=beian_url,
			beian_license=beian_license,
			beian_time=beian_time
		)
		yield item
