# Path: config/base_settings.py
# BASE_PATH = os.path.split(os.path.split(__file__)[0])[0]
# OUTPUT_PATH = os.path.join(BASE_PATH, 'output')
# OUTPUT_GROUP_PATH = os.path.join(OUTPUT_PATH, TimeUtil.getFormatTime('%Y%m%d_%H%M%S'))
# DATABASE_NAME = os.path.join(OUTPUT_GROUP_PATH, 'Patent.db')
# EXCEL_NAME = os.path.join(OUTPUT_GROUP_PATH, '专利.xlsx')
# CHARTS_NAME = os.path.join(OUTPUT_GROUP_PATH, 'charts.html')
# LOG_FILENAME = os.path.join(OUTPUT_GROUP_PATH, "PatentCrawler.log")
# CAPTCHA_MODEL_NAME = os.path.join(BASE_PATH, 'res', 'captcha', 'sipo3.job')
# AD_PATH = os.path.join(BASE_PATH, 'res', 'advertisement', 'ad.html')
# USE_PROXY = False
# PROXY_URL = 'http://127.0.0.1:5010/get'
# TIMEOUT = 10
# DOWNLOAD_DELAY = 1
# OUTPUT_ITEMS = ['data', 'log', 'chart']
#         USE_PROXY = use_proxy
#                 PROXY_URL = proxy_url
#             TIMEOUT = timeout
#             DOWNLOAD_DELAY = delay
#             OUTPUT_ITEMS = output_items
# def check_proxy(cfg):
# def check_request(cfg: configparser.ConfigParser):
# def check_output(cfg):
# 
# Path: controller/url_config.py



import json
import controller as ctrl
import requests
from logbook import Logger
from requests.exceptions import RequestException, ReadTimeout
from config import base_settings as bs
from controller.url_config import url_pre_execute, url_index


def notify_ip_address():
    """
    通知专利网我们的ip地址，
    这个网站比较特别，每当有陌生ip地址时，都需要通过这个方法向网站发送一次请求先。
    :return:
    """
    resp = requests.post(url_pre_execute.get('url'), proxies=ctrl.PROXIES, timeout=bs.TIMEOUT, cookies=ctrl.COOKIES)
    # logger.debug(resp.text)
    ip_address = json.loads(resp.text)
    if ctrl.PROXIES is not None:
        if ip_address.get('IP') == ctrl.PROXIES.get('http').split(':')[0]:
            return resp.text
        else:
            raise Exception('ip error')
    else:
        return resp.text


def get_proxy():
    """
    获取代理ip，并更新控制器PROXIES
    :return: 可用的ip代理
    """
    if bs.USE_PROXY is False:
        return None

    try:
        logger.info('获取代理···')

# Path: config/base_settings.py
# BASE_PATH = os.path.split(os.path.split(__file__)[0])[0]
# OUTPUT_PATH = os.path.join(BASE_PATH, 'output')
# OUTPUT_GROUP_PATH = os.path.join(OUTPUT_PATH, TimeUtil.getFormatTime('%Y%m%d_%H%M%S'))
# DATABASE_NAME = os.path.join(OUTPUT_GROUP_PATH, 'Patent.db')
# EXCEL_NAME = os.path.join(OUTPUT_GROUP_PATH, '专利.xlsx')
# CHARTS_NAME = os.path.join(OUTPUT_GROUP_PATH, 'charts.html')
# LOG_FILENAME = os.path.join(OUTPUT_GROUP_PATH, "PatentCrawler.log")
# CAPTCHA_MODEL_NAME = os.path.join(BASE_PATH, 'res', 'captcha', 'sipo3.job')
# AD_PATH = os.path.join(BASE_PATH, 'res', 'advertisement', 'ad.html')
# USE_PROXY = False
# PROXY_URL = 'http://127.0.0.1:5010/get'
# TIMEOUT = 10
# DOWNLOAD_DELAY = 1
# OUTPUT_ITEMS = ['data', 'log', 'chart']
#         USE_PROXY = use_proxy
#                 PROXY_URL = proxy_url
#             TIMEOUT = timeout
#             DOWNLOAD_DELAY = delay
#             OUTPUT_ITEMS = output_items
# def check_proxy(cfg):
# def check_request(cfg: configparser.ConfigParser):
# def check_output(cfg):
# 
# Path: controller/url_config.py



# Path: service/proxy.py
import json
import controller as ctrl
import requests
from logbook import Logger
from requests.exceptions import RequestException, ReadTimeout
from config import base_settings as bs
from controller.url_config import url_pre_execute, url_index



def notify_ip_address():
    """
    通知专利网我们的ip地址，
    这个网站比较特别，每当有陌生ip地址时，都需要通过这个方法向网站发送一次请求先。
    :return:
    """
    resp = requests.post(url_pre_execute.get('url'), proxies=ctrl.PROXIES, timeout=bs.TIMEOUT, cookies=ctrl.COOKIES)
    # logger.debug(resp.text)
    ip_address = json.loads(resp.text)
    if ctrl.PROXIES is not None:
        if ip_address.get('IP') == ctrl.PROXIES.get('http').split(':')[0]:
            return resp.text
        else:
            raise Exception('ip error')
    else:
        return resp.text


def get_proxy():
    """
    获取代理ip，并更新控制器PROXIES
    :return: 可用的ip代理
    """
    if bs.USE_PROXY is False:
        return None

    try:
        logger.info('获取代理···')

        resp = requests.get(bs.PROXY_URL, timeout=bs.TIMEOUT)