diff --git a/docs/programming.md b/docs/programming.md index 0d940870b4..bc4349cc1e 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -510,6 +510,18 @@ GitHub 官方也提供了一些 RSS: +## 拉勾网 + +::: tip 提示 + +拉勾网官方提供职位的[邮件订阅](https://www.lagou.com/s/subscribe.html),请根据自身需要选择使用。 + +::: + +### 职位招聘 + + + ## 洛谷 ### 日报 diff --git a/lib/router.js b/lib/router.js index 221da5b38b..8c6104d05c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2321,6 +2321,9 @@ router.get('/blogs/hedwig/:type', require('./routes/blogs/hedwig')); // LoveHeaven router.get('/loveheaven/update/:slug', require('./routes/loveheaven/update')); +// 拉勾 +router.get('/lagou/jobs/:position/:city', require('./routes/lagou/jobs')); + // 扬州大学 router.get('/yzu/home/:type', require('./routes/universities/yzu/home')); router.get('/yzu/yjszs/:type', require('./routes/universities/yzu/yjszs')); diff --git a/lib/routes/lagou/jobs.js b/lib/routes/lagou/jobs.js new file mode 100644 index 0000000000..886ba04060 --- /dev/null +++ b/lib/routes/lagou/jobs.js @@ -0,0 +1,59 @@ +const got = require('@/utils/got'); + +async function getCookie(url, ctx) { + const cache_key = `lagou-cookie-${new Date().toLocaleDateString()}`; + const cached_cookie = await ctx.cache.get(cache_key); + if (cached_cookie) { + return cached_cookie; + } + const { headers } = await got.get(url, { headers: { Referer: 'https://www.lagou.com/' } }); + const cookie = headers['set-cookie'] + .filter((c) => c.match(/(user_trace_token|X_HTTP_TOKEN)/)) + .map((c) => c.split(';')[0]) + .join('; '); + ctx.cache.set(cache_key, cookie); + return cookie; +} + +module.exports = async (ctx) => { + const city = ctx.params.city; + const position = ctx.params.position; + + const url = `https://www.lagou.com/jobs/list_${encodeURIComponent(position)}?px=new&city=${encodeURIComponent(city)}`; + + const cookie = await getCookie(url, ctx); + + const response = await got({ + method: 'post', + url: `https://www.lagou.com/jobs/positionAjax.json?px=new&city=${encodeURIComponent(city)}&needAddtionalResult=false`, + headers: { + Referer: url, + Cookie: cookie, + }, + form: { + first: true, + pn: 1, + kd: position, + }, + }); + + if (!response.data.success) { + throw response.data.msg; + } + + const list = response.data.content.positionResult.result; + + ctx.state.data = { + title: `${position} - ${city} - 拉勾网`, + link: url, + item: list.map((item) => ({ + title: `${item.positionName}[${city === '全国' ? item.city + '·' : ''}${item.businessZones ? item.businessZones[0] : item.district}] - ${item.companyShortName}`, + author: item.companyShortName, + description: `薪资:${item.salary}
要求:${item.workYear} / ${item.education}
公司名称:${item.companyFullName}
公司描述:${item.industryField} / ${item.financeStage} / ${item.companySize}${ + item.companyLabelList && item.companyLabelList.length !== 0 ? '
公司标签:' + item.companyLabelList.join(', ') : '' + }
职位优势:${item.positionAdvantage}${item.positionLables ? '
职位标签:' + item.positionLables.join(', ') : ''}${item.linestaion ? '
附近线路:' + item.linestaion : ''}`, + link: `https://www.lagou.com/jobs/${item.positionId}.html`, + pubDate: new Date(item.createTime), + })), + }; +};