0.2.2
This commit is contained in:
@@ -309,10 +309,10 @@ def main2():
|
||||
browser_id = '5ba9eb974c7c45e2bb086585c75f70e8'
|
||||
# 关闭浏览器
|
||||
# res = bit.bit_browser_close(browser_id)
|
||||
# res = bit.bit_browser_get()
|
||||
# print(res)
|
||||
res = bit.bit_browser_get()
|
||||
print(res)
|
||||
|
||||
# if __name__ == '__main__':
|
||||
# main2()
|
||||
if __name__ == '__main__':
|
||||
main2()
|
||||
|
||||
bit_browser = BitBrowser()
|
||||
# bit_browser = BitBrowser()
|
||||
@@ -8,7 +8,6 @@ from mail_ import mail_
|
||||
from bit_browser import bit_browser
|
||||
from api import api
|
||||
from proxys import proxy_list
|
||||
import asyncio
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
@@ -210,6 +209,9 @@ class Auto:
|
||||
jc += 1
|
||||
except Exception as e:
|
||||
logger.error(f"点击Continue按钮异常: {e}")
|
||||
jc += 1
|
||||
if jc > 3:
|
||||
return False
|
||||
self.tab.wait(1)
|
||||
|
||||
# 随机取城市
|
||||
@@ -567,7 +569,7 @@ def create_fingerprint_browser(proxy: str) -> tuple[str, str] | None:
|
||||
if not browser_id:
|
||||
return None
|
||||
logger.info(f"创建指纹浏览器成功: {browser_id}")
|
||||
time.sleep(0.1)
|
||||
time.sleep(1)
|
||||
http = bit_browser.bit_browser_open(browser_id)
|
||||
if not http:
|
||||
return None
|
||||
@@ -589,7 +591,7 @@ def close_and_delete_browser(browser_id: str) -> None:
|
||||
bit_browser.bit_browser_close(browser_id)
|
||||
except Exception as e:
|
||||
logger.warning(f"关闭浏览器失败或已关闭: {browser_id} - {e}")
|
||||
time.sleep(0.1)
|
||||
time.sleep(1)
|
||||
try:
|
||||
bit_browser.bit_browser_delete(browser_id)
|
||||
except Exception as e:
|
||||
@@ -645,6 +647,8 @@ def proxy_loop(proxy: str, stop_event: threading.Event) -> None:
|
||||
while not stop_event.is_set():
|
||||
try:
|
||||
if is_forbidden_time():
|
||||
if stop_event.wait(timeout=60):
|
||||
break
|
||||
cleanup_all_browsers()
|
||||
secs = seconds_until(20, 0)
|
||||
if stop_event.wait(timeout=secs):
|
||||
@@ -725,28 +729,49 @@ def cleanup_all_browsers() -> None:
|
||||
logger.warning(f"清理所有指纹浏览器失败: {e}")
|
||||
|
||||
|
||||
def delete_excess_browsers(limit: int) -> None:
|
||||
"""
|
||||
删除超出上限的指纹浏览器,从列表末尾开始删除
|
||||
|
||||
参数:
|
||||
limit: 允许的最大浏览器数量
|
||||
"""
|
||||
try:
|
||||
res = bit_browser.bit_browser_get(0, 100)
|
||||
data = res.get("data", {}) if isinstance(res, dict) else {}
|
||||
lst = data.get("list", [])
|
||||
ids = [i.get("id") for i in lst if i.get("id")]
|
||||
count = len(ids)
|
||||
if count <= limit:
|
||||
return
|
||||
excess = count - limit
|
||||
to_delete = ids[-excess:]
|
||||
for bid in reversed(to_delete):
|
||||
close_and_delete_browser(bid)
|
||||
logger.info(f"已删除超出数量 {excess},当前限制为 {limit}")
|
||||
except Exception as e:
|
||||
logger.warning(f"删除超额浏览器失败: {e}")
|
||||
|
||||
|
||||
def monitor_browsers_and_restart(limit: int, stop_event: threading.Event, restart_event: threading.Event) -> None:
|
||||
"""
|
||||
每 30 秒检测指纹浏览器数量,超过 `limit` 则触发重启事件并清理所有浏览器
|
||||
每 3 秒检测指纹浏览器数量,超过 `limit` 则从末尾删除超出部分
|
||||
|
||||
参数:
|
||||
limit: 允许的最大浏览器数量(通常为代理数量)
|
||||
restart_event: 触发重启的事件
|
||||
restart_event: 触发重启的事件(当前策略不使用)
|
||||
"""
|
||||
while not stop_event.is_set():
|
||||
time.sleep(30)
|
||||
time.sleep(3)
|
||||
count = count_fingerprint_browsers()
|
||||
if count > limit:
|
||||
logger.warning(f"指纹浏览器数量 {count} 超过限制 {limit},执行重启")
|
||||
restart_event.set()
|
||||
stop_event.set()
|
||||
cleanup_all_browsers()
|
||||
break
|
||||
logger.warning(f"指纹浏览器数量 {count} 超过限制 {limit},开始删除超出部分")
|
||||
delete_excess_browsers(limit)
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
多线程并发管理:按代理数量并发创建指纹浏览器并执行任务;每 30 秒监控数量,超限则重启。
|
||||
多线程并发管理:按代理数量并发创建指纹浏览器并执行任务;每 3 秒监控数量,超限则从末尾删除多余浏览器。
|
||||
"""
|
||||
proxies = list(proxy_list)
|
||||
while True:
|
||||
@@ -754,6 +779,8 @@ def main():
|
||||
restart_event = threading.Event()
|
||||
|
||||
if is_forbidden_time():
|
||||
if stop_event.wait(timeout=60):
|
||||
continue
|
||||
cleanup_all_browsers()
|
||||
secs = seconds_until(20, 0)
|
||||
logger.info(f"处于禁跑时段,休眠至20:00,剩余 {int(secs)} 秒")
|
||||
@@ -780,12 +807,13 @@ def main():
|
||||
pass
|
||||
break
|
||||
if is_forbidden_time():
|
||||
logger.info("进入禁跑时段,停止当前批次并清理指纹浏览器")
|
||||
logger.info("进入禁跑时段,停止当前批次,等待1分钟后清理指纹浏览器")
|
||||
stop_event.set()
|
||||
try:
|
||||
executor.shutdown(wait=False)
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(60)
|
||||
cleanup_all_browsers()
|
||||
break
|
||||
for f, proxy in list(futures_map.items()):
|
||||
|
||||
Reference in New Issue
Block a user