9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
41 lines
1.9 KiB
JavaScript
41 lines
1.9 KiB
JavaScript
import { CliError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { DESC_MAX_LEN, wikiFetch } from './utils.js';
|
|
cli({
|
|
site: 'wikipedia',
|
|
name: 'trending',
|
|
access: 'read',
|
|
description: 'Most-read Wikipedia articles (yesterday)',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: false,
|
|
args: [
|
|
{ name: 'limit', type: 'int', default: 10, help: 'Max results' },
|
|
{ name: 'lang', default: 'en', help: 'Language code (e.g. en, zh, ja)' },
|
|
],
|
|
columns: ['rank', 'title', 'description', 'views'],
|
|
func: async (args) => {
|
|
const lang = args.lang || 'en';
|
|
const limit = Math.max(1, Math.min(Number(args.limit), 50));
|
|
// Use yesterday's UTC date — Wikipedia API expects UTC and yesterday
|
|
// guarantees data availability (today's aggregation may be incomplete).
|
|
const d = new Date(Date.now() - 86_400_000);
|
|
const yyyy = d.getUTCFullYear();
|
|
const mm = String(d.getUTCMonth() + 1).padStart(2, '0');
|
|
const dd = String(d.getUTCDate()).padStart(2, '0');
|
|
const data = (await wikiFetch(lang, `/api/rest_v1/feed/featured/${yyyy}/${mm}/${dd}`));
|
|
const articles = data?.mostread?.articles;
|
|
if (!articles?.length)
|
|
throw new CliError('NOT_FOUND', 'No trending articles available', 'Try a different language with --lang');
|
|
const selectedArticles = articles.slice(0, limit);
|
|
if (selectedArticles.some((article) => !String(article?.title || '').trim())) {
|
|
throw new CliError('PARSE_ERROR', 'Wikipedia trending returned an article without title', 'Trending rows require a title so they can be opened with wikipedia page.');
|
|
}
|
|
return selectedArticles.map((a, i) => ({
|
|
rank: i + 1,
|
|
title: a.title,
|
|
description: (a.description ?? '').slice(0, DESC_MAX_LEN),
|
|
views: a.views ?? 0,
|
|
}));
|
|
},
|
|
});
|