# -*- coding: utf-8 -*-
# ==============================================================================
# --- BƯỚC 0: CÀI ĐẶT CHROME & DRIVER CHO GOOGLE COLAB ---
# ==============================================================================
# Các lệnh  sẽ cài đặt trình duyệt Chrome và driver cần thiết  môi trường Colab.
# BẮT BUỘC  chạy bước  .
!apt-get update
!apt-get install -y chromium-browser chromium-chromedriver
!pip install selenium
import time
import random
import threading
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# ==============================================================================
# --- BƯỚC 1: KHU VỰC CẤU HÌNH ---
# ==============================================================================
# URL chương 1 của bộ truyện bạn  chạy
START_CHAPTER_URL = "https://mottruyen.com.vn/ga-nham-hao-mon-chu-mau-kho-duong/50600/chuong/1"
# Số "  ảo" chạy song song ( luồng)
NUM_WORKERS = 5
# Thời gian "" mỗi chương (giây)
MIN_READ_TIME_S = 45
MAX_READ_TIME_S = 65
# Thời gian nghỉ (giây)  khi  xong một lượt truyện  khi reset
MIN_RESET_WAIT_S = 300  # 5 phút
MAX_RESET_WAIT_S = 600  # 10 phút
# ==============================================================================
# --- BƯỚC 2: LÕI CỦA CÔNG CỤ (KHÔNG CẦN CHỈNH SỬA) ---
# ==============================================================================
def setup_colab_driver():
    """Thiết lập một trình duyệt Chrome để chạy  môi trường Google Colab."""
    chrome_options = Options()
    # Các tùy chọn  là BẮT BUỘC để chạy Selenium  Colab
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    # Các tùy chọn tối ưu hóa khác
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--log-level=3")
    chrome_options.add_experimental_option(
        "prefs", {"profile.managed_default_content_settings.images": 2}
    )
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
    chrome_options.add_argument(f'user-agent={user_agent}')
    try:
        # Không cần dùng webdriver-manager nữa, vì   cài đặt thủ công
        driver = webdriver.Chrome(options=chrome_options)
        return driver
    except Exception as e:
        print(f"Lỗi nghiêm trọng khi khởi tạo driver: {e}")
        return None
def selenium_worker(worker_id, start_url):
    """
[Truyện được đăng tải duy nhất tại MonkeyD.net.vn - https://www.monkeydtruyen.com/ga-nham-hao-mon-chu-mau-kho-duong/chuong-158.html.]
    Hàm công việc cho mỗi luồng.
    Mỗi luồng sẽ  từ đầu đến cuối truyện,  đó reset và lặp .
    """
    base_url, _ = start_url.rsplit('/', 1)
    # Vòng lặp vô tận:  xong sẽ tự reset và bắt đầu 
    while True:
        driver = setup_colab_driver()
        if not driver:
            print(f"[Worker #{worker_id}] Không thể khởi tạo trình duyệt. Sẽ thử   5 phút.")
            time.sleep(300)
            continue
        print(f"[Worker #{worker_id}] Đã khởi tạo profile mới. Bắt đầu  từ chương 1.")
        current_chapter_num = 1
        # Vòng lặp  từng chương
        while True:
            try:
                current_url = f"{base_url}/{current_chapter_num}"
                driver.get(current_url)
                # Nếu URL trả về  chứa "chuong", nghĩa là  hết truyện
                if "chuong" not in driver.current_url:
                    print(f"[Worker #{worker_id}] Đã  đến chương cuối cùng. Hoàn thành một lượt.")
                    break
                read_duration = random.randint(MIN_READ_TIME_S, MAX_READ_TIME_S)
                print(f"[Worker #{worker_id}] Đang  chương {current_chapter_num} trong {read_duration} giây...")
Lãnh Hàn Hạ Vũ
                time.sleep(read_duration)
                current_chapter_num += 1
            except Exception as e:
                print(f"[Worker #{worker_id}] Gặp  ở chương {current_chapter_num}: {e}. Có thể  hết truyện hoặc  mạng.")
                break # Thoát vòng lặp  chương khi  
        # Đóng trình duyệt để xóa profile cũ
        driver.quit()
        # Nghỉ ngơi  khi tạo profile mới và bắt đầu 
        reset_wait = random.randint(MIN_RESET_WAIT_S, MAX_RESET_WAIT_S)
        print(f"[Worker #{worker_id}] Đã reset profile. Sẽ bắt đầu   {reset_wait // 60} phút.")
        time.sleep(reset_wait)
if __name__ == "__main__":
    print(">>> Bắt đầu công cụ tăng view (Phiên bản Google Colab) <<<")
    print(f"Số luồng chạy song song: {NUM_WORKERS}")
    print("Nhấn 'Dừng' trong Colab để kết thúc chương trình.")
    threads = []
    for i in range(NUM_WORKERS):
        thread = threading.Thread(
            target=selenium_worker,
            args=(i + 1, START_CHAPTER_URL),
            daemon=True
        )
        threads.append(thread)
        thread.start()
        time.sleep(5) # Dãn cách thời gian khởi động các trình duyệt
    # Giữ cho chương trình chính chạy mãi mãi để các luồng con hoạt động
    try:
        while True:
            time.sleep(3600) # Ngủ 1 tiếng, chỉ để giữ chương trình sống
    except KeyboardInterrupt:
        print("\nĐã nhận tín hiệu dừng. Chương trình kết thúc.")