示例#1
0
 def max(self, proxy: Proxy) -> int:
     """
     set proxy to max score
     :param proxy: proxy
     :return: new score
     """
     logger.info(f'{proxy.string()} is valid, set to {PROXY_SCORE_MAX}')
     if IS_REDIS_VERSION_2:
         return self.db.zadd(REDIS_KEY, PROXY_SCORE_MAX, proxy.string())
     return self.db.zadd(REDIS_KEY, {proxy.string(): PROXY_SCORE_MAX})
示例#2
0
 def add(self, proxy: Proxy, score=PROXY_SCORE_INIT) -> int:
     """
     add proxy and set it to init score
     :param proxy: proxy, ip:port, like 8.8.8.8:88
     :param score: int score
     :return: result
     """
     if not is_valid_proxy(f'{proxy.host}:{proxy.port}'):#判断获得的ip是否合法
         logger.info(f'invalid proxy {proxy}, throw it')
         return
     if not self.exists(proxy):
         if IS_REDIS_VERSION_2:
             return self.db.zadd(REDIS_KEY, score, proxy.string())
         return self.db.zadd(REDIS_KEY, {proxy.string(): score})
示例#3
0
 def decrease(self, proxy: Proxy) -> int:
     """
     decrease score of proxy, if small than PROXY_SCORE_MIN, delete it
     :param proxy: proxy
     :return: new score
     """
     if IS_REDIS_VERSION_2:
         self.db.zincrby(REDIS_KEY, proxy.string(), -1)
     else:
         self.db.zincrby(REDIS_KEY, -1, proxy.string())#如果ip验证失败则分数减一
     score = self.db.zscore(REDIS_KEY, proxy.string())
     logger.info(f'{proxy.string()} score decrease 1, current {score}')
     if score <= PROXY_SCORE_MIN:
         logger.info(f'{proxy.string()} current score {score}, remove')
         self.db.zrem(REDIS_KEY, proxy.string())#如果分数小于0则移除ip
示例#4
0
 def exists(self, proxy: Proxy) -> bool:
     """
     if proxy exists
     :param proxy: proxy
     :return: if exists, bool
     """
     return not self.db.zscore(REDIS_KEY, proxy.string()) is None
示例#5
0
 def decrease(self, proxy: Proxy) -> int:
     """
     decrease score of proxy, if small than PROXY_SCORE_MIN, delete it
     :param proxy: proxy
     :return: new score
     """
     score = self.db.zscore(REDIS_KEY, proxy.string())
     # current score is larger than PROXY_SCORE_MIN
     if score and score > PROXY_SCORE_MIN:
         logger.info(f'{proxy.string()} current score {score}, decrease 1')
         if IS_REDIS_VERSION_2:
             return self.db.zincrby(REDIS_KEY, proxy.string(), -1)
         return self.db.zincrby(REDIS_KEY, -1, proxy.string())
     # otherwise delete proxy
     else:
         logger.info(f'{proxy.string()} current score {score}, remove')
         return self.db.zrem(REDIS_KEY, proxy.string())