search.js
1 import { cli } from '@jackwener/opencli/registry'; 2 3 cli({ 4 site: 'nowcoder', 5 name: 'search', 6 description: 'Full-text search', 7 domain: 'www.nowcoder.com', 8 args: [ 9 { name: 'query', positional: true, required: true, help: 'Search keyword' }, 10 { name: 'type', type: 'str', default: 'all', help: 'Search type (all/post/question/user/job)' }, 11 { name: 'limit', type: 'int', default: 10, help: 'Number of results' }, 12 ], 13 columns: ['rank', 'title', 'author', 'school', 'content', 'id'], 14 pipeline: [ 15 { navigate: 'https://www.nowcoder.com' }, 16 { evaluate: `(async () => { 17 const query = \${{ args.query | json }}; 18 const type = \${{ args.type | json }}; 19 const limit = \${{ args.limit }}; 20 const strip = (html) => (html || '').replace(/<[^>]+>/g, '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/ /g, ' ').trim(); 21 const r = await fetch('https://gw-c.nowcoder.com/api/sparta/pc/search', { 22 method: 'POST', 23 credentials: 'include', 24 headers: {'Content-Type': 'application/json'}, 25 body: JSON.stringify({query, type, page: 1, pageSize: limit}) 26 }); 27 const d = await r.json(); 28 if (!d.success) throw new Error(d.msg || 'search failed'); 29 return (d.data?.records || []).map((item, i) => { 30 const data = item.data || {}; 31 const moment = data.momentData || {}; 32 const contentData = data.contentData || {}; 33 const user = data.userBrief || {}; 34 const uuid = moment.uuid || contentData.uuid || ''; 35 const id = data.contentId || ''; 36 return { 37 rank: i + 1, 38 title: moment.title || contentData.title || user.nickname || '', 39 author: user.nickname || '', 40 school: user.educationInfo || '', 41 content: strip(moment.content || contentData.content || ''), 42 id: uuid || id, 43 }; 44 }).filter(r => r.title); 45 })() 46 ` }, 47 { limit: '${{ args.limit }}' }, 48 ], 49 });