when arrive the terminal of the recommend job list, reload page and continue trying to find except job

This commit is contained in:
bossgeekgo
2024-02-09 19:22:59 +08:00
parent 21119e346b
commit 6e8f6cbf81
+117 -105
View File
@@ -50,7 +50,7 @@ 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()
while (true) { afterPageLoad: while (true) {
await sleepWithRandomDelay(3000) await sleepWithRandomDelay(3000)
const expectJobList = (await page.evaluate(` const expectJobList = (await page.evaluate(`
@@ -75,134 +75,146 @@ async function mainLoop () {
); );
await sleepWithRandomDelay(2000) await sleepWithRandomDelay(2000)
const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => { try {
// job list const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => {
const recommendJobListElProxy = await page.$('.job-list-container .rec-job-list') // job list
const recommendJobListElProxy = await page.$('.job-list-container .rec-job-list')
let jobListData = await page.evaluate( let jobListData = await page.evaluate(
`
document.querySelector('.job-recommend-main')?.__vue__?.jobList
`
)
let targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name)))
let hasReachLastPage = false
while (targetJobIndex < 0 && !hasReachLastPage) {
// fetch new
const recommendJobListElBBox = await recommendJobListElProxy.boundingBox()
const windowInnerHeight = await page.evaluate('window.innerHeight')
await page.mouse.move(
recommendJobListElBBox.x + recommendJobListElBBox.width / 2,
windowInnerHeight / 2
)
let scrolledHeight = 0
const targetHeight = 3000
const increase = 40 + Math.floor(30 * Math.random())
while (scrolledHeight < targetHeight) {
scrolledHeight += increase
await page.mouse.wheel({deltaY: increase});
await sleep(1)
}
hasReachLastPage = await page.evaluate(`
!(document.querySelector('.job-recommend-main')?.__vue__?.hasMore)
`)
if (hasReachLastPage) {
console.log(`Arrive the terminal of the job list.`)
}
await sleep(3000)
jobListData = await page.evaluate(
` `
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))) let targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name)))
} let hasReachLastPage = false
if (targetJobIndex < 0 && hasReachLastPage) { while (targetJobIndex < 0 && !hasReachLastPage) {
// has reach last page and not find target job // fetch new
reject(new Error('CANNOT_FIND_EXCEPT_JOB')) const recommendJobListElBBox = await recommendJobListElProxy.boundingBox()
return const windowInnerHeight = await page.evaluate('window.innerHeight')
} await page.mouse.move(
recommendJobListElBBox.x + recommendJobListElBBox.width / 2,
const recommendJobItemList = await recommendJobListElProxy.$$('ul.rec-job-list > li') windowInnerHeight / 2
resolve( )
{ let scrolledHeight = 0
targetJobElProxy: recommendJobItemList[targetJobIndex], const targetHeight = 3000
targetJobIndex const increase = 40 + Math.floor(30 * Math.random())
} while (scrolledHeight < targetHeight) {
) scrolledHeight += increase
}) await page.mouse.wheel({deltaY: increase});
if (targetJobIndex > 0) { await sleep(1)
// scroll that target element into view
await page.evaluate(`
const targetEl = document.querySelector("ul.rec-job-list").children[${targetJobIndex}]
targetEl.scrollIntoView({
behavior: 'smooth',
block: ${Math.random() > 0.5 ? '\'center\'' : '\'end\''}
})
`)
await sleepWithRandomDelay(200)
// click that element
await targetJobElProxy.click()
await page.waitForResponse(
response => {
if (
response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/job/detail.json')
) {
return true
} }
return false hasReachLastPage = await page.evaluate(`
!(document.querySelector('.job-recommend-main')?.__vue__?.hasMore)
`)
if (hasReachLastPage) {
console.log(`Arrive the terminal of the job list.`)
}
await sleep(3000)
jobListData = await page.evaluate(
`
document.querySelector('.job-recommend-main')?.__vue__?.jobList
`
)
targetJobIndex = targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name)))
} }
);
await sleepWithRandomDelay(2000)
const jobData = await page.evaluate('document.querySelector(".job-detail-box").__vue__.data')
const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()') if (targetJobIndex < 0 && hasReachLastPage) {
if (startChatButtonInnerHTML === '立即沟通') { // has reach last page and not find target job
const startChatButtonProxy = await page.$('.job-detail-box .op-btn.op-btn-chat') reject(new Error('CANNOT_FIND_EXCEPT_JOB'))
await startChatButtonProxy.click() return
}
const addFriendResponse = await page.waitForResponse( const recommendJobItemList = await recommendJobListElProxy.$$('ul.rec-job-list > li')
resolve(
{
targetJobElProxy: recommendJobItemList[targetJobIndex],
targetJobIndex
}
)
})
if (targetJobIndex > 0) {
// scroll that target element into view
await page.evaluate(`
const targetEl = document.querySelector("ul.rec-job-list").children[${targetJobIndex}]
targetEl.scrollIntoView({
behavior: 'smooth',
block: ${Math.random() > 0.5 ? '\'center\'' : '\'end\''}
})
`)
await sleepWithRandomDelay(200)
// click that element
await targetJobElProxy.click()
await page.waitForResponse(
response => { response => {
if ( if (
response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/friend/add.json') && response.url().includes(`jobId=${jobData.jobInfo.encryptId}`) response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/job/detail.json')
) { ) {
return true return true
} }
return false return false
} }
); );
try { await sleepWithRandomDelay(2000)
const res = await addFriendResponse.json() const jobData = await page.evaluate('document.querySelector(".job-detail-box").__vue__.data')
if (res.code !== 0) { const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()')
console.err(res) if (startChatButtonInnerHTML === '立即沟通') {
break const startChatButtonProxy = await page.$('.job-detail-box .op-btn.op-btn-chat')
} await startChatButtonProxy.click()
} catch(err) {
// console.warn(err)
} finally {
//#region TODO: temporary work with legacy logic
await sleep(500)
const continueChatButtonProxy = await page.$('.greet-boss-dialog .greet-boss-footer .sure-btn')
await continueChatButtonProxy.click() const addFriendResponse = await page.waitForResponse(
//#endregion response => {
await sleepWithRandomDelay(2500) if (
if (page.url().startsWith('https://www.zhipin.com/web/geek/chat')) { response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/friend/add.json') && response.url().includes(`jobId=${jobData.jobInfo.encryptId}`)
await sleepWithRandomDelay(3000) ) {
return true
}
return false
}
);
try {
const res = await addFriendResponse.json()
await Promise.all([ if (res.code !== 0) {
page.waitForNavigation(), console.err(res)
page.goBack(), break
]) }
await sleepWithRandomDelay(1000) } catch(err) {
// console.warn(err)
} finally {
//#region TODO: temporary work with legacy logic
await sleep(500)
const continueChatButtonProxy = await page.$('.greet-boss-dialog .greet-boss-footer .sure-btn')
await continueChatButtonProxy.click()
//#endregion
await sleepWithRandomDelay(2500)
if (page.url().startsWith('https://www.zhipin.com/web/geek/chat')) {
await sleepWithRandomDelay(3000)
await Promise.all([
page.waitForNavigation(),
page.goBack(),
])
await sleepWithRandomDelay(1000)
}
} }
} else {
} }
}
} catch (err) {
if (err instanceof Error && err.message === 'CANNOT_FIND_EXCEPT_JOB') {
await Promise.all([
page.reload(),
page.waitForNavigation()
])
continue afterPageLoad;
} else { } else {
throw err
} }
} }
} }