Improve tweet read reliability and test coverage

This commit is contained in:
Peter Steinberger
2025-11-28 08:07:44 +01:00
parent b2dc418860
commit 5889044d7b
5 changed files with 125 additions and 67 deletions
+12
View File
@@ -0,0 +1,12 @@
/**
* Extract tweet ID from a Twitter/X URL or return the input unchanged if it's already an ID.
*/
export function extractTweetId(input: string): string {
// If it's a URL, extract the tweet ID
const urlMatch = input.match(/(?:twitter\.com|x\.com)\/\w+\/status\/(\d+)/);
if (urlMatch) {
return urlMatch[1];
}
// Assume it's already an ID
return input;
}
+56 -37
View File
@@ -77,6 +77,55 @@ export class TwitterClient {
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
}
private findTweetInInstructions(
instructions:
| Array<{
entries?: Array<{
content?: {
itemContent?: {
tweet_results?: {
result?: {
rest_id?: string;
legacy?: {
full_text?: string;
created_at?: string;
reply_count?: number;
retweet_count?: number;
favorite_count?: number;
};
core?: {
user_results?: {
result?: {
legacy?: {
screen_name?: string;
name?: string;
};
};
};
};
};
};
};
};
}>;
}>
| undefined,
tweetId: string,
) {
if (!instructions) return undefined;
for (const instruction of instructions) {
for (const entry of instruction.entries || []) {
const result = entry.content?.itemContent?.tweet_results?.result;
if (result?.rest_id === tweetId) {
return result;
}
}
}
return undefined;
}
private getHeaders(): Record<string, string> {
return {
authorization:
@@ -222,43 +271,13 @@ export class TwitterClient {
};
}
// Try to find the tweet in the conversation thread
const instructions = data.data?.threaded_conversation_with_injections_v2?.instructions;
let tweetResult:
| {
rest_id?: string;
legacy?: {
full_text?: string;
created_at?: string;
reply_count?: number;
retweet_count?: number;
favorite_count?: number;
};
core?: {
user_results?: {
result?: {
legacy?: {
screen_name?: string;
name?: string;
};
};
};
};
}
| undefined;
if (instructions) {
for (const instruction of instructions) {
for (const entry of instruction.entries || []) {
const result = entry.content?.itemContent?.tweet_results?.result;
if (result?.rest_id === tweetId) {
tweetResult = result;
break;
}
}
if (tweetResult) break;
}
}
// Prefer direct tweetResult if present, otherwise search the conversation thread
let tweetResult =
data.data?.tweetResult?.result ??
this.findTweetInInstructions(
data.data?.threaded_conversation_with_injections_v2?.instructions,
tweetId,
);
if (!tweetResult) {
return {