from bs4 import BeautifulSoup
import math
import json
#from dynaconf import settings as Config
from spider.config.settings import Config
from spider.tools.logtools import Logger
from spider.tools.commontools import getUUID, Timer
from spider.tools.webtools import myGetRequest
from spider.tools.mqtools import rabbitmqConnection
from spider.service.RegionService import RegionService
from urllib3.exceptions import MaxRetryError
import traceback
import os

currentFileName = os.path.basename(os.path.realpath(__file__))

logger = Logger(currentFileName.replace('.py', ''))

regionService = RegionService()

loupanNumPerPage = 10

class ProcessTownByPageQueue:
    def __init__(self):
        # self.queueName = queueName
        # 初始化消息队列
        self.mqObj = rabbitmqConnection()
        # self.myConnection = self.mqObj.getRabbitmqConnection()
        self.readChannel = self.mqObj.getChannel(processQueue)
        # 获取runId
        self.run_id = getUUID()

    # 读取队列消息
    def callback(self, ch, method, properties, message):
        try:
            self.parse(message)
            # 当正常完成任务，会反馈给rabbitmq
            ch.basic_ack(delivery_tag=method.delivery_tag)
        except MaxRetryError:
            ch.basic_reject(delivery_tag=method.delivery_tag)
        except Exception:
            # 发告警
            # 打印异常
            # traceback.print_exc()
            # 推送异常
            error_message = traceback.format_exc()
            logger.error(error_message)
            ch.basic_reject(delivery_tag=method.delivery_tag)
            dict = {'run_id': self.run_id, 'error_message': error_message}
            self.mqObj.pushMessageToMq(Config.ERROR_QUEUE, json.dumps(dict))

    def run(self):
        try:
            # 设置预处理数量，可用来分配每个消费者的处理数量，并发消费时需要用
            self.readChannel.basic_qos(prefetch_count=50)
            # 告诉rabbitmq，用callback来接收消息
            self.readChannel.basic_consume(processQueue, self.callback)

            logger.info('开始读取%s队列' % processQueue)
            # 开始接收信息，并进入阻塞状态，队列里有信息才会调用callback进行处理
            self.readChannel.start_consuming()
        except Exception:
            # 打印异常
            # traceback.print_exc()
            # 推送异常
            error_message = traceback.format_exc()
            logger.error(error_message)
            dict = {'run_id': self.run_id, 'error_message': error_message}
            self.mqObj.pushMessageToMq(Config.ERROR_QUEUE, json.dumps(dict))

    # 解析队列消息，业务逻辑
    def parse(self, message):
        dictObj = json.loads(message)

        province_id = dictObj.get('province_id')
        province_name = dictObj.get('province_name')
        city_id = dictObj.get('city_id')
        city_name = dictObj.get('city_name')
        area_id = dictObj.get('area_id')
        area_name = dictObj.get('area_name')
        town_id=dictObj.get('town_id')
        town_name=dictObj.get('town_name')
        townUrlByPage = dictObj.get('townUrlByPage')

        logger.info('处理[%s]市[%s]区，分页url:[%s] ' % (city_name,area_name, townUrlByPage))
        # 打开分页url，抓取信息
        status_code, text, content = myGetRequest(townUrlByPage, Config.PROXY_FLAG)

        soup = BeautifulSoup(text,"html.parser")

        infoSoup = soup.find_all('div',class_='info')

        for info in infoSoup:
            title = info.find_all('div',class_='title')[0].find_all('a',attrs={'data-click-evtid':'15432'})[0].getText().strip()
            community_name = title.split(' ')[0]
            unit_price = info.find_all('div',class_='unitPrice')[0].find_all('span',class_='number')[0].getText().strip()
            deal_date = info.find_all('div',class_='dealDate')[0].getText().strip()
            url = info.find_all('div',class_='title')[0].find_all('a',attrs={'data-click-evtid':'15432'})[0]['href']
            # 推送商圈信息
            # 组装数据推送到区域队列
            dict = {
                'province_id': province_id,
                'province_name': province_name,
                'city_id': city_id,
                'city_name': city_name,
                'area_id': area_id,
                'area_name': area_name,
                'town_id': town_id,
                'town_name': town_name,
                'title': title,
                'deal_date': deal_date,
                'community_name':community_name,
                'unit_price':unit_price,
                'url': url
            }
            # 推送本次结果
            self.pushResult(dict)


    def pushResult(self, dict):
        # 推送小区明细队列
        self.mqObj.pushMessageToMq(targetQueue, json.dumps(dict))


if __name__ == '__main__':
    # 定义当前处理队列
    processQueue = Config.TOWN_BY_PAGE_QUEUE
    # 定义处理完成后推送的队列
    targetQueue = Config.CHENGJIAO_DETAIL_QUEUE

    tq = ProcessTownByPageQueue()
    tq.run()
