import time
from playwright.sync_api import sync_playwright
USERNAME = "janichan"
PASSWORD = "ab270894"
DIRECT_LOGIN_URL = "https://forumketqua.net/login/"
SOURCE_URL = "https://nguoidudoan.blogspot.com/p/dan-ac-biet-64-so-hang-ngay.html"
# Đã cập nhật lại đường dẫn đúng chuẩn bài viết
TARGET_POST_URL = "https://forumketqua.net/threads/dan-dac-biet-xsmb-64s-thang-9-2026.101725/"
def get_numbers_from_textarea(page):
"""Lấy nội dung dàn số từ ô textarea#copyText."""
try:
print("[Blogspot] Đang truy cập trang lấy dàn số...")
page.goto(SOURCE_URL, wait_until="domcontentloaded", timeout=60000)
page.wait_for_selector("#copyText", timeout=15000)
numbers_text = page.input_value("#copyText")
if numbers_text and numbers_text.strip():
print(f"[Blogspot] Lấy dữ liệu thành công:\n---> {numbers_text}")
return numbers_text.strip()
else:
print("[Blogspot] Khung #copyText đang trống.")
except Exception as e:
print(f"[Blogspot] Lỗi khi lấy dàn số: {e}")
return None
def run_automation_job():
"""Thực hiện quy trình đăng nhập và gửi bài."""
print("Đang khởi động trình duyệt...")
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
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"
)
page = context.new_page()
try:
# 1. Lấy dàn số
numbers_text = get_numbers_from_textarea(page)
if not numbers_text:
print("Hủy lượt chạy này do không lấy được dữ liệu số.")
return
# 2. Đăng nhập
print("[Forum] Đang truy cập trang đăng nhập...")
page.goto(DIRECT_LOGIN_URL, wait_until="domcontentloaded", timeout=60000)
print("[Forum] Đang điền thông tin tài khoản...")
page.fill("input[name='login'], #LoginControl", USERNAME)
page.fill("input[name='password'], #ctrl_password", PASSWORD)
page.click("input[type='submit'][value='Đăng nhập']")
page.wait_for_load_state("domcontentloaded")
print("[Forum] Đăng nhập thành công!")
# 3. Truy cập bài viết mục tiêu
print("[Forum] Đang chuyển tới bài viết chốt số...")
page.goto(TARGET_POST_URL, wait_until="domcontentloaded", timeout=60000)
# 4. Điền nội dung vào iframe Redactor theo đúng file HTML gửi kèm
print("[Forum] Đang nhập nội dung dàn số...")
# Chờ iframe soạn thảo load lên màn hình
page.wait_for_selector("iframe.redactor_MessageEditor", timeout=30000)
# Trỏ trực tiếp vào body bên trong iframe để gõ dàn số
editor_frame = page.frame_locator("iframe.redactor_MessageEditor")
editor_frame.locator("body").fill(numbers_text)
# 5. Click nút "Gửi trả lời"
print("[Forum] Đang bấm gửi trả lời...")
page.click("input[type='submit'][value='Gửi trả lời']")
page.wait_for_timeout(5000)
print("==> Chốt số thành công!")
except Exception as e:
print(f"Lỗi trong quá trình thực thi: {e}")
finally:
browser.close()
def main():
while True:
run_automation_job()
print("\nĐang chờ 24 giờ cho lượt chạy tiếp theo...\n")
time.sleep(86400)
if __name__ == "__main__":
main()