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
62 lines
2.4 KiB
JavaScript
62 lines
2.4 KiB
JavaScript
// juejin recommend: Juejin homepage recommendation feed.
|
|
//
|
|
// Hits the `recommend_all_feed` endpoint, which mirrors what the Juejin web UI
|
|
// renders on the front page; `sort_type` 200 is the default "recommended" mix.
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import {
|
|
juejinFetch,
|
|
mapFeedItem,
|
|
readDataArray,
|
|
requireBoundedInt,
|
|
requireCursor,
|
|
} from './utils.js';
|
|
|
|
function readResponseCursor(value) {
|
|
if (value == null || value === '') return '';
|
|
try {
|
|
return requireCursor(value);
|
|
}
|
|
catch {
|
|
throw new CommandExecutionError('juejin recommend returned a malformed cursor');
|
|
}
|
|
}
|
|
|
|
cli({
|
|
site: 'juejin',
|
|
name: 'recommend',
|
|
access: 'read',
|
|
description: 'Juejin (掘金) homepage recommended article feed',
|
|
domain: 'api.juejin.cn',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: false,
|
|
args: [
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Max articles (1-100, single page).' },
|
|
{ name: 'cursor', type: 'string', default: '0', help: 'Pagination cursor; pass back the previous response\'s next-page cursor to keep scrolling.' },
|
|
],
|
|
columns: ['rank', 'article_id', 'title', 'brief', 'views', 'likes', 'comments', 'author', 'tags', 'url', 'next_cursor', 'has_more'],
|
|
func: async (args) => {
|
|
const limit = requireBoundedInt(args.limit, 20, 100);
|
|
const cursor = requireCursor(args.cursor);
|
|
const payload = await juejinFetch(
|
|
'/recommend_api/v1/article/recommend_all_feed',
|
|
{ id_type: 2, client_type: 2608, sort_type: 200, limit, cursor },
|
|
'juejin recommend',
|
|
);
|
|
const data = readDataArray(payload, 'juejin recommend');
|
|
const nextCursor = readResponseCursor(payload.cursor);
|
|
if (payload.has_more != null && typeof payload.has_more !== 'boolean') {
|
|
throw new CommandExecutionError('juejin recommend returned a malformed has_more flag');
|
|
}
|
|
const hasMore = payload.has_more == null ? '' : String(payload.has_more);
|
|
if (payload.has_more === true && !nextCursor) {
|
|
throw new CommandExecutionError('juejin recommend returned has_more without a next cursor');
|
|
}
|
|
return data.slice(0, limit).map((row, i) => ({
|
|
...mapFeedItem(row, i + 1),
|
|
next_cursor: nextCursor,
|
|
has_more: hasMore,
|
|
}));
|
|
},
|
|
});
|