|
| 1 | +/** |
| 2 | + * Maimai talent search - Browser cookie API. |
| 3 | + * Reuses Chrome login session to search for candidates on maimai.cn |
| 4 | + */ |
| 5 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 6 | + |
| 7 | +cli({ |
| 8 | + site: 'maimai', |
| 9 | + name: 'search-talents', |
| 10 | + description: 'Search for candidates on Maimai with multi-dimensional filters', |
| 11 | + domain: 'maimai.cn', |
| 12 | + strategy: Strategy.COOKIE, |
| 13 | + browser: true, |
| 14 | + args: [ |
| 15 | + { name: 'query', positional: true, required: true, help: 'Search keyword (e.g., "Java", "产品经理")' }, |
| 16 | + { name: 'page', type: 'int', default: 0, help: 'Page number (0-based)' }, |
| 17 | + { name: 'size', type: 'int', default: 20, help: 'Results per page' }, |
| 18 | + { name: 'positions', help: 'Positions (e.g., "运营", "Java 开发工程师")' }, |
| 19 | + { name: 'companies', help: 'Companies, comma-separated (e.g., "百度", "字节跳动,阿里巴巴")' }, |
| 20 | + { name: 'schools', help: 'Schools, comma-separated (e.g., "北京大学", "清华大学,复旦大学")' }, |
| 21 | + { name: 'provinces', help: 'Provinces (e.g., "北京", "上海")' }, |
| 22 | + { name: 'cities', help: 'Cities (e.g., "北京市", "上海市")' }, |
| 23 | + { name: 'worktimes', help: 'Work years: 1=1-3y, 2=3-5y, 3=5-10y, 4=10+y' }, |
| 24 | + { name: 'degrees', help: 'Education: 1=大专,2=本科,3=硕士,4=博士,5=MBA' }, |
| 25 | + { name: 'professions', help: 'Industries: 01=互联网,02=金融,03=电子,04=通信' }, |
| 26 | + { name: 'is_211', type: 'int', help: '211 university: 0=any, 1=211' }, |
| 27 | + { name: 'is_985', type: 'int', help: '985 university: 0=any, 1=985' }, |
| 28 | + { name: 'sortby', type: 'int', default: 0, help: 'Sort: 0=relevance, 1=activity, 2=work_years, 3=education' }, |
| 29 | + { name: 'is_direct_chat', type: 'int', default: 0, help: 'Direct chat: 0=any, 1=available' }, |
| 30 | + ], |
| 31 | + columns: ['name', 'job_title', 'company', 'historical_companies', 'location', 'work_year', 'school', 'degree', 'active_status', 'age', 'tags', 'mutual_friends'], |
| 32 | + func: async (page, kwargs) => { |
| 33 | + const { |
| 34 | + query, |
| 35 | + page: pageNum = 0, |
| 36 | + size = 20, |
| 37 | + positions = '', |
| 38 | + companies = '', |
| 39 | + schools = '', |
| 40 | + provinces = '', |
| 41 | + cities = '', |
| 42 | + worktimes = '', |
| 43 | + degrees = '', |
| 44 | + professions = '', |
| 45 | + is_211 = 0, |
| 46 | + is_985 = 0, |
| 47 | + sortby = 0, |
| 48 | + is_direct_chat = 0, |
| 49 | + } = kwargs; |
| 50 | + |
| 51 | + // Navigate to the search page |
| 52 | + await page.goto('https://maimai.cn/ent/talents/discover/search_v2', { waitUntil: 'networkidle' }); |
| 53 | + await page.waitForTimeout(5000); |
| 54 | + |
| 55 | + // Generate random session IDs |
| 56 | + const sessionid = 'b92d0fb5-f3fd-1f4b-fcdc-' + Math.random().toString(16).slice(2, 14); |
| 57 | + const deletesessionid = 'ae907d75-315c-8db7-2cc7-' + Math.random().toString(16).slice(2, 14); |
| 58 | + |
| 59 | + const requestBody = { |
| 60 | + search: { |
| 61 | + page: pageNum, |
| 62 | + size: size, |
| 63 | + sessionid: sessionid, |
| 64 | + deletesessionid: deletesessionid, |
| 65 | + worktimes: worktimes, |
| 66 | + degrees: degrees, |
| 67 | + professions: professions, |
| 68 | + schools: schools, |
| 69 | + positions: positions, |
| 70 | + companyscope: 0, |
| 71 | + sortby: sortby, |
| 72 | + is_direct_chat: is_direct_chat, |
| 73 | + query: query, |
| 74 | + cities: cities, |
| 75 | + provinces: provinces, |
| 76 | + is_211: is_211, |
| 77 | + is_985: is_985, |
| 78 | + allcompanies: companies, |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + // Execute the search API call in browser context |
| 83 | + const data = await page.evaluate(async (body) => { |
| 84 | + // Get CSRF token from cookie or meta tag |
| 85 | + let csrftoken = document.cookie.split('; ') |
| 86 | + .find(row => row.startsWith('csrftoken=')) |
| 87 | + ?.split('=')[1] || ''; |
| 88 | + |
| 89 | + if (!csrftoken) { |
| 90 | + const meta = document.querySelector('meta[name="csrf-token"]'); |
| 91 | + if (meta) csrftoken = meta.getAttribute('content') || ''; |
| 92 | + } |
| 93 | + |
| 94 | + const res = await fetch('https://maimai.cn/api/ent/discover/search?channel=www&data_version=3.0&version=1.0.0', { |
| 95 | + method: 'POST', |
| 96 | + headers: { |
| 97 | + 'accept': '*/*', |
| 98 | + 'content-type': 'text/plain;charset=UTF-8', |
| 99 | + 'origin': 'https://maimai.cn', |
| 100 | + 'referer': 'https://maimai.cn/ent/talents/discover/search_v2', |
| 101 | + 'x-csrf-token': csrftoken, |
| 102 | + }, |
| 103 | + credentials: 'include', |
| 104 | + body: JSON.stringify(body), |
| 105 | + }); |
| 106 | + |
| 107 | + const result = await res.json(); |
| 108 | + |
| 109 | + // Check login status |
| 110 | + if (res.status === 401 || res.status === 403 || result.error_code === 20002) { |
| 111 | + throw new Error('需要登录!请先在浏览器中访问 maimai.cn 并登录'); |
| 112 | + } |
| 113 | + |
| 114 | + if (result.code !== 200 && result.code !== 0) { |
| 115 | + throw new Error(result.message || result.error || 'API 请求失败'); |
| 116 | + } |
| 117 | + |
| 118 | + return result; |
| 119 | + }, requestBody); |
| 120 | + |
| 121 | + // Extract talent list from response |
| 122 | + const talentList = data.data?.list || data.data?.talent_list || data.list || data.talent_list || []; |
| 123 | + |
| 124 | + if (!talentList || talentList.length === 0) { |
| 125 | + return [{ error: '未找到匹配的候选人', query: query }]; |
| 126 | + } |
| 127 | + |
| 128 | + // Map to output format |
| 129 | + return talentList.map(item => { |
| 130 | + // Extract school info (first one) |
| 131 | + const schoolInfo = item.edu && item.edu.length > 0 ? item.edu[0] : {}; |
| 132 | + |
| 133 | + // Work years: use work_time field directly (e.g., "11 年", "10 年") |
| 134 | + const workYear = item.work_time || item.worktime || ''; |
| 135 | + |
| 136 | + // Extract all companies from work experience (deduplicated, excluding current company) |
| 137 | + const currentCompany = item.company || ''; |
| 138 | + const historicalCompanies = (item.exp || []) |
| 139 | + .map(e => e.company) |
| 140 | + .filter(c => c && c.trim() !== '' && c !== currentCompany) |
| 141 | + .filter((c, i, arr) => arr.indexOf(c) === i) |
| 142 | + .join(' / '); |
| 143 | + |
| 144 | + // Extract tags/skills from tag_list array |
| 145 | + const tags = (item.tag_list || item.tags || []) |
| 146 | + .filter(t => t && t.trim() !== '') |
| 147 | + .join(', '); |
| 148 | + |
| 149 | + // Extract mutual friends count and list |
| 150 | + const mutualFriendsCount = item.friends_cnt || item.common_friends_count || 0; |
| 151 | + const mutualFriendsList = (item.friends || item.common_friends || []) |
| 152 | + .map(f => f.name || f.user_name || f) |
| 153 | + .slice(0, 3) |
| 154 | + .join(', '); |
| 155 | + |
| 156 | + return { |
| 157 | + name: item.name || '', |
| 158 | + job_title: item.position || item.job_title || '', |
| 159 | + company: currentCompany, |
| 160 | + historical_companies: historicalCompanies, |
| 161 | + location: (item.province || '') + (item.city ? '·' + item.city : ''), |
| 162 | + work_year: workYear, |
| 163 | + school: schoolInfo.school || schoolInfo.hover?.name || '', |
| 164 | + degree: schoolInfo.sdegree || schoolInfo.hover?.school_level || '', |
| 165 | + active_status: item.active_state_v2 || item.active_state_v1 || item.active_state || '', |
| 166 | + age: item.age || '', |
| 167 | + tags: tags, |
| 168 | + mutual_friends: mutualFriendsCount > 0 ? `${mutualFriendsCount}人${mutualFriendsList ? ' (' + mutualFriendsList + ')' : ''}` : '', |
| 169 | + }; |
| 170 | + }); |
| 171 | + }, |
| 172 | +}); |
0 commit comments