mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
d2974a9ff6
* refactor(adapters): convert adapter layer from TypeScript to JavaScript Core framework stays TypeScript; adapter layer moves to JS-first. Adapters are essentially "executable config + browser scripts" that barely use TS features — this simplifies the build/distribution pipeline by removing the dist/clis/ intermediate compilation step. Changes: - Convert all 753 adapter files in clis/ from .ts to .js - Update tsconfig to exclude clis/ from compilation - Simplify build-manifest to scan clis/*.js directly (no dist/clis/) - Update discovery, main, fetch-adapters to load JS adapters from clis/ - Update generate-verified to output .js artifacts - Update package.json files field: dist/clis/ → clis/ - Fix all test files for the .ts → .js transition * fix(main): use findPackageRoot for BUILTIN_CLIS path The previous relative path (../../clis from __dirname) only worked for dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname is <repo>/src — resolving to /clis instead of <repo>/clis. Use findPackageRoot() which works for both dev and prod paths.
96 lines
4.3 KiB
JavaScript
96 lines
4.3 KiB
JavaScript
import { AuthRequiredError, SelectorError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
cli({
|
|
site: 'twitter',
|
|
name: 'following',
|
|
description: 'Get accounts a Twitter/X user is following',
|
|
domain: 'x.com',
|
|
strategy: Strategy.INTERCEPT,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'user', positional: true, type: 'string', required: false },
|
|
{ name: 'limit', type: 'int', default: 50 },
|
|
],
|
|
columns: ['screen_name', 'name', 'bio', 'followers'],
|
|
func: async (page, kwargs) => {
|
|
let targetUser = kwargs.user;
|
|
// If no user is specified, figure out the logged-in user's handle
|
|
if (!targetUser) {
|
|
await page.goto('https://x.com/home');
|
|
await page.wait({ selector: '[data-testid="primaryColumn"]' });
|
|
const href = await page.evaluate(`() => {
|
|
const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
|
|
return link ? link.getAttribute('href') : null;
|
|
}`);
|
|
if (!href) {
|
|
throw new AuthRequiredError('x.com', 'Could not find logged-in user profile link. Are you logged in?');
|
|
}
|
|
targetUser = href.replace('/', '');
|
|
}
|
|
// 1. Navigate to profile page
|
|
await page.goto(`https://x.com/${targetUser}`);
|
|
await page.wait(3);
|
|
// 2. Install interceptor BEFORE SPA navigation.
|
|
// goto() resets JS context, but SPA click preserves it.
|
|
await page.installInterceptor('Following');
|
|
// 3. Click the following link via SPA navigation (preserves interceptor)
|
|
const safeUser = JSON.stringify(targetUser);
|
|
const clicked = await page.evaluate(`() => {
|
|
const target = ${safeUser};
|
|
const link = document.querySelector('a[href="/' + target + '/following"]');
|
|
if (link) { link.click(); return true; }
|
|
return false;
|
|
}`);
|
|
if (!clicked) {
|
|
throw new SelectorError('Twitter following link', 'Twitter may have changed the layout.');
|
|
}
|
|
await page.waitForCapture(5);
|
|
// 4. Scroll to trigger pagination API calls
|
|
await page.autoScroll({ times: Math.ceil(kwargs.limit / 20), delayMs: 2000 });
|
|
// 5. Retrieve intercepted data
|
|
const requests = await page.getInterceptedRequests();
|
|
const requestList = Array.isArray(requests) ? requests : [];
|
|
if (requestList.length === 0) {
|
|
return [];
|
|
}
|
|
let results = [];
|
|
for (const req of requestList) {
|
|
try {
|
|
// GraphQL response: { data: { user: { result: { timeline: ... } } } }
|
|
let instructions = req.data?.user?.result?.timeline?.timeline?.instructions;
|
|
if (!instructions)
|
|
continue;
|
|
let addEntries = instructions.find((i) => i.type === 'TimelineAddEntries');
|
|
if (!addEntries) {
|
|
addEntries = instructions.find((i) => i.entries && Array.isArray(i.entries));
|
|
}
|
|
if (!addEntries)
|
|
continue;
|
|
for (const entry of addEntries.entries) {
|
|
if (!entry.entryId.startsWith('user-'))
|
|
continue;
|
|
const item = entry.content?.itemContent?.user_results?.result;
|
|
if (!item || item.__typename !== 'User')
|
|
continue;
|
|
const core = item.core || {};
|
|
const legacy = item.legacy || {};
|
|
results.push({
|
|
screen_name: core.screen_name || legacy.screen_name || 'unknown',
|
|
name: core.name || legacy.name || 'unknown',
|
|
bio: legacy.description || item.profile_bio?.description || '',
|
|
followers: legacy.followers_count || legacy.normal_followers_count || 0
|
|
});
|
|
}
|
|
}
|
|
catch (e) {
|
|
// ignore parsing errors for individual payloads
|
|
}
|
|
}
|
|
// Deduplicate by screen_name
|
|
const unique = new Map();
|
|
results.forEach(r => unique.set(r.screen_name, r));
|
|
const deduplicatedResults = Array.from(unique.values());
|
|
return deduplicatedResults.slice(0, kwargs.limit);
|
|
}
|
|
});
|