# Path: config/base_settings.py
# USE_PROXY = False
# 
# Path: service/account.py
# def login(username=None, password=None):
#     """
#     登录API
#     :return: True: 登录成功; False: 登录失败
#     """
#     if username is None or password is None:
#         username = account.username
#         password = account.password
#     ctrl.BEING_LOG = True
#     if check_login_status():
#         ctrl.BEING_LOG = False
#         return True
# 
#     error_times = 0
#     while True:
#         try:
#             # logger.debug("before proxy")
#             update_proxy()
#             # logger.debug("before cookie")
#             update_cookies()
#             # logger.debug("after cookie")
#             busername = change_to_base64(username)
#             bpassword = change_to_base64(password)
#             captcha = get_captcha()
#             logger.info('验证码识别结果：%s' % captcha)
#             form_data = url_login.get('form_data')
#             form_data.__setitem__('j_validation_code', captcha)
#             form_data.__setitem__('j_username', busername)
#             form_data.__setitem__('j_password', bpassword)
# 
#             resp = requests.post(url=url_login.get('url'), headers=url_login.get('headers'), data=form_data,
#                                  cookies=ctrl.COOKIES, proxies=ctrl.PROXIES, timeout=TIMEOUT)
#             if resp.text.find(username + '，欢迎访问') != -1:
#                 # 网站调整了逻辑，下面这句不用了
#                 # print(resp.cookies)
#                 # ctrl.COOKIES.__delitem__('IS_LOGIN')
#                 # ctrl.COOKIES.set('IS_LOGIN', 'true', domain='www.pss-system.gov.cn/sipopublicsearch/patentsearch')
#                 jsession = ctrl.COOKIES.get('JSESSIONID')
#                 resp.cookies.__delitem__('JSESSIONID')
#                 resp.cookies.set('JSESSIONID', jsession, domain='www.pss-system.gov.cn')
#                 update_cookies(resp.cookies)
#                 requests.post(
#                     'http://www.pss-system.gov.cn/sipopublicsearch/patentsearch/showViewList-jumpToView.shtml',
#                     cookies=ctrl.COOKIES, proxies=ctrl.PROXIES)
#                 ctrl.BEING_LOG = False
#                 logger.info('登录成功')
#                 return True
#             else:
#                 if error_times > 5:
#                     break
#                 logger.error('登录失败')
#                 error_times += 1
#         except Exception as e:
#             logger.error(e)
# 
#     ctrl.BEING_LOG = False
#     return False


import time
import controller as ctrl
from logbook import Logger
from requests.utils import dict_from_cookiejar
from scrapy.downloadermiddlewares.retry import RetryMiddleware
from config.base_settings import USE_PROXY
from service.account import login
    def process_request(self, request, spider):
        if USE_PROXY and ctrl.PROXIES is not None:
            request.meta['proxy'] = "http://%s" % (ctrl.PROXIES.get('http'))
        if ctrl.COOKIES is not None:
            request.cookies = dict_from_cookiejar(ctrl.COOKIES)

    def process_response(self, request, response, spider):
        body = response.body_as_unicode()
        # logger.info(body.find('window.location.href = contextPath +"/portal/uilogin-forwardLogin.shtml";'))
        # logger.info(body.find('访问受限'))
        # logger.info(response.status)
        if response.status == 404 or response.status == 417:
            pass
            # logger.info(body)
        if body.find('window.location.href = contextPath +"/portal/uilogin-forwardLogin.shtml";') != -1 or body.find(
                '访问受限') != -1 or response.status == 404:
            logger.info('未登录，登陆中，请稍后···')
            login_ok = False
            if ctrl.BEING_LOG is False:
                login_ok = login()
            while ctrl.BEING_LOG:
                time.sleep(1)
            if login_ok:
                return self._retry(request, 'unlogin', spider)
        return response

    def process_exception(self, request, exception, spider):
        logger.error(exception)
        login_ok = False
        if ctrl.BEING_LOG is False:

# Path: config/base_settings.py
# USE_PROXY = False
# 
# Path: service/account.py
# def login(username=None, password=None):
#     """
#     登录API
#     :return: True: 登录成功; False: 登录失败
#     """
#     if username is None or password is None:
#         username = account.username
#         password = account.password
#     ctrl.BEING_LOG = True
#     if check_login_status():
#         ctrl.BEING_LOG = False
#         return True
# 
#     error_times = 0
#     while True:
#         try:
#             # logger.debug("before proxy")
#             update_proxy()
#             # logger.debug("before cookie")
#             update_cookies()
#             # logger.debug("after cookie")
#             busername = change_to_base64(username)
#             bpassword = change_to_base64(password)
#             captcha = get_captcha()
#             logger.info('验证码识别结果：%s' % captcha)
#             form_data = url_login.get('form_data')
#             form_data.__setitem__('j_validation_code', captcha)
#             form_data.__setitem__('j_username', busername)
#             form_data.__setitem__('j_password', bpassword)
# 
#             resp = requests.post(url=url_login.get('url'), headers=url_login.get('headers'), data=form_data,
#                                  cookies=ctrl.COOKIES, proxies=ctrl.PROXIES, timeout=TIMEOUT)
#             if resp.text.find(username + '，欢迎访问') != -1:
#                 # 网站调整了逻辑，下面这句不用了
#                 # print(resp.cookies)
#                 # ctrl.COOKIES.__delitem__('IS_LOGIN')
#                 # ctrl.COOKIES.set('IS_LOGIN', 'true', domain='www.pss-system.gov.cn/sipopublicsearch/patentsearch')
#                 jsession = ctrl.COOKIES.get('JSESSIONID')
#                 resp.cookies.__delitem__('JSESSIONID')
#                 resp.cookies.set('JSESSIONID', jsession, domain='www.pss-system.gov.cn')
#                 update_cookies(resp.cookies)
#                 requests.post(
#                     'http://www.pss-system.gov.cn/sipopublicsearch/patentsearch/showViewList-jumpToView.shtml',
#                     cookies=ctrl.COOKIES, proxies=ctrl.PROXIES)
#                 ctrl.BEING_LOG = False
#                 logger.info('登录成功')
#                 return True
#             else:
#                 if error_times > 5:
#                     break
#                 logger.error('登录失败')
#                 error_times += 1
#         except Exception as e:
#             logger.error(e)
# 
#     ctrl.BEING_LOG = False
#     return False


# Path: crawler/middlewares.py
import time
import controller as ctrl
from logbook import Logger
from requests.utils import dict_from_cookiejar
from scrapy.downloadermiddlewares.retry import RetryMiddleware
from config.base_settings import USE_PROXY
from service.account import login

    def process_request(self, request, spider):
        if USE_PROXY and ctrl.PROXIES is not None:
            request.meta['proxy'] = "http://%s" % (ctrl.PROXIES.get('http'))
        if ctrl.COOKIES is not None:
            request.cookies = dict_from_cookiejar(ctrl.COOKIES)

    def process_response(self, request, response, spider):
        body = response.body_as_unicode()
        # logger.info(body.find('window.location.href = contextPath +"/portal/uilogin-forwardLogin.shtml";'))
        # logger.info(body.find('访问受限'))
        # logger.info(response.status)
        if response.status == 404 or response.status == 417:
            pass
            # logger.info(body)
        if body.find('window.location.href = contextPath +"/portal/uilogin-forwardLogin.shtml";') != -1 or body.find(
                '访问受限') != -1 or response.status == 404:
            logger.info('未登录，登陆中，请稍后···')
            login_ok = False
            if ctrl.BEING_LOG is False:
                login_ok = login()
            while ctrl.BEING_LOG:
                time.sleep(1)
            if login_ok:
                return self._retry(request, 'unlogin', spider)
        return response

    def process_exception(self, request, exception, spider):
        logger.error(exception)
        login_ok = False
        if ctrl.BEING_LOG is False:

            login_ok = login()