|
| 1 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 2 | +import { CommandExecutionError } from '@jackwener/opencli/errors'; |
| 3 | +import { clampInt, requireNonEmptyQuery } from '../_shared/common.js'; |
| 4 | + |
| 5 | +cli({ |
| 6 | + site: 'google-scholar', |
| 7 | + name: 'profile', |
| 8 | + description: 'View a Google Scholar author profile', |
| 9 | + domain: 'scholar.google.com', |
| 10 | + strategy: Strategy.PUBLIC, |
| 11 | + browser: true, |
| 12 | + args: [ |
| 13 | + { name: 'author', positional: true, required: true, help: 'Author name or Scholar user ID (e.g. JicYPdAAAAAJ)' }, |
| 14 | + { name: 'limit', type: 'int', default: 10, help: 'Max papers to show (max 20)' }, |
| 15 | + ], |
| 16 | + columns: ['rank', 'title', 'cited', 'year'], |
| 17 | + navigateBefore: false, |
| 18 | + func: async (page, kwargs) => { |
| 19 | + const author = requireNonEmptyQuery(kwargs.author, 'author'); |
| 20 | + const limit = clampInt(kwargs.limit, 10, 1, 20); |
| 21 | + |
| 22 | + const isUserId = /^[A-Za-z0-9_-]{12}$/.test(author); |
| 23 | + if (isUserId) { |
| 24 | + await page.goto(`https://scholar.google.com/citations?user=${author}&hl=en&sortby=citedby`); |
| 25 | + } else { |
| 26 | + await page.goto(`https://scholar.google.com/citations?view_op=search_authors&mauthors=${encodeURIComponent(author)}&hl=en`); |
| 27 | + await page.wait(3); |
| 28 | + |
| 29 | + const profileClicked = await page.evaluate(`(() => { |
| 30 | + var link = document.querySelector('.gs_ai_pho, .gsc_oai_photo, a[href*="citations?user="]'); |
| 31 | + if (link) { link.click(); return true; } |
| 32 | + return false; |
| 33 | + })()`); |
| 34 | + |
| 35 | + if (!profileClicked) { |
| 36 | + throw new CommandExecutionError(`No profile found for: ${author}`); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + await page.wait(3); |
| 41 | + |
| 42 | + const data = await page.evaluate(`(() => { |
| 43 | + var name = (document.querySelector('#gsc_prf_in') || {}).textContent || ''; |
| 44 | + var affiliation = (document.querySelector('.gsc_prf_il') || {}).textContent || ''; |
| 45 | +
|
| 46 | + var stats = document.querySelectorAll('#gsc_rsb_st td.gsc_rsb_std'); |
| 47 | + var citations = stats[0] ? stats[0].textContent.trim() : ''; |
| 48 | + var hIndex = stats[2] ? stats[2].textContent.trim() : ''; |
| 49 | + var i10Index = stats[4] ? stats[4].textContent.trim() : ''; |
| 50 | +
|
| 51 | + var papers = []; |
| 52 | + var rows = document.querySelectorAll('#gsc_a_b .gsc_a_tr'); |
| 53 | + for (var i = 0; i < rows.length && i < ${limit}; i++) { |
| 54 | + var titleEl = rows[i].querySelector('.gsc_a_at'); |
| 55 | + var citedEl = rows[i].querySelector('.gsc_a_ac'); |
| 56 | + var yearEl = rows[i].querySelector('.gsc_a_y span'); |
| 57 | + if (titleEl) papers.push({ |
| 58 | + rank: i + 1, |
| 59 | + title: titleEl.textContent.trim(), |
| 60 | + cited: citedEl ? citedEl.textContent.trim() : '0', |
| 61 | + year: yearEl ? yearEl.textContent.trim() : '', |
| 62 | + }); |
| 63 | + } |
| 64 | +
|
| 65 | + return { |
| 66 | + name: name.trim(), |
| 67 | + affiliation: affiliation.trim(), |
| 68 | + citations: citations, |
| 69 | + hIndex: hIndex, |
| 70 | + i10Index: i10Index, |
| 71 | + papers: papers, |
| 72 | + }; |
| 73 | + })()`); |
| 74 | + |
| 75 | + if (!data?.name) { |
| 76 | + throw new CommandExecutionError(`Could not load Google Scholar profile for: ${author}`); |
| 77 | + } |
| 78 | + |
| 79 | + if (!data.papers || data.papers.length === 0) { |
| 80 | + throw new CommandExecutionError(`No papers found for: ${data.name || author}`); |
| 81 | + } |
| 82 | + |
| 83 | + const summary = { |
| 84 | + rank: 0, |
| 85 | + title: data.name + (data.affiliation ? ' (' + data.affiliation + ')' : ''), |
| 86 | + cited: 'h=' + data.hIndex + ' i10=' + data.i10Index + ' total=' + data.citations, |
| 87 | + year: '-', |
| 88 | + }; |
| 89 | + |
| 90 | + return [summary, ...data.papers]; |
| 91 | + }, |
| 92 | +}); |
0 commit comments