WIP: add the basic logic to iterate and change filter condition when no more job can find. TODO: make sure filter condition is sync between this app and recommend page in bosszhipin site.

This commit is contained in:
geekgeekrun
2024-11-23 13:28:41 +08:00
parent 00f9ef12df
commit c51c19a1d6
2 changed files with 413 additions and 356 deletions
@@ -1,13 +1,4 @@
//# region get all combinations
export function* combineFiltersWithConstraintsGenerator(selectedFilters) {
const {
salaryList = [],
experienceList = [],
degreeList = [],
scaleList = [],
industryList = []
} = selectedFilters
// 使用迭代生成组合
function combine(arr, min, max) {
const result = []
@@ -45,14 +36,37 @@ export function* combineFiltersWithConstraintsGenerator(selectedFilters) {
return result
}
// 生成符合限制条件的组合
const salaryComb = combine(salaryList, 0, 1) // Salary: 0-1 个
const experienceComb = combine(experienceList, 0, experienceList.length) // Experience: 0 个或更多
const degreeComb = combine(degreeList, 0, degreeList.length) // Degree: 0 个或更多
const scaleComb = combine(scaleList, 0, scaleList.length) // Scale: 0 个或更多
const industryComb = combine(industryList, 0, 3) // Industry: 0-3 个
// 生成符合"0"限制的组合
function combineWithZero(arr, min, max) {
let combineResult;
if (arr.includes(0)) {
// 如果包含 00不参与组合
combineResult = [].concat(
combine(
arr.filter((x) => x !== 0),
min,
max
)
);
} else {
// 如果不包含 0,直接生成组合
combineResult = [].concat(combine(arr, min, max));
}
return combineResult;
}
// 通过迭代生成所有组合,代替递归
export function* combineFiltersWithConstraintsGenerator(selectedFilters) {
const { salaryList, experienceList, degreeList, scaleList, industryList } =
selectedFilters;
// 生成符合限制条件的组合
const salaryComb = combineWithZero(salaryList, 0, 1) // Salary: 0-1 个
const experienceComb = combineWithZero(experienceList, 0, experienceList.length) // Experience: 0 个或更多
const degreeComb = combineWithZero(degreeList, 0, degreeList.length) // Degree: 0 个或更多
const scaleComb = combineWithZero(scaleList, 0, scaleList.length) // Scale: 0 个或更多
const industryComb = combineWithZero(industryList, 0, 3) // Industry: 0-3 个
// 通过迭代生成所有组合
for (const salary of salaryComb) {
for (const experience of experienceComb) {
for (const degree of degreeComb) {
@@ -74,27 +88,8 @@ export function* combineFiltersWithConstraintsGenerator(selectedFilters) {
//#endregion
//#region get count of combinations
// 计算从 n 个元素中选 r 个的组合数 C(n, r)
function combination(n, r) {
if (r > n) return 0
let numerator = 1,
denominator = 1
for (let i = 0; i < r; i++) {
numerator *= n - i
denominator *= i + 1
}
return numerator / denominator
}
// 计算符合限制条件的组合数量
function calculateCombinationCount(arrLength, min, max) {
let totalCombinations = 0
for (let i = min; i <= Math.min(max, arrLength); i++) {
totalCombinations += combination(arrLength, i)
}
return totalCombinations
}
export function calculateTotalCombinations(selectedFilters) {
const {
salaryList = [],
@@ -104,21 +99,15 @@ export function calculateTotalCombinations(selectedFilters) {
industryList = []
} = selectedFilters
// 计算每个条件的组合数量
const salaryCombCount = calculateCombinationCount(salaryList.length, 0, 1) // Salary: 0-1 个
const experienceCombCount = calculateCombinationCount(
experienceList.length,
0,
experienceList.length
) // Experience: 0 个或更多
const degreeCombCount = calculateCombinationCount(degreeList.length, 0, degreeList.length) // Degree: 0 个或更多
const scaleCombCount = calculateCombinationCount(scaleList.length, 0, scaleList.length) // Scale: 0 个或更多
const industryCombCount = calculateCombinationCount(industryList.length, 0, 3) // Industry: 0-3 个
// 生成符合限制条件的组合
const salaryComb = combineWithZero(salaryList, 0, 1) // Salary: 0-1 个
const experienceComb = combineWithZero(experienceList, 0, experienceList.length) // Experience: 0 个或更多
const degreeComb = combineWithZero(degreeList, 0, degreeList.length) // Degree: 0 个或更多
const scaleComb = combineWithZero(scaleList, 0, scaleList.length) // Scale: 0 个或更多
const industryComb = combineWithZero(industryList, 0, 3) // Industry: 0-3 个
// 总组合数是每个条件的组合数量的乘积
const totalCombinations =
salaryCombCount * experienceCombCount * degreeCombCount * scaleCombCount * industryCombCount
return totalCombinations
return [salaryComb, experienceComb, degreeComb, scaleComb, industryComb].reduce((accu, cur) => {
return accu * cur.length
}, 1)
}
//#endregion
@@ -12,6 +12,7 @@ import { EventEmitter } from 'node:events'
import { setDomainLocalStorage } from '@geekgeekrun/utils/puppeteer/local-storage.mjs'
import { readConfigFile, writeStorageFile, ensureConfigFileExist, readStorageFile, ensureStorageFileExist } from './runtime-file-utils.mjs'
import { calculateTotalCombinations, combineFiltersWithConstraintsGenerator } from './combineCalculator.mjs'
ensureConfigFileExist()
ensureStorageFileExist()
@@ -63,6 +64,8 @@ const bossLocalStorage = readStorageFile('boss-local-storage.json')
const targetCompanyList = readConfigFile('target-company-list.json').filter(it => !!it.trim());
const anyCombineRecommendJobFilter = readConfigFile('boss.json').anyCombineRecommendJobFilter
const localStoragePageUrl = `https://www.zhipin.com/desktop/`
const recommendJobPageUrl = `https://www.zhipin.com/web/geek/job-recommend`
@@ -138,6 +141,59 @@ async function markJobAsNotSuitInRecommendPage () {
}
}
async function setFilterCondition (selectedFilters) {
const {
salaryList = [],
experienceList = [],
degreeList = [],
scaleList = [],
industryList = []
} = selectedFilters
const placeholderTexts = ['薪资待遇', '工作经验', '学历要求', '公司规模']
const optionKaPrefixes = ['sel-job-rec-salary-', 'sel-job-rec-exp-', 'sel-job-rec-degree-', 'sel-job-rec-scale-']
const conditionArr = [salaryList, experienceList, degreeList, scaleList]
for(let i = 0; i < placeholderTexts.length; i++) {
const placeholderText = placeholderTexts[i]
const filterDropdownProxy = await (async () => {
const jsHandle = (await page.evaluateHandle((placeholderText) => {
const filterBar = document.querySelector('.job-recommend-main .job-recommend-search')
const dropdownEntry = filterBar.__vue__.$children.find(it => it.placeholder === placeholderText)
return dropdownEntry.$el
}, placeholderText)).asElement();
return jsHandle
})()
if (!filterDropdownProxy) {
continue
}
const filterDropdownCssList = await filterDropdownProxy.evaluate(el => Array.from(el.classList));
if (!filterDropdownCssList.includes('is-select') && !conditionArr[i].length) {
continue
} else {
const filterDropdownElBBox = await filterDropdownProxy.boundingBox()
await page.mouse.move(
filterDropdownElBBox.x + filterDropdownElBBox.width / 2,
filterDropdownElBBox.y + filterDropdownElBBox.height / 2,
)
await sleepWithRandomDelay(500)
const optionKaPrefix = optionKaPrefixes[i]
for(let j = 0; j < conditionArr[i].length; j++) {
const optionValue = conditionArr[i][j]
await sleepWithRandomDelay(500)
const optionElProxy = await page.$(`li[ka="${optionKaPrefix}${optionValue}"]`)
if (!optionElProxy) {
continue;
}
await optionElProxy.click()
}
await sleepWithRandomDelay(500)
}
}
}
async function toRecommendPage (hooks) {
let userInfoPromise = page.waitForResponse((response) => {
if (response.url().startsWith('https://www.zhipin.com/wapi/zpuser/wap/getUserInfo.json')) {
@@ -188,6 +244,13 @@ async function toRecommendPage (hooks) {
const INIT_START_EXCEPT_JOB_INDEX = 1
let currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
afterPageLoad: while (true) {
let expectJobList
iterateFilterCondition: for (
const filterCondition of combineFiltersWithConstraintsGenerator(
anyCombineRecommendJobFilter
)
) {
findInCurrentFilterCondition: while(true) {
await sleepWithRandomDelay(2500)
await Promise.all([
@@ -198,7 +261,7 @@ async function toRecommendPage (hooks) {
[...document.querySelectorAll('.job-recommend-main .recommend-search-expect .recommend-job-btn')].findIndex(it => it.classList.contains('active'))
`)
const expectJobList = await page.evaluate(`document.querySelector('.job-recommend-search')?.__vue__?.expectList`)
expectJobList = await page.evaluate(`document.querySelector('.job-recommend-search')?.__vue__?.expectList`)
if (currentActiveJobIndex === currentExceptJobIndex) {
// first navigation and can immediately start chat (recommend job)
} else {
@@ -219,6 +282,8 @@ async function toRecommendPage (hooks) {
await storeStorage(page).catch(() => void 0)
await sleepWithRandomDelay(2000)
}
await sleepWithRandomDelay(1500)
await setFilterCondition(filterCondition)
try {
const { targetJobIndex, targetJobData } = await new Promise(async (resolve, reject) => {
@@ -303,7 +368,7 @@ async function toRecommendPage (hooks) {
if (tempTargetJobIndexToCheckDetail < 0 && hasReachLastPage) {
// has reach last page and not find target job
reject(new Error('CANNOT_FIND_EXCEPT_JOB_IN_THIS_JOB_EXPECTATION'))
reject(new Error('CANNOT_FIND_EXCEPT_JOB_IN_THIS_FILTER_CONDITION'))
return
}
@@ -376,7 +441,7 @@ async function toRecommendPage (hooks) {
if (targetJobIndex < 0 && hasReachLastPage) {
// has reach last page and not find target job
reject(new Error('CANNOT_FIND_EXCEPT_JOB_IN_THIS_JOB_EXPECTATION'))
reject(new Error('CANNOT_FIND_EXCEPT_JOB_IN_THIS_FILTER_CONDITION'))
return
}
}
@@ -434,23 +499,8 @@ async function toRecommendPage (hooks) {
} catch (err) {
if (err instanceof Error) {
switch (err.message) {
case 'CANNOT_FIND_EXCEPT_JOB_IN_THIS_JOB_EXPECTATION': {
if (
currentExceptJobIndex + 1 > expectJobList.length
) {
hooks.noPositionFoundForCurrentJob?.call()
await Promise.all([
page.reload(),
page.waitForNavigation()
])
currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
} else {
hooks.noPositionFoundForCurrentJob?.call()
hooks.noPositionFoundAfterTraverseAllJob?.call()
currentExceptJobIndex += 1
}
continue afterPageLoad;
case 'CANNOT_FIND_EXCEPT_JOB_IN_THIS_FILTER_CONDITION': {
continue iterateFilterCondition;
}
case 'STARTUP_CHAT_ERROR_DUE_TO_TODAY_CHANCE_HAS_USED_OUT': {
let nextTrySeconds = 60 * 60
@@ -476,6 +526,24 @@ async function toRecommendPage (hooks) {
}
}
}
// for of reach terminal
if (
currentExceptJobIndex + 1 > expectJobList.length
) {
hooks.noPositionFoundForCurrentJob?.call()
await Promise.all([
page.reload(),
page.waitForNavigation()
])
currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
} else {
hooks.noPositionFoundForCurrentJob?.call()
hooks.noPositionFoundAfterTraverseAllJob?.call()
currentExceptJobIndex += 1
}
}
}
export async function mainLoop (hooks) {
if (!puppeteer) {