setting.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import platform
  2. from os.path import dirname, abspath, join
  3. from environs import Env
  4. from loguru import logger
  5. from proxypool.utils.parse import parse_redis_connection_string
  6. env = Env()
  7. env.read_env()
  8. # definition of flags
  9. IS_WINDOWS = platform.system().lower() == 'windows'
  10. # definition of dirs
  11. ROOT_DIR = dirname(dirname(abspath(__file__)))
  12. LOG_DIR = join(ROOT_DIR, env.str('LOG_DIR', 'logs'))
  13. # definition of environments
  14. DEV_MODE, TEST_MODE, PROD_MODE = 'dev', 'test', 'prod'
  15. APP_ENV = env.str('APP_ENV', DEV_MODE).lower()
  16. APP_DEBUG = env.bool('APP_DEBUG', True if APP_ENV == DEV_MODE else False)
  17. APP_DEV = IS_DEV = APP_ENV == DEV_MODE
  18. APP_PROD = IS_PROD = APP_ENV == PROD_MODE
  19. APP_TEST = IS_TEST = APP_ENV == TEST_MODE
  20. # redis host
  21. REDIS_HOST = env.str('REDIS_HOST', '127.0.0.1')
  22. # redis port
  23. REDIS_PORT = env.int('REDIS_PORT', 6379)
  24. # redis password, if no password, set it to None
  25. REDIS_PASSWORD = env.str('REDIS_PASSWORD', None)
  26. # redis db, if no choice, set it to 0
  27. REDIS_DB = env.int('REDIS_DB', 0)
  28. # redis connection string, like redis://[password]@host:port or rediss://[password]@host:port/0
  29. REDIS_CONNECTION_STRING = env.str('REDIS_CONNECTION_STRING', None)
  30. if REDIS_CONNECTION_STRING:
  31. REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_DB = parse_redis_connection_string(REDIS_CONNECTION_STRING)
  32. # redis hash table key name
  33. REDIS_KEY = env.str('REDIS_KEY', 'proxies:universal')
  34. # definition of proxy scores
  35. PROXY_SCORE_MAX = 100
  36. PROXY_SCORE_MIN = 0
  37. PROXY_SCORE_INIT = 10
  38. # definition of proxy number
  39. PROXY_NUMBER_MAX = 50000
  40. PROXY_NUMBER_MIN = 0
  41. # definition of tester cycle, it will test every CYCLE_TESTER second
  42. CYCLE_TESTER = env.int('CYCLE_TESTER', 20)
  43. # definition of getter cycle, it will get proxy every CYCLE_GETTER second
  44. CYCLE_GETTER = env.int('CYCLE_GETTER', 100)
  45. GET_TIMEOUT = env.int('GET_TIMEOUT', 10)
  46. # definition of tester
  47. TEST_URL = env.str('TEST_URL', 'http://www.baidu.com')
  48. TEST_TIMEOUT = env.int('TEST_TIMEOUT', 10)
  49. TEST_BATCH = env.int('TEST_BATCH', 20)
  50. # only save anonymous proxy
  51. TEST_ANONYMOUS = True
  52. # TEST_HEADERS = env.json('TEST_HEADERS', {
  53. # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
  54. # })
  55. TEST_VALID_STATUS = env.list('TEST_VALID_STATUS', [200, 206, 302])
  56. # definition of api
  57. API_HOST = env.str('API_HOST', '0.0.0.0')
  58. API_PORT = env.int('API_PORT', 5555)
  59. API_THREADED = env.bool('API_THREADED', True)
  60. # flags of enable
  61. ENABLE_TESTER = env.bool('ENABLE_TESTER', True)
  62. ENABLE_GETTER = env.bool('ENABLE_GETTER', True)
  63. ENABLE_SERVER = env.bool('ENABLE_SERVER', True)
  64. # logger.add(env.str('LOG_RUNTIME_FILE', join(LOG_DIR, 'runtime.log')), level='DEBUG', rotation='1 week', retention='20 days')
  65. # logger.add(env.str('LOG_ERROR_FILE', join(LOG_DIR, 'error.log')), level='ERROR', rotation='1 week')