# coding:utf-8
import os
import random
from yaml import load

config_path = os.path.join(os.path.dirname(__file__), 'spider.yaml')


with open(config_path, encoding='utf-8') as f:
    cont = f.read()

cf = load(cont)


def get_db_args():
    return cf.get('db')


def get_redis_args():
    return cf.get('redis')


def get_timeout():
    return cf.get('time_out')


def get_crawl_interal():
    interal = random.randint(cf.get('min_crawl_interal'), cf.get('max_crawl_interal'))
    return interal


def get_excp_interal():
    return cf.get('excp_interal')


def get_max_repost_page():
    return cf.get('max_repost_page')


def get_max_search_page():
    return cf.get('max_search_page')


def get_max_home_page():
    return cf.get('max_home_page')


def get_max_comment_page():
    return cf.get('max_comment_page')


def get_max_retries():
    return cf.get('max_retries')


def get_broker_or_backend(types):
    """
    :param types: 类型，1表示中间人，2表示消息后端
    :return: 
    """
    redis_info = cf.get('redis')
    host = redis_info.get('host')
    port = redis_info.get('port')
    password = redis_info.get('password')

    if types == 1:
        db = redis_info.get('broker')
    else:
        db = redis_info.get('backend')
    url = 'redis://:{}@{}:{}/{}'.format(password, host, port, db)

    return url


def get_code_username():
    return cf.get('yundama_username')


def get_code_password():
    return cf.get('yundama_passwd')


def get_running_mode():
    return cf.get('mode')


def get_share_host_count():
    return cf.get('share_host_count')


def get_cookie_expire_time():
    return cf.get('cookie_expire_time')


def get_email_args():
    return cf.get('email')
@@ -887,18 +887,19 @@
 _broker_
-or
+and
 _backend
@@ -903,85 +903,58 @@
 end(
-types
 ):%0A    
-%22%22%22%0A    :param types: %E7%B1%BB%E5%9E%8B%EF%BC%8C1%E8%A1%A8%E7%A4%BA%E4%B8%AD%E9%97%B4%E4%BA%BA%EF%BC%8C2%E8%A1%A8%E7%A4%BA%E6%B6%88%E6%81%AF%E5%90%8E%E7%AB%AF%0A    :return: %0A    %22%22%22%0A   
+redis_info = cf.get('redis')%0A    password =
  red
@@ -964,35 +964,42 @@
 info
- = cf
 .get('
-redis')%0A    host
+password')%0A    sentinel_args
  = r
@@ -1017,23 +1017,29 @@
 et('
-host
+sentinel', '
 ')%0A    
-port
+db
  = r
@@ -1057,54 +1057,113 @@
 et('
-port')%0A    password = redis_info.ge
+broker', 5)%0A    if sentinel_args:%0A        broker_url = %22;%22.join('sentinel://:%7B%7D@%7B%7D:%7B%7D/%7B%7D'.forma
 t(
-'
 password
 ')%0A%0A
@@ -1162,29 +1162,145 @@
 word
-')%0A%0A    if types == 1
+, sentinel%5B'host'%5D, sentinel%5B'port'%5D, db) for%0A                              sentinel in sentinel_args)%0A        return broker_url%0A    else
 :%0A  
@@ -1297,34 +1297,36 @@
   else:%0A        
-db
+host
  = redis_info.ge
@@ -1332,26 +1332,52 @@
 et('
-broker
+host
 ')%0A    
+ 
-else:
+   port = redis_info.get('port')
 %0A   
@@ -1377,24 +1377,32 @@
 t')%0A        
+backend_
 db = redis_i
@@ -1418,22 +1418,36 @@
 backend'
+, 6
 )%0A    
+    broker_
 url = 'r
@@ -1504,23 +1504,203 @@
 db)%0A
-%0A
+        backend_url = 'redis://:%7B%7D@%7B%7D:%7B%7D/%7B%7D'.format(password, host, port, backend_db)%0A  
     
+  
 return 
-url
+broker_url, backend_url%0A%0A%0Adef get_redis_master():%0A    return cf.get('redis').get('master', '')
 %0A%0A%0Ad
@@ -2066,8 +2066,10 @@
 'email')
+%0A%0A

add redis sentinel support