from playwright.sync_api import sync_playwright
import json
import time
import requests
MANUAL_SESSION_COOKIES = [
{
"name": "WHMCSUser",
"value": "xxx",
"domain": "www.dmit.io",
"path": "/"
},
{
"name": "cf_app_sid",
"value": "xxx",
"domain": "www.dmit.io",
"path": "/"
},
{
"name": "cf_clearance",
"value": "xxx",
"domain": ".dmit.io",
"path": "/"
}
]
def get_page_with_cf_bypass(url, existing_cookies):
with sync_playwright() as p:
browser = p.chromium.launch(
headless=True,
args=['--disable-blink-features=AutomationControlled'],
)
context = browser.new_context(
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
viewport={'width': 1280, 'height': 800},
)
if existing_cookies:
context.add_cookies(existing_cookies)
page = context.new_page()
try:
page.goto(url, wait_until='networkidle', timeout=30000)
retries = 0
while retries < 6:
title = page.title()
if 'Just a moment' not in title and 'Checking' not in title:
break
print("仍在挑战页面,等待中...")
time.sleep(3)
retries += 1
cookies = context.cookies()
json_text = page.locator("pre").inner_text()
return json_text, cookies
finally:
browser.close()
def send_telegram_message(
bot_token: str,
chat_id: str,
text: str,
timeout: int,
) -> None:
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
response = requests.post(
url,
json={
"chat_id": chat_id,
"parse_mode": "HTML",
"text": text,
},
timeout=timeout,
)
response.raise_for_status()
payload = response.json()
if not payload.get("ok"):
raise RuntimeError(f"Telegram 返回异常: {payload}")
if __name__ == "__main__":
url = "https://www.dmit.io/clientarea.php?action=productdetails&id=ID&json=1&pure=1&page=standard&subaction=whmcsdetail"
json_text, cookies = get_page_with_cf_bypass(url, MANUAL_SESSION_COOKIES)
data = json.loads(json_text)
try:
send_telegram_message(
bot_token='xxx',
chat_id='xxx',
text=f"<b>DMIT Transfer</b>\n"
f"<code>bwusage: {r['bwusage'] / 1024:.2f} GB\n"
f"bwusage_in: {r['bwusage_in'] / 1024:.2f} GB\n"
f"bwusage_out: {r['bwusage_out'] / 1024:.2f} GB\n"
f"lastupdate: {r['lastupdate']} UTC</code>",
timeout=20,
)
except Exception as exc:
print("执行失败: %s", exc)
自用的,改改url的id,cookie,tg bot的token和发送的群组id就能用了