From 61cb4818c018ec3546c36948903df29b99291dc8 Mon Sep 17 00:00:00 2001 From: bossgeekgo Date: Sat, 10 Feb 2024 12:42:12 +0800 Subject: [PATCH] handle today chance used out --- main/index.mjs | 82 +++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/main/index.mjs b/main/index.mjs index 6d74a32..c15bbea 100644 --- a/main/index.mjs +++ b/main/index.mjs @@ -187,51 +187,59 @@ async function mainLoop () { return false } ); - try { - const res = await addFriendResponse.json() - - if (res.code !== 0) { - console.err(res) - break - } - } 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) + const res = await addFriendResponse.json() + + if (res.code !== 0) { + // startup chat error, may the chance of today has used out + if (res.zpData.bizCode === 1 && res.zpData.bizData?.chatRemindDialog?.blockLevel === 0 && res.zpData.bizData?.chatRemindDialog?.content === `今日沟通人数已达上限,请明天再试`) { + throw new Error('STARTUP_CHAT_ERROR_DUE_TO_TODAY_CHANCE_HAS_USED_OUT') + } else { + console.error(res) + throw new Error('STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR') } + } else { + blockBossNotNewChat.add(jobData.jobInfo.encryptUserId) + + await sleepWithRandomDelay(750) + const closeDialogButtonProxy = await page.$('.greet-boss-dialog .greet-boss-footer .cancel-btn') + await closeDialogButtonProxy.click() + await sleepWithRandomDelay(1000) } } else { blockBossNotNewChat.add(jobData.jobInfo.encryptUserId) } } } catch (err) { - if (err instanceof Error && err.message === 'CANNOT_FIND_EXCEPT_JOB') { - if ( - currentExceptJobIndex + 1 > expectJobList.length - ) { - await Promise.all([ - page.reload(), - page.waitForNavigation() - ]) - currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX - } else { - currentExceptJobIndex += 1 + if (err instanceof Error) { + switch (err.message) { + case 'CANNOT_FIND_EXCEPT_JOB': { + if ( + currentExceptJobIndex + 1 > expectJobList.length + ) { + await Promise.all([ + page.reload(), + page.waitForNavigation() + ]) + currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX + } else { + currentExceptJobIndex += 1 + } + continue afterPageLoad; + break; + } + case 'STARTUP_CHAT_ERROR_DUE_TO_TODAY_CHANCE_HAS_USED_OUT': { + let nextTrySeconds = 60 * 60 + console.error(`Today chance has used out. Just explore positions you\'ve chatted. New chat will be tried to start after ${nextTrySeconds} seconds.`) + await sleep(nextTrySeconds * 1000) + throw err + } + case 'STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR': { + throw err + } + default: { + throw err + } } - continue afterPageLoad; } else { throw err }