Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Convert X (Twitter) tweets, threads, and articles to Markdown with YAML front matter via reverse-engineered API.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/referenced-tweets.ts
1import { fetchXTweet } from "./graphql.js";2import {3extractReferencedTweetIds,4type ReferencedTweetInfo,5} from "./markdown.js";67type ResolveReferencedTweetsOptions = {8log?: (message: string) => void;9};1011function extractReferencedTweetInfo(tweet: any, fallbackTweetId: string): ReferencedTweetInfo {12const userCore = tweet?.core?.user_results?.result?.core;13const userLegacy = tweet?.core?.user_results?.result?.legacy;1415const authorName =16typeof userCore?.name === "string"17? userCore.name18: typeof userLegacy?.name === "string"19? userLegacy.name20: undefined;2122const authorUsername =23typeof userCore?.screen_name === "string"24? userCore.screen_name25: typeof userLegacy?.screen_name === "string"26? userLegacy.screen_name27: undefined;2829const text =30tweet?.note_tweet?.note_tweet_results?.result?.text ??31tweet?.legacy?.full_text ??32tweet?.legacy?.text ??33undefined;3435const tweetId =36typeof tweet?.rest_id === "string" && tweet.rest_id.length > 037? tweet.rest_id38: fallbackTweetId;3940const url = authorUsername41? `https://x.com/${authorUsername}/status/${tweetId}`42: `https://x.com/i/web/status/${tweetId}`;4344return {45id: tweetId,46url,47authorName,48authorUsername,49text: typeof text === "string" ? text : undefined,50};51}5253export async function resolveReferencedTweetsFromArticle(54article: unknown,55cookieMap: Record<string, string>,56options: ResolveReferencedTweetsOptions = {}57): Promise<Map<string, ReferencedTweetInfo>> {58const log = options.log ?? (() => {});59const ids = extractReferencedTweetIds(article);60const referencedTweets = new Map<string, ReferencedTweetInfo>();6162for (const id of ids) {63try {64const tweet = await fetchXTweet(id, cookieMap, false);65const info = extractReferencedTweetInfo(tweet, id);66referencedTweets.set(id, info);67} catch (error) {68log(69`[x-to-markdown] Failed to fetch referenced tweet ${id}: ${70error instanceof Error ? error.message : String(error)71}`72);73referencedTweets.set(id, {74id,75url: `https://x.com/i/web/status/${id}`,76});77}78}7980return referencedTweets;81}82