成功获取登录后的 ck

This commit is contained in:
yml2213
2026-06-21 23:02:09 +08:00
parent 4dca691890
commit ea8a56ca66
4 changed files with 397 additions and 81 deletions
+183 -78
View File
@@ -2,10 +2,12 @@
import imaplib
import email
import html
import re
import time
from datetime import datetime, timedelta
from email.header import decode_header
from email.utils import parsedate_to_datetime
from typing import Optional
from loguru import logger
@@ -20,12 +22,18 @@ class EmailVerifier:
username: str,
password: str,
timeout: float = 12,
mailbox: str = "INBOX",
lookback_minutes: int = 10,
max_messages: int = 20,
):
self.imap_server = imap_server
self.imap_port = imap_port
self.username = username
self.password = password
self.timeout = timeout
self.mailbox = mailbox
self.lookback_minutes = lookback_minutes
self.max_messages = max_messages
self._connection: Optional[imaplib.IMAP4_SSL] = None
def connect(self) -> None:
@@ -52,13 +60,21 @@ class EmailVerifier:
pass
self._connection = None
def get_verification_code(self, max_wait: int = 60, interval: int = 2) -> str:
def get_verification_code(
self,
max_wait: int = 60,
interval: int = 2,
after_timestamp: Optional[float] = None,
allow_old_seconds: int = 15,
) -> str:
"""
获取斗鱼验证码
Args:
max_wait: 最大等待时间(秒)
interval: 轮询间隔(秒)
after_timestamp: 发起发送验证码请求的时间戳,用于过滤旧邮件
allow_old_seconds: 邮件服务器时间允许向前偏移的秒数
Returns:
6位验证码
@@ -68,9 +84,12 @@ class EmailVerifier:
self.connect()
try:
start_time = time.time()
while time.time() - start_time < max_wait:
code = self._fetch_latest_code()
deadline = time.monotonic() + max_wait
while time.monotonic() < deadline:
code = self._fetch_latest_code(
after_timestamp=after_timestamp,
allow_old_seconds=allow_old_seconds,
)
if code:
logger.success(f"获取到验证码: {code}")
return code
@@ -82,56 +101,151 @@ class EmailVerifier:
finally:
self.disconnect()
def _fetch_latest_code(self) -> Optional[str]:
def _fetch_latest_code(
self,
after_timestamp: Optional[float] = None,
allow_old_seconds: int = 15,
) -> Optional[str]:
"""从IMAP获取最新验证码"""
try:
self._connection.select('INBOX')
if not self._connection:
raise RuntimeError("IMAP未连接")
# 搜索最近5分钟的邮件
since = (datetime.now() - timedelta(minutes=5)).strftime("%d-%b-%Y")
status, messages = self._connection.search(
None,
f'(OR (FROM "douyu") (FROM "斗鱼") (SUBJECT "验证码")) SINCE {since}'
)
status, _ = self._connection.select(self.mailbox, readonly=True)
if status != 'OK':
logger.warning(f"选择邮箱目录失败: {self.mailbox}")
return None
since = self._build_since_date(after_timestamp, allow_old_seconds)
# IMAP命令只能稳定发送ASCII条件,中文主题/发件人改到本地解析过滤。
status, messages = self._connection.search(None, 'SINCE', since)
if status != 'OK' or not messages[0]:
return None
# 获取最新的一封邮件
latest_id = messages[0].split()[-1]
status, msg_data = self._connection.fetch(latest_id, '(RFC822)')
message_ids = messages[0].split()
recent_ids = list(reversed(message_ids[-self.max_messages:]))
logger.debug(f"扫描最近 {len(recent_ids)} 封邮件,SINCE {since}")
if status != 'OK':
return None
for message_id in recent_ids:
msg = self._fetch_message(message_id)
if not msg:
continue
msg = email.message_from_bytes(msg_data[0][1])
if not self._is_recent_enough(msg, after_timestamp, allow_old_seconds):
continue
# 检查是否是斗鱼的邮件
subject = self._decode_subject(msg.get('Subject', ''))
if not self._is_douyu_email(subject, msg.get('From', '')):
return None
subject = self._decode_subject(msg.get('Subject', ''))
from_addr = msg.get('From', '')
body = self._get_email_body(msg)
# 提取验证码
body = self._get_email_body(msg)
code = self._extract_verification_code(body)
if not self._is_douyu_email(subject, from_addr, body):
continue
if code:
logger.info(f"从邮件中提取到验证码: {code}")
code = self._extract_verification_code(body)
if code:
logger.info(f"从邮件中提取到验证码,主题: {subject}")
return code
return code
return None
except imaplib.IMAP4.abort as e:
logger.warning(f"IMAP连接中断,准备下轮重连: {e}")
self.disconnect()
self.connect()
return None
except Exception as e:
logger.error(f"获取邮件失败: {e}")
return None
def _is_douyu_email(self, subject: str, from_addr: str) -> bool:
"""判断是否是斗鱼的邮件"""
douyu_keywords = ['斗鱼', 'douyu', '验证码', '安全验证']
subject_lower = subject.lower()
from_lower = from_addr.lower()
def _build_since_date(
self,
after_timestamp: Optional[float],
allow_old_seconds: int,
) -> str:
"""构造IMAP SINCE日期,月份固定用英文缩写。"""
if after_timestamp:
since_dt = datetime.fromtimestamp(
max(0, after_timestamp - allow_old_seconds)
)
else:
since_dt = datetime.now() - timedelta(minutes=self.lookback_minutes)
return any(keyword in subject_lower or keyword in from_lower
for keyword in douyu_keywords)
return self._format_imap_date(since_dt)
def _format_imap_date(self, value: datetime) -> str:
"""格式化IMAP日期,避免系统locale影响月份名称。"""
months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
]
return f"{value.day:02d}-{months[value.month - 1]}-{value.year}"
def _fetch_message(self, message_id: bytes) -> Optional[email.message.Message]:
"""获取邮件完整内容,使用PEEK避免标记已读。"""
status, msg_data = self._connection.fetch(message_id, '(BODY.PEEK[])')
if status != 'OK':
return None
raw_message = self._join_fetch_payload(msg_data)
if not raw_message:
return None
return email.message_from_bytes(raw_message)
def _join_fetch_payload(self, msg_data) -> bytes:
"""合并IMAP fetch返回中的邮件字节内容。"""
chunks = []
for item in msg_data:
if isinstance(item, tuple) and len(item) >= 2 and isinstance(item[1], bytes):
chunks.append(item[1])
elif isinstance(item, bytes) and item.startswith(b'From '):
chunks.append(item)
return b''.join(chunks)
def _is_recent_enough(
self,
msg: email.message.Message,
after_timestamp: Optional[float],
allow_old_seconds: int,
) -> bool:
"""检查邮件时间是否晚于本次发送验证码请求。"""
if not after_timestamp:
return True
message_time = self._parse_message_time(msg)
if not message_time:
logger.debug("邮件缺少Date头,保守纳入候选")
return True
if message_time.timestamp() < after_timestamp - allow_old_seconds:
subject = self._decode_subject(msg.get('Subject', ''))
logger.debug(f"跳过旧邮件: {subject}")
return False
return True
def _parse_message_time(self, msg: email.message.Message) -> Optional[datetime]:
"""解析邮件Date头。"""
date_header = msg.get('Date')
if not date_header:
return None
try:
return parsedate_to_datetime(date_header)
except (TypeError, ValueError):
return None
def _is_douyu_email(self, subject: str, from_addr: str, body: str = "") -> bool:
"""判断是否是斗鱼的邮件"""
douyu_keywords = ['斗鱼', 'douyu', 'douyutv']
verify_keywords = ['验证码', '安全验证', '登录验证', '动态码', '校验码']
combined = f"{subject}\n{from_addr}\n{body[:500]}".lower()
return (
any(keyword in combined for keyword in douyu_keywords)
or any(keyword in subject for keyword in verify_keywords)
)
def _decode_subject(self, subject: str) -> str:
"""解码邮件主题"""
@@ -149,72 +263,63 @@ class EmailVerifier:
def _get_email_body(self, msg: email.message.Message) -> str:
"""获取邮件正文"""
body = ""
parts = []
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
if content_type == 'text/plain' or content_type == 'text/html':
try:
payload = part.get_payload(decode=True)
charset = part.get_content_charset() or 'utf-8'
body += payload.decode(charset, errors='ignore')
except:
pass
else:
try:
payload = msg.get_payload(decode=True)
charset = msg.get_content_charset() or 'utf-8'
body = payload.decode(charset, errors='ignore')
except:
pass
if part.get_content_disposition() == 'attachment':
continue
return body
if content_type == 'text/plain' or content_type == 'text/html':
decoded = self._decode_part_payload(part)
if decoded:
parts.append(decoded)
else:
decoded = self._decode_part_payload(msg)
if decoded:
parts.append(decoded)
return "\n".join(parts)
def _decode_part_payload(self, part: email.message.Message) -> str:
"""解码邮件片段内容。"""
try:
payload = part.get_payload(decode=True)
if payload is None:
raw_payload = part.get_payload()
return raw_payload if isinstance(raw_payload, str) else ""
charset = part.get_content_charset() or 'utf-8'
return payload.decode(charset, errors='ignore')
except Exception:
return ""
def _extract_verification_code(self, text: str) -> Optional[str]:
"""从文本中提取验证码"""
# 清理HTML标签
# 清理HTML标签和实体,方便匹配中文邮件模板。
text = html.unescape(text)
text = re.sub(r'<[^>]+>', ' ', text)
text = re.sub(r'\s+', ' ', text)
# 查找6位数字验证码
# 注意:中文冒号和英文冒号需要分别处理
patterns = [
r'验证码[:]\s*(\d{6})', # 验证码:123456 或 验证码:123456
r'验证码\s+(\d{6})', # 验证码 123456
r'(\d{6})\s*是您的验证码', # 123456是您的验证码
r'您的验证码[是为]\s*[:]\s*(\d{6})', # 您的验证码是:123456
r'您的验证码[是为]\s*(\d{6})', # 您的验证码是123456
r'verification code[:]\s*(\d{6})',
r'code[:]\s*(\d{6})',
r'(\d{6})', # 最后尝试匹配任何6位数字
r'(?:验证码|校验码|动态码|安全码)\s*(?:是|为|:|)?\s*(\d{6})',
r'(?:您的|你的|本次)?(?:验证码|校验码|动态码|安全码)\s*(?:是|为)?\s*(\d{6})',
r'(\d{6})\s*(?:是|为)?(?:您的|你的|本次)?(?:验证码|校验码|动态码|安全码)',
r'(?:verification\s+code|security\s+code|code)\s*(?:is|:|)?\s*(\d{6})',
r'\b(\d{6})\b', # 候选邮件已经过滤为斗鱼验证码邮件,最后再兜底匹配6位数字。
]
for pattern in patterns:
match = re.search(pattern, text, re.IGNORECASE)
if match:
code = match.group(1)
# 验证码有效性检查(简单过滤明显不是验证码的数字)
if not self._is_likely_code(code):
continue
return code
return None
def _is_likely_code(self, code: str) -> bool:
"""判断是否是有效的验证码"""
# 过滤掉一些明显不是验证码的数字
invalid_patterns = [
r'^(\d)\1{5}$', # 全部相同:111111, 222222
r'^123456$', # 顺序数字
r'^654321$', # 逆序数字
]
for pattern in invalid_patterns:
if re.match(pattern, code):
return False
return True
def get_email_config_for_account(email_address: str) -> dict:
"""根据邮箱地址自动配置IMAP服务器"""
+7 -3
View File
@@ -222,11 +222,12 @@ class DouyuLogin:
# 4️⃣ 发送邮箱验证
logger.info("步骤4: 发送邮箱验证...")
email_sent_at = time.time()
self._send_email_verify(remote_code)
# 5️⃣ IMAP获取验证码
logger.info("步骤5: 获取邮箱验证码...")
verify_code = self._get_email_code()
verify_code = self._get_email_code(after_timestamp=email_sent_at)
# 6️⃣ 提交验证码
logger.info("步骤6: 提交验证码...")
@@ -455,7 +456,7 @@ class DouyuLogin:
logger.info("验证邮件已发送")
def _get_email_code(self) -> str:
def _get_email_code(self, after_timestamp: Optional[float] = None) -> str:
"""获取邮箱验证码"""
verifier = EmailVerifier(
imap_server=self.account.email_imap_server,
@@ -464,7 +465,10 @@ class DouyuLogin:
password=self.account.email_password,
)
return verifier.get_verification_code(max_wait=60)
return verifier.get_verification_code(
max_wait=60,
after_timestamp=after_timestamp,
)
def _submit_verify_code(self, remote_code: str, verify_code: str) -> str:
"""