auto start chat between recommend and expect job looply; add a set so that prevent chat with a boss multi time

This commit is contained in:
bossgeekgo
2024-02-10 09:28:52 +08:00
parent 6e8f6cbf81
commit 64db06a5d2
+45 -28
View File
@@ -21,6 +21,8 @@ const recommendJobPageUrl = `https://www.zhipin.com/web/geek/job-recommend`
const expectCompanySet = new Set(targetCompanyList) const expectCompanySet = new Set(targetCompanyList)
let browser, page let browser, page
const blockBossNotNewChat = new Set()
async function mainLoop () { async function mainLoop () {
try { try {
browser = await puppeteer.launch({ browser = await puppeteer.launch({
@@ -50,30 +52,34 @@ async function mainLoop () {
const recommendJobLink = (await page.$('[ka=header-job-recommend]')) const recommendJobLink = (await page.$('[ka=header-job-recommend]'))
await recommendJobLink.click() await recommendJobLink.click()
const INIT_START_EXCEPT_JOB_INDEX = 0
let currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
afterPageLoad: while (true) { afterPageLoad: while (true) {
await sleepWithRandomDelay(3000) const currentActiveJobIndex = await page.evaluate(`
[...document.querySelectorAll('.job-recommend-search .recommend-job-btn')].findIndex(it => it.classList.contains('active'))
const expectJobList = (await page.evaluate(`
document.querySelector('.job-recommend-search')?.__vue__?.expectList
`) `)
) if (currentActiveJobIndex === currentExceptJobIndex) {
// first navigation and can immediately start chat (recommend job)
const expectJobTabHandlers = await page.$$('.job-recommend-main .recommend-search-expect .recommend-job-btn') } else {
expectJobTabHandlers.shift() // not first navigation and should choose a job (except job)
const expectJobList = await page.evaluate(`document.querySelector('.job-recommend-search')?.__vue__?.expectList`)
// click first expect job
await expectJobTabHandlers[0].click() const expectJobTabHandlers = await page.$$('.job-recommend-main .recommend-search-expect .recommend-job-btn')
await page.waitForResponse(
response => { // click first expect job
if ( await expectJobTabHandlers[currentExceptJobIndex].click()
response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/pc/recommend/job/list.json') await page.waitForResponse(
) { response => {
return true if (
response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/pc/recommend/job/list.json')
) {
return true
}
return false
} }
return false );
} await sleepWithRandomDelay(2000)
); }
await sleepWithRandomDelay(2000)
try { try {
const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => { const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => {
@@ -85,7 +91,10 @@ async function mainLoop () {
document.querySelector('.job-recommend-main')?.__vue__?.jobList document.querySelector('.job-recommend-main')?.__vue__?.jobList
` `
) )
let targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name))) let targetJobIndex = jobListData.findIndex(
it => !blockBossNotNewChat.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name))
)
let hasReachLastPage = false let hasReachLastPage = false
while (targetJobIndex < 0 && !hasReachLastPage) { while (targetJobIndex < 0 && !hasReachLastPage) {
@@ -117,7 +126,7 @@ async function mainLoop () {
document.querySelector('.job-recommend-main')?.__vue__?.jobList document.querySelector('.job-recommend-main')?.__vue__?.jobList
` `
) )
targetJobIndex = targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name))) targetJobIndex = jobListData.findIndex(it => !blockBossNotNewChat.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name)))
} }
if (targetJobIndex < 0 && hasReachLastPage) { if (targetJobIndex < 0 && hasReachLastPage) {
@@ -204,14 +213,22 @@ async function mainLoop () {
} }
} }
} else { } else {
blockBossNotNewChat.add(jobData.jobInfo.encryptUserId)
} }
} }
} catch (err) { } catch (err) {
if (err instanceof Error && err.message === 'CANNOT_FIND_EXCEPT_JOB') { if (err instanceof Error && err.message === 'CANNOT_FIND_EXCEPT_JOB') {
await Promise.all([ if (
page.reload(), currentExceptJobIndex + 1 > expectJobTabHandlers.length
page.waitForNavigation() ) {
]) await Promise.all([
page.reload(),
page.waitForNavigation()
])
currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
} else {
currentExceptJobIndex += 1
}
continue afterPageLoad; continue afterPageLoad;
} else { } else {
throw err throw err
@@ -225,8 +242,8 @@ async function mainLoop () {
browse = null browse = null
page = null page = null
throw err
console.error(err) console.error(err)
throw err
} }
} }