This commit is contained in:
2026-08-25 19:03:41 +08:00
commit e1416994bc
34 changed files with 3266 additions and 0 deletions
+340
View File
@@ -0,0 +1,340 @@
"""
投递自动化模块
在后台线程运行,支持用户随时开始/结束。
向云端服务端上报投递记录与统计。
"""
import random
import re
import time
import threading
from dataclasses import dataclass
from loguru import logger
from .manager import BrowserManager
from ..ai import AIClient
from ..server_api import ServerAPI
@dataclass
class JobCard:
job_id: str
title: str
company: str
salary: str
location: str
hr_active: str
can_chat: bool
can_deliver: bool
jd_text: str = ""
class Applicant:
"""投递执行器(可启停)"""
BASE_URL = "https://www.zhipin.com"
def __init__(
self,
manager: BrowserManager,
server_api: ServerAPI,
ai_client: AIClient | None,
):
self.manager = manager
self.server = server_api
self.ai = ai_client
# 运行控制
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
self.is_running = False
# 运行时状态(供 Web 面板查询)
self.session_id: int | None = None
self.primary_resume_content: str = ""
self.stats = {"applied": 0, "skipped": 0, "current": ""}
# ------------------------------------------------------------------
# 启停控制
# ------------------------------------------------------------------
def start(self, keywords: list[str], city_code: str,
salary_min: int | None, salary_max: int | None,
greeting: str, min_score: int, delay_range: tuple[int, int]) -> None:
"""在新线程中启动投递任务"""
if self.is_running:
logger.warning("已有投递任务在运行")
return
# 创建服务端会话
session = self.server.start_session(
keywords=",".join(keywords),
city_code=city_code,
salary_min=salary_min,
salary_max=salary_max,
)
self.session_id = session["id"]
# 获取主简历内容
primary = self.server.get_primary_resume()
self.primary_resume_content = primary["content"] if primary else ""
self._stop_event.clear()
self.is_running = True
self.stats = {"applied": 0, "skipped": 0, "current": "初始化..."}
self._thread = threading.Thread(
target=self._run,
args=(keywords, city_code, salary_min, salary_max,
greeting, min_score, delay_range),
daemon=True,
)
self._thread.start()
logger.success(f"投递任务已启动,会话 ID:{self.session_id}")
def stop(self) -> None:
"""请求停止投递任务"""
logger.info("收到停止投递请求...")
self._stop_event.set()
def _finish(self) -> None:
"""任务结束后的收尾工作"""
self.is_running = False
if self.session_id:
try:
self.server.stop_session(self.session_id)
except Exception as e:
logger.warning(f"结束会话失败:{e}")
self.stats["current"] = "已停止"
logger.info(
f"投递任务结束。本次投递 {self.stats['applied']} 个,"
f"跳过 {self.stats['skipped']} 个"
)
def _should_stop(self) -> bool:
return self._stop_event.is_set()
# ------------------------------------------------------------------
# 主循环
# ------------------------------------------------------------------
def _run(self, keywords, city_code, salary_min, salary_max,
greeting, min_score, delay_range):
try:
for keyword in keywords:
if self._should_stop():
break
self.stats["current"] = f"搜索关键词:{keyword}"
self._process_keyword(
keyword, city_code, salary_min, salary_max,
greeting, min_score, delay_range,
)
if not self._should_stop():
self._sleep(5, 9)
except Exception as e:
logger.error(f"投递任务异常:{e}")
finally:
self._finish()
def _process_keyword(self, keyword, city_code, salary_min, salary_max,
greeting, min_score, delay_range, max_per_keyword: int = 15):
page = 1
applied_in_kw = 0
while applied_in_kw < max_per_keyword and not self._should_stop():
cards = self._fetch_job_list(keyword, city_code, salary_min, salary_max, page)
if not cards:
break
for card in cards:
if self._should_stop():
return
if applied_in_kw >= max_per_keyword:
break
applied_in_kw += self._handle_card(card, greeting, min_score, delay_range)
page += 1
def _handle_card(self, card: JobCard, greeting: str, min_score: int,
delay_range: tuple[int, int]) -> int:
"""处理单个职位卡片,返回是否投递成功(1/0)"""
# 1. 去重(服务端查询)
try:
if self.server.check_applied(card.job_id):
return 0
except Exception:
pass
self.stats["current"] = f"评估:{card.company} - {card.title}"
# 2. AI 匹配度评分
ai_score, ai_reason = -1, ""
if self.ai and self.ai.available() and self.primary_resume_content:
jd = card.jd_text or self._fetch_jd_detail(card)
result = self.ai.score_job(jd, self.primary_resume_content)
ai_score, ai_reason = result.score, result.reason
if not result.is_worth_applying(min_score):
self._report_skip(card, f"AI 评分低({ai_score})", ai_score, ai_reason)
return 0
# 3. 执行投递
success, apply_type = self._apply(card, greeting)
if success:
self._report_apply(card, apply_type, ai_score, ai_reason)
self._sleep(*delay_range)
return 1
return 0
# ------------------------------------------------------------------
# 服务端上报
# ------------------------------------------------------------------
def _report_apply(self, card: JobCard, apply_type: str, ai_score: int, ai_reason: str):
try:
self.server.report_application(self.session_id, {
"job_id": card.job_id,
"job_title": card.title,
"company": card.company,
"salary": card.salary,
"location": card.location,
"apply_type": apply_type,
"ai_score": ai_score,
"ai_reason": ai_reason[:255] if ai_reason else None,
})
self.server.update_session_stats(self.session_id, applied_delta=1, skipped_delta=0)
self.stats["applied"] += 1
logger.success(f"[{self.stats['applied']}] 投递成功({apply_type}):{card.company} - {card.title}")
except Exception as e:
logger.warning(f"上报投递记录失败:{e}")
def _report_skip(self, card: JobCard, reason: str, ai_score: int = -1, ai_reason: str = ""):
try:
self.server.report_application(self.session_id, {
"job_id": card.job_id,
"job_title": card.title,
"company": card.company,
"salary": card.salary,
"apply_type": "skipped",
"ai_score": ai_score,
"ai_reason": ai_reason[:255] if ai_reason else None,
"skip_reason": reason,
})
self.server.update_session_stats(self.session_id, applied_delta=0, skipped_delta=1)
self.stats["skipped"] += 1
logger.info(f"跳过({reason}):{card.company} - {card.title}")
except Exception as e:
logger.debug(f"上报跳过记录失败:{e}")
# ------------------------------------------------------------------
# 页面操作
# ------------------------------------------------------------------
def _fetch_job_list(self, keyword, city_code, salary_min, salary_max, page) -> list[JobCard]:
page_obj = self.manager.ensure_connected()
url = f"{self.BASE_URL}/web/geek/job?query={keyword}&city={city_code}&page={page}"
if salary_min or salary_max:
url += f"&salary={(salary_min or 0) * 1000},{(salary_max or 0) * 1000}"
self.stats["current"] = f"加载职位列表 第{page}页"
try:
page_obj.goto(url, wait_until="domcontentloaded", timeout=30000)
page_obj.wait_for_timeout(2500)
page_obj.wait_for_selector(".job-card-wrapper", timeout=10000)
except Exception:
logger.warning(f"第 {page} 页无职位列表")
return []
cards = []
for el in page_obj.query_selector_all(".job-card-wrapper"):
try:
card = self._parse_card(el)
if card:
cards.append(card)
except Exception:
continue
logger.info(f"第 {page} 页解析到 {len(cards)} 个职位")
return cards
def _parse_card(self, el) -> JobCard | None:
def text(sel: str) -> str:
node = el.query_selector(sel)
return node.inner_text().strip() if node else ""
link = el.query_selector("a.job-card-left")
href = link.get_attribute("href") if link else ""
m = re.search(r"/job_detail/(\w+)\.html", href or "")
if not m:
return None
return JobCard(
job_id=m.group(1),
title=text(".job-name"),
company=text(".company-name"),
salary=text(".salary"),
location=text(".job-area"),
hr_active=text(".info-public"),
can_chat=bool(el.query_selector(".start-chat-btn:not([disabled])")),
can_deliver=bool(el.query_selector(".btn-deliver")),
)
def _fetch_jd_detail(self, card: JobCard) -> str:
page_obj = self.manager.page
try:
page_obj.goto(f"{self.BASE_URL}/job_detail/{card.job_id}.html",
wait_until="domcontentloaded", timeout=20000)
page_obj.wait_for_timeout(1500)
el = page_obj.query_selector(".job-detail-section")
jd = el.inner_text().strip() if el else f"{card.title} {card.company}"
page_obj.go_back()
page_obj.wait_for_timeout(1500)
return jd
except Exception:
return f"{card.title} {card.company}"
def _apply(self, card: JobCard, greeting: str) -> tuple[bool, str]:
if card.can_chat and self._do_chat(card, greeting):
return True, "chat"
if card.can_deliver and self._do_deliver(card):
return True, "deliver"
return False, ""
def _do_chat(self, card: JobCard, greeting: str) -> bool:
page_obj = self.manager.page
try:
btn = page_obj.query_selector(f"[data-job-id='{card.job_id}'] .start-chat-btn")
if not btn:
return False
btn.click()
page_obj.wait_for_timeout(2000)
if greeting:
inp = page_obj.query_selector(".chat-input textarea")
if inp:
inp.fill(greeting)
page_obj.wait_for_timeout(500)
send = page_obj.query_selector(".chat-input .btn-send")
if send:
send.click()
page_obj.wait_for_timeout(1500)
close = page_obj.query_selector(".dialog-container .close-btn")
if close:
close.click()
return True
except Exception as e:
logger.debug(f"立即沟通失败:{e}")
return False
def _do_deliver(self, card: JobCard) -> bool:
page_obj = self.manager.page
try:
btn = page_obj.query_selector(f"[data-job-id='{card.job_id}'] .btn-deliver")
if not btn:
return False
btn.click()
page_obj.wait_for_timeout(2000)
confirm = page_obj.query_selector(".dialog-confirm .btn-sure")
if confirm:
confirm.click()
page_obj.wait_for_timeout(1500)
return True
except Exception as e:
logger.debug(f"投递简历失败:{e}")
return False
def _sleep(self, min_s: float, max_s: float) -> None:
# 分段睡眠以便及时响应停止信号
total = random.uniform(min_s, max_s)
elapsed = 0.0
while elapsed < total and not self._should_stop():
time.sleep(0.5)
elapsed += 0.5