|
| 1 | +import { CliError } from '../../errors.js'; |
| 2 | +import { cli, Strategy } from '../../registry.js'; |
| 3 | +import { DESC_MAX_LEN, type WikiMostReadArticle, wikiFetch } from './utils.js'; |
| 4 | + |
| 5 | +cli({ |
| 6 | + site: 'wikipedia', |
| 7 | + name: 'trending', |
| 8 | + description: 'Most-read Wikipedia articles (yesterday)', |
| 9 | + strategy: Strategy.PUBLIC, |
| 10 | + browser: false, |
| 11 | + args: [ |
| 12 | + { name: 'limit', type: 'int', default: 10, help: 'Max results' }, |
| 13 | + { name: 'lang', default: 'en', help: 'Language code (e.g. en, zh, ja)' }, |
| 14 | + ], |
| 15 | + columns: ['rank', 'title', 'description', 'views'], |
| 16 | + func: async (_page, args) => { |
| 17 | + const lang = args.lang || 'en'; |
| 18 | + const limit = Math.max(1, Math.min(Number(args.limit), 50)); |
| 19 | + |
| 20 | + // Use yesterday's UTC date — Wikipedia API expects UTC and yesterday |
| 21 | + // guarantees data availability (today's aggregation may be incomplete). |
| 22 | + const d = new Date(Date.now() - 86_400_000); |
| 23 | + const yyyy = d.getUTCFullYear(); |
| 24 | + const mm = String(d.getUTCMonth() + 1).padStart(2, '0'); |
| 25 | + const dd = String(d.getUTCDate()).padStart(2, '0'); |
| 26 | + |
| 27 | + const data = (await wikiFetch(lang, `/api/rest_v1/feed/featured/${yyyy}/${mm}/${dd}`)) as { |
| 28 | + mostread?: { articles?: WikiMostReadArticle[] }; |
| 29 | + }; |
| 30 | + const articles = data?.mostread?.articles; |
| 31 | + if (!articles?.length) |
| 32 | + throw new CliError('NOT_FOUND', 'No trending articles available', 'Try a different language with --lang'); |
| 33 | + |
| 34 | + return articles.slice(0, limit).map((a, i) => ({ |
| 35 | + rank: i + 1, |
| 36 | + title: a.title ?? '-', |
| 37 | + description: (a.description ?? '-').slice(0, DESC_MAX_LEN), |
| 38 | + views: a.views ?? 0, |
| 39 | + })); |
| 40 | + }, |
| 41 | +}); |
0 commit comments