# 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
# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/spider-middleware.html


logger = Logger(__name__)


class PatentMiddleware(RetryMiddleware):

    def process_request(self, request, spider):

# 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

# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/spider-middleware.html


logger = Logger(__name__)


class PatentMiddleware(RetryMiddleware):

    def process_request(self, request, spider):

        if USE_PROXY and ctrl.PROXIES is not None: