import json
import logging as logger
import uuid

import scrapy
import validators
from selenium.webdriver.common.keys import Keys

from ForumSpiders.items import ZhihuItem
from ForumSpiders.scrapy_selenium import SeleniumRequest
from ForumSpiders.utils.time_util import pause_for_random


class ZhihuPersonSpider(scrapy.Spider):
    name = 'zhihu_person'

    def __init__(self, params=None, *args, **kwargs):
        super(ZhihuPersonSpider, self).__init__(*args, **kwargs)
        json_params = json.loads(params)
        self.pids = json_params['pids']

    def start_requests(self):
        for pid in self.pids:
            url = 'https://www.zhihu.com/people/{}'.format(pid)
            yield SeleniumRequest(url=url, callback=self.parse, meta={"pid": pid})

    def parse(self, response, **kwargs):
        browser = response.request.meta['driver']
        pid = response.request.meta['pid']
        logger.info("start to get person: {}".format(pid))
        pause_for_random(3)
        close_button = browser.find_element_by_xpath("//button[@class='Button Modal-closeButton Button--plain']")
        if close_button is not None:
            close_button.click()

        index = 0
        max_status_count = 20
        status_list_item = browser.find_elements_by_xpath(
            '//div[@id="Profile-activities"]//div[@class="List-item"]')
        while index < max_status_count:
            pause_for_random(2)
            if index >= len(status_list_item):
                logger.info("已遍历{}条，进行翻页加载操作...".format(index))
                browser.find_element_by_css_selector("body").send_keys(Keys.END)
                pause_for_random(0.5)
                browser.find_element_by_css_selector("body").send_keys(Keys.ARROW_UP)
                browser.find_element_by_css_selector("body").send_keys(Keys.ARROW_DOWN)
                browser.find_element_by_css_selector("body").send_keys(Keys.ARROW_DOWN)
                browser.find_element_by_css_selector("body").send_keys(Keys.ARROW_DOWN)
                pause_for_random(3)
                status_list_item = browser.find_elements_by_xpath(
                    '//div[@id="Profile-activities"]//div[@class="List-item"]')
                if index >= len(status_list_item):
                    logger.info("已加载该用户的全部{}条动态".format(len(status_list_item)))
                    break
            try:
                current_status = status_list_item[index]
                current_status_content = current_status.find_element_by_xpath('.//div[contains(@class,"ContentItem ")]')
                current_status_type = current_status_content.get_attribute('itemprop')
                data_za_extra_module = current_status_content.get_attribute("data-za-extra-module")
                data_zop = current_status_content.get_attribute("data-zop")
                if data_za_extra_module is None:
                    logger.info("似乎遍历到了一条非回答的Item，跳过这一条")
                    index += 1
                    continue
                data = json.loads(data_za_extra_module)
                sid = json.loads(data_zop)['itemId']
                status_title = json.loads(data_zop)['title']
                status_info = {
                    "item_id": str(uuid.uuid4()).replace("-", ""),
                    "content_type": current_status_type,
                    "answer_id": sid,
                    "question_title": status_title,
                    "answer_link": current_status_content.find_element_by_xpath(".//h2//a").get_attribute("href"),
                    "author_name": json.loads(current_status_content.get_attribute("data-zop"))['authorName'],
                    "author_link": current_status_content.find_element_by_xpath(
                        '//meta[@itemprop="url"]').get_attribute('content'),
                    "upvote_num": data["card"]["content"]["upvote_num"],
                    "comment_num": data["card"]["content"]["comment_num"],
                    "date_created": current_status.find_element_by_xpath(
                        ".//div[@class='ActivityItem-meta']/span[2]").text,
                }
                expand_more_button = current_status_content.find_element_by_xpath(
                    "//button[contains(@class, 'ContentItem-more')]")
                if expand_more_button is not None:
                    try:
                        expand_more_button.click()
                        pause_for_random(1.5)
                    except:
                        logger.error("展开按钮被遮挡")
                content_text = current_status_content.find_element_by_xpath(
                    ".//span[contains(@class, 'CopyrightRichText-richText')]").text
                status_info['content_text'] = content_text

                imgs = current_status_content.find_element_by_class_name('RichContent-inner').find_elements_by_tag_name(
                    'img')
                status_info['image_urls'] = []
                for img in imgs:
                    img_url = img.get_attribute('src')
                    if validators.url(img_url):
                        status_info['image_urls'].append(img_url)
                status_info['author_id'] = status_info['author_link'].split('/')[-1]
                status_item = ZhihuItem()
                for key in status_info:
                    status_item[key] = status_info[key]
                logger.info("{}: {}".format(status_item['author_name'], status_item['content_text'][:50]))
                yield status_item
            except Exception as e:
                logger.error(e)
            index += 1
        if index >= max_status_count:
            logger.info("最多获取{}条动态，已经全部获取到。".format(max_status_count))
