Commit 29539169 by yexing

更新代理IP

parent 8ca54d41
......@@ -9,22 +9,28 @@ from curl_cffi.requests import AsyncSession, Response
IS_DEBUG = int(os.environ.get("IS_DEBUG", False))
LOG_PATH = f"./log/{Path(__file__).stem}.log"
if not IS_DEBUG:
if not IS_DEBUG:
logger.remove()
logger.add(LOG_PATH, level="INFO", rotation="1 day", retention="1 months")
class Subsidiary:
def __init__(self):
self._tasks = []
self.s = AsyncSession()
self.r: redis.Redis = None
async def connect(self):
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():
logger.info("redis 连接成功")
async def bind(self):
while True:
try:
......@@ -34,7 +40,7 @@ class Subsidiary:
await asyncio.sleep(10)
except Exception as e:
logger.warning(f"绑定失败: {e}")
async def cache_ips(self):
while True:
try:
......@@ -54,10 +60,9 @@ class Subsidiary:
await self.r.aclose()
await self.s.close()
logger.info("资源已回收")
async def run_during_night(self):
"""在晚上运行任务
"""
"""在晚上运行任务"""
while True:
now = datetime.now().time()
if time(22, 0) > now and now > time(7, 0):
......@@ -67,12 +72,12 @@ class Subsidiary:
logger.info("不在指定时间范围内, 停止运行")
break
await asyncio.sleep(60)
async def run(self, monitor: bool = False):
try:
await self.connect()
self._tasks = [
asyncio.create_task(self.bind()),
asyncio.create_task(self.bind()),
asyncio.create_task(self.cache_ips()),
]
if monitor:
......@@ -86,6 +91,7 @@ class Subsidiary:
finally:
await self.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
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