mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
666a955fac
Adds two additive columns to the Twitter read commands (search, timeline, tweets, thread, likes): - has_media: boolean — true if the tweet contains any photo, video, or GIF - media_urls: string[] — photo URLs and mp4 variant URLs for videos/GIFs, extracted from legacy.extended_entities.media (falls back to entities.media) The INTERCEPT/COOKIE payloads already carry this data; this change only extends the row-mapping layer, so no new network work is needed. Pattern mirrors #465 (time column). Shared extraction helper lives in clis/twitter/shared.js so all five adapters stay consistent, with unit coverage for photo, video (mp4 variant selection), animated_gif, entities.media fallback, and the empty case. Closes #1107
69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
const QUERY_ID_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
export function sanitizeQueryId(resolved, fallbackId) {
|
|
return typeof resolved === 'string' && QUERY_ID_PATTERN.test(resolved) ? resolved : fallbackId;
|
|
}
|
|
export async function resolveTwitterQueryId(page, operationName, fallbackId) {
|
|
const resolved = await page.evaluate(`async () => {
|
|
const operationName = ${JSON.stringify(operationName)};
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
try {
|
|
const ghResp = await fetch('https://raw.githubusercontent.com/fa0311/twitter-openapi/refs/heads/main/src/config/placeholder.json', { signal: controller.signal });
|
|
clearTimeout(timeout);
|
|
if (ghResp.ok) {
|
|
const data = await ghResp.json();
|
|
const entry = data?.[operationName];
|
|
if (entry && entry.queryId) return entry.queryId;
|
|
}
|
|
} catch {
|
|
clearTimeout(timeout);
|
|
}
|
|
try {
|
|
const scripts = performance.getEntriesByType('resource')
|
|
.filter(r => r.name.includes('client-web') && r.name.endsWith('.js'))
|
|
.map(r => r.name);
|
|
for (const scriptUrl of scripts.slice(0, 15)) {
|
|
try {
|
|
const text = await (await fetch(scriptUrl)).text();
|
|
const re = new RegExp('queryId:"([A-Za-z0-9_-]+)"[^}]{0,200}operationName:"' + operationName + '"');
|
|
const match = text.match(re);
|
|
if (match) return match[1];
|
|
} catch {}
|
|
}
|
|
} catch {}
|
|
return null;
|
|
}`);
|
|
return sanitizeQueryId(resolved, fallbackId);
|
|
}
|
|
/**
|
|
* Extract media flags and URLs from a tweet's `legacy` object.
|
|
*
|
|
* Prefers `extended_entities.media` (superset with full video_info) and falls
|
|
* back to `entities.media` when the extended form is missing. For videos and
|
|
* animated GIFs, returns the mp4 variant URL; for photos, returns
|
|
* `media_url_https`.
|
|
*/
|
|
export function extractMedia(legacy) {
|
|
const media = legacy?.extended_entities?.media || legacy?.entities?.media;
|
|
if (!Array.isArray(media) || media.length === 0) {
|
|
return { has_media: false, media_urls: [] };
|
|
}
|
|
const urls = [];
|
|
for (const m of media) {
|
|
if (!m) continue;
|
|
if (m.type === 'video' || m.type === 'animated_gif') {
|
|
const variants = m.video_info?.variants || [];
|
|
const mp4 = variants.find((v) => v?.content_type === 'video/mp4');
|
|
const url = mp4?.url || m.media_url_https;
|
|
if (url) urls.push(url);
|
|
} else {
|
|
if (m.media_url_https) urls.push(m.media_url_https);
|
|
}
|
|
}
|
|
return { has_media: urls.length > 0, media_urls: urls };
|
|
}
|
|
export const __test__ = {
|
|
sanitizeQueryId,
|
|
extractMedia,
|
|
};
|