目 录CONTENT

文章目录

TTG自动签到

前言

几个月前,我使用nas-tool对pt站进行管理。原本nas-tool内部已经实现了站点的自动签到功能,但是这两天登录发现签到失败了,排查nas-tool无果,遂想自己实现一个签到脚本。

原本以为只需要发送一个get请求就能完成,结果发现TTG的签到用的是JS,直接get肯定不行了,只能用浏览器仿真。之前没弄过这方面,好在现在ai足够智能,还是整出来了,就当学习了orz。

步骤

首先下载chrome以及安装:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb

接着pip安装seleniumwebdriver-manager

pip install selenium
pip install webdriver-manager

然后定时执行以下脚本,注意要输入的签到网页以及网站的cookie:

import os
import time
import shutil
import signal
import subprocess
import glob
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

def kill_chrome_processes():
    try:
        if os.name == 'nt':  # Windows
            subprocess.call("taskkill /F /IM chrome.exe", shell=True)
        elif os.name == 'posix':  # macOS/Linux
            subprocess.call("pkill -f chrome", shell=True)
    except Exception as e:
        print(f"Error killing Chrome processes: {e}")

def cleanup_old_user_data_dirs():
    """清理旧的 Chrome 用户数据目录"""
    for dir_path in glob.glob(os.path.join(os.getcwd(), "chrome_user_data_*")):
        try:
            shutil.rmtree(dir_path)
            print(f"Deleted old user data directory: {dir_path}")
        except Exception as e:
            print(f"Error deleting old user data directory: {dir_path}, {e}")
cleanup_old_user_data_dirs()
kill_chrome_processes()

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# 生成唯一的目录路径
user_data_dir = os.path.join(os.getcwd(), f"chrome_user_data_{int(time.time())}")

# 检查目录是否存在,如果存在则删除
if os.path.exists(user_data_dir):
    shutil.rmtree(user_data_dir)

# 使用 os.makedirs 创建目录
os.makedirs(user_data_dir, exist_ok=True)

options.add_argument(f"--user-data-dir={user_data_dir}")
# 指定 Chrome 浏览器可执行文件路径
options.binary_location = "/usr/bin/google-chrome"  # Linux 默认路径
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)

# 你的签到页面 URL
sign_in_url = ""

#Cookie 字符串
cookie_string = ""


def parse_cookie_string(cookie_string):
    cookies = []
    for item in cookie_string.split(';'):
        item = item.strip()
        if '=' in item:
            name, value = item.split('=', 1)
            cookies.append({'name': name, 'value': value})
    return cookies

try:
    # 1. 解析 Cookie 字符串
    cookies = parse_cookie_string(cookie_string)

    # 2. 添加 Cookie 到浏览器
    driver.get(sign_in_url) # 先访问一个页面,确保 Cookie 可以正确设置
    for cookie in cookies:
        driver.add_cookie(cookie)

    # 3. 重新访问签到页面
    driver.get(sign_in_url)

    # 4. 查找签到按钮并点击
    sign_in_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "signed"))
    )
    sign_in_button.click()

    # 5. 等待签到完成
    time.sleep(10)
    print("签到成功!")

except Exception as e:
    print(f"签到失败: {e}")

finally:
    # 6. 关闭浏览器
    driver.quit()
    try:
        shutil.rmtree(user_data_dir)
        print(f"Deleted current user data directory: {user_data_dir}")
    except Exception as e:
        print(f"Error deleting current user data directory: {user_data_dir}, {e}")


1

评论区