setting.py 2.9 KB

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