123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import json
- import time
- import sys
- from random import choice
- # from libs.db_redis import DbRedis
- from log.print_log import PrintLog
- sys.path.append("..")
- from rds_model.db_redis import DbRedis
- class Proxy:
- redis = DbRedis.connect()
- proxy_info = ''
- @staticmethod
- def get():
- while True:
- proxy = Proxy.random_proxy()
- if proxy is not None:
- return proxy
- PrintLog.print('未拿到有效的代理')
- time.sleep(0.1)
- @staticmethod
- def random_proxy():
- key = 'IpProxyHash'
- proxy_dict = Proxy.redis.hgetall(key)
- if (proxy_dict is None) or (len(proxy_dict) == 0):
- return
- proxy_list = list(proxy_dict)
- now = int(time.time())
- while True:
- proxy = choice(proxy_list)#Proxy.choice_proxy()
- if proxy is None:
- return
- proxy_info = proxy_dict.get(proxy)
- if proxy_info is None:
- continue
- Proxy.proxy_info = proxy_info
- proxy_info = json.loads(proxy_info)
- expire_at = int(proxy_info.get('expired_at'))
- # 删除过期的代理
- if expire_at <= now:
- Proxy.redis.hdel(key, proxy)
- proxy_list.remove(proxy)
- continue
- return proxy
- @staticmethod
- def del_proxy(proxy):
- proxy_info = Proxy.redis.hget('IpProxyHash', proxy)
- if proxy_info is None:
- return
- proxy_info = json.loads(proxy_info)
- min_expired_at = proxy_info.get('min_expired_at')
- if min_expired_at is None:
- min_expired_at = 0
- min_expired_at = int(min_expired_at)
- now = int(time.time())
- if min_expired_at >= now:
- return
- # 删除失效的代理
- print('删除失效代理:' + proxy)
- @staticmethod
- def dailiyun_get():
- while True:
- proxy = Proxy.dailiyun_random_proxy()
- if proxy is not None:
- return proxy
- PrintLog.print('未拿到有效的代理')
- time.sleep(0.1)
- @staticmethod
- def dailiyun_random_proxy():
- key = 'IpProxyHashDaiLiYun'
- proxy_dict = Proxy.redis.hgetall(key)
- if (proxy_dict is None) or (len(proxy_dict) == 0):
- return
- proxy_list = list(proxy_dict)
- now = int(time.time())
- while True:
- proxy = choice(proxy_list)#Proxy.choice_proxy()
- if proxy is None:
- return
- proxy_info = proxy_dict.get(proxy)
- if proxy_info is None:
- continue
- Proxy.proxy_info = proxy_info
- proxy_info = json.loads(proxy_info)
- expire_at = int(proxy_info.get('expired_at'))
- # 删除过期的代理
- if expire_at <= now:
- Proxy.redis.hdel(key, proxy)
- proxy_list.remove(proxy)
- continue
- return proxy
- @staticmethod
- def dailiyun_del_proxy(proxy):
- proxy_info = Proxy.redis.hget('IpProxyHashDaiLiYun', proxy)
- if proxy_info is None:
- return
- proxy_info = json.loads(proxy_info)
- min_expired_at = proxy_info.get('min_expired_at')
- if min_expired_at is None:
- min_expired_at = 0
- min_expired_at = int(min_expired_at)
- now = int(time.time())
- if min_expired_at >= now:
- return
- # 删除失效的代理
- print('删除失效代理:' + proxy)
- @staticmethod
- def rola_get():
- while True:
- proxy = Proxy.rola_random_proxy()
- if proxy is not None:
- return proxy
- PrintLog.print('未拿到有效的代理')
- time.sleep(0.1)
- @staticmethod
- def rola_random_proxy():
- key = 'IpProxyHashRola'
- proxy_dict = Proxy.redis.hgetall(key)
- if (proxy_dict is None) or (len(proxy_dict) == 0):
- return
- proxy_list = list(proxy_dict)
- now = int(time.time())
- while True:
- proxy = choice(proxy_list)#Proxy.choice_proxy()
- if proxy is None:
- return
- proxy_info = proxy_dict.get(proxy)
- if proxy_info is None:
- continue
- Proxy.proxy_info = proxy_info
- proxy_info = json.loads(proxy_info)
- expire_at = int(proxy_info.get('expired_at'))
- # 删除过期的代理
- if expire_at <= now:
- Proxy.redis.hdel(key, proxy)
- proxy_list.remove(proxy)
- continue
- return proxy
- @staticmethod
- def rola_del_proxy(proxy):
- proxy_info = Proxy.redis.hget('IpProxyHashRola', proxy)
- if proxy_info is None:
- return
- proxy_info = json.loads(proxy_info)
- min_expired_at = proxy_info.get('min_expired_at')
- if min_expired_at is None:
- min_expired_at = 0
- min_expired_at = int(min_expired_at)
- now = int(time.time())
- if min_expired_at >= now:
- return
- # 删除失效的代理
- print('删除失效代理:' + proxy)
|