Commit 29539169 by yexing

更新代理IP

parent 8ca54d41
...@@ -9,22 +9,28 @@ from curl_cffi.requests import AsyncSession, Response ...@@ -9,22 +9,28 @@ from curl_cffi.requests import AsyncSession, Response
IS_DEBUG = int(os.environ.get("IS_DEBUG", False)) IS_DEBUG = int(os.environ.get("IS_DEBUG", False))
LOG_PATH = f"./log/{Path(__file__).stem}.log" LOG_PATH = f"./log/{Path(__file__).stem}.log"
if not IS_DEBUG: if not IS_DEBUG:
logger.remove() logger.remove()
logger.add(LOG_PATH, level="INFO", rotation="1 day", retention="1 months") logger.add(LOG_PATH, level="INFO", rotation="1 day", retention="1 months")
class Subsidiary: class Subsidiary:
def __init__(self): def __init__(self):
self._tasks = [] self._tasks = []
self.s = AsyncSession() self.s = AsyncSession()
self.r: redis.Redis = None self.r: redis.Redis = None
async def connect(self): async def connect(self):
if self.r is None: if self.r is None:
self.r = redis.Redis(host="localhost", port=6380, db=0) self.r = redis.Redis(
host="localhost",
port=int(os.getenv("REDIS_HOST", 6380)),
password=os.getenv("REDIS_PASSWORD"),
db=0,
)
if await self.r.ping(): if await self.r.ping():
logger.info("redis 连接成功") logger.info("redis 连接成功")
async def bind(self): async def bind(self):
while True: while True:
try: try:
...@@ -34,7 +40,7 @@ class Subsidiary: ...@@ -34,7 +40,7 @@ class Subsidiary:
await asyncio.sleep(10) await asyncio.sleep(10)
except Exception as e: except Exception as e:
logger.warning(f"绑定失败: {e}") logger.warning(f"绑定失败: {e}")
async def cache_ips(self): async def cache_ips(self):
while True: while True:
try: try:
...@@ -54,10 +60,9 @@ class Subsidiary: ...@@ -54,10 +60,9 @@ class Subsidiary:
await self.r.aclose() await self.r.aclose()
await self.s.close() await self.s.close()
logger.info("资源已回收") logger.info("资源已回收")
async def run_during_night(self): async def run_during_night(self):
"""在晚上运行任务 """在晚上运行任务"""
"""
while True: while True:
now = datetime.now().time() now = datetime.now().time()
if time(22, 0) > now and now > time(7, 0): if time(22, 0) > now and now > time(7, 0):
...@@ -67,12 +72,12 @@ class Subsidiary: ...@@ -67,12 +72,12 @@ class Subsidiary:
logger.info("不在指定时间范围内, 停止运行") logger.info("不在指定时间范围内, 停止运行")
break break
await asyncio.sleep(60) await asyncio.sleep(60)
async def run(self, monitor: bool = False): async def run(self, monitor: bool = False):
try: try:
await self.connect() await self.connect()
self._tasks = [ self._tasks = [
asyncio.create_task(self.bind()), asyncio.create_task(self.bind()),
asyncio.create_task(self.cache_ips()), asyncio.create_task(self.cache_ips()),
] ]
if monitor: if monitor:
...@@ -86,6 +91,7 @@ class Subsidiary: ...@@ -86,6 +91,7 @@ class Subsidiary:
finally: finally:
await self.close() await self.close()
if __name__ == "__main__": if __name__ == "__main__":
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
if IS_DEBUG: if IS_DEBUG:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment