first commi
This commit is contained in:
127
checker/legacy/utils.py
Normal file
127
checker/legacy/utils.py
Normal file
@@ -0,0 +1,127 @@
|
||||
import random
|
||||
import string
|
||||
|
||||
|
||||
import random
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from datetime import timezone
|
||||
|
||||
|
||||
|
||||
# fmt 参数是 keyword-only 参数,且有默认值
|
||||
# 默认值 '%Y-%m-%dT%H:%M:%SZ' 来自上一段字节码末尾的常量池
|
||||
def format_ts(dt, *, fmt='%Y-%m-%dT%H:%M:%SZ'):
|
||||
# 1. 将时间对象 dt 转换为 UTC 时区
|
||||
# 2. 将转换后的时间对象格式化为字符串
|
||||
return dt.astimezone(timezone.utc).strftime(fmt)
|
||||
|
||||
def telegram_send(bot_token, chat_id, text, timeout):
|
||||
# 1. 检查必要参数
|
||||
if not bot_token or not chat_id:
|
||||
return False
|
||||
|
||||
try:
|
||||
# 2. 构建 API URL
|
||||
url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
|
||||
|
||||
# 3. 构建请求载荷
|
||||
payload = {
|
||||
'chat_id': chat_id,
|
||||
'text': text,
|
||||
'parse_mode': 'HTML',
|
||||
'disable_web_page_preview': True
|
||||
}
|
||||
|
||||
# 4. 发送 POST 请求
|
||||
requests.post(url, json=payload, timeout=timeout)
|
||||
|
||||
# 5. 成功返回
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
# 6. 发生错误返回 False
|
||||
return False
|
||||
|
||||
|
||||
def gstr(t, p, s):
|
||||
# 1. 检查前缀 p 是否存在于文本 t 中
|
||||
if p not in t:
|
||||
return ''
|
||||
|
||||
# 2. 计算截取的起始位置 a
|
||||
# t.find(p) 找到前缀的起始索引,加上 len(p) 跳过前缀本身
|
||||
a = t.find(p) + len(p)
|
||||
|
||||
# 3. 查找后缀 s 的位置 b
|
||||
# 从位置 a 开始向后查找,确保后缀在前缀之后
|
||||
b = t.find(s, a)
|
||||
|
||||
# 4. 如果找到后缀(find 返回不是 -1),则切片返回内容
|
||||
if b != -1:
|
||||
return t[a:b]
|
||||
|
||||
# 5. 如果没找到后缀,返回空字符串
|
||||
return ''
|
||||
# 根据字节码底部的常量还原的全局变量 UA
|
||||
UA = (
|
||||
# Desktop Chrome (macOS)
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
||||
# Desktop Chrome (Windows)
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
||||
# Desktop Edge (Windows)
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
|
||||
# Desktop Chrome (Linux)
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
||||
# Desktop Firefox (macOS)
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:131.0) Gecko/20100101 Firefox/131.0',
|
||||
# Desktop Firefox (Windows)
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0'
|
||||
)
|
||||
|
||||
def sleep_random(min_seconds, max_seconds):
|
||||
# 1. 生成 min_seconds 和 max_seconds 之间的随机浮点数
|
||||
delay = random.uniform(min_seconds, max_seconds)
|
||||
|
||||
# 2. 执行睡眠
|
||||
time.sleep(delay)
|
||||
|
||||
# 3. 返回延迟时间 (用于后续可能的日志记录或显示)
|
||||
return delay
|
||||
|
||||
|
||||
# 生成密码
|
||||
def generate_password(min_length, max_length):
|
||||
|
||||
|
||||
# 1. 确定密码长度
|
||||
length = random.randint(min_length, max_length)
|
||||
|
||||
# 2. 定义字符集
|
||||
lower = string.ascii_lowercase
|
||||
upper = string.ascii_uppercase
|
||||
digits = string.digits
|
||||
symbols = '!@#$%^&*()-_=+[]{}|;:,.<>?/'
|
||||
|
||||
# 3. 初始化密码列表,强制包含每种类型至少一个字符
|
||||
password = [
|
||||
random.choice(lower),
|
||||
random.choice(upper),
|
||||
random.choice(digits),
|
||||
random.choice(symbols)
|
||||
]
|
||||
|
||||
# 4. 组合所有可用字符
|
||||
all_chars = lower + upper + digits + symbols
|
||||
|
||||
# 5. 填充剩余长度 (总长度 - 已生成的4个字符)
|
||||
# 使用 random.choices 允许重复选取
|
||||
password += random.choices(all_chars, k=length - len(password))
|
||||
|
||||
# 6. 打乱字符顺序
|
||||
random.shuffle(password)
|
||||
|
||||
# 7. 拼接成字符串并返回
|
||||
return ''.join(password)
|
||||
Reference in New Issue
Block a user