Skip to content

Commit

Permalink
feat: include raw tweet as part of results
Browse files Browse the repository at this point in the history
  • Loading branch information
karashiiro committed Jul 13, 2024
1 parent 9216f30 commit 79b4ac2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
15 changes: 9 additions & 6 deletions src/timeline-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,21 @@ export type ParseTweetResult =

function parseTimelineTweet(
timeline: TimelineV1,
id: string,
tweetId: string,
): ParseTweetResult {
const tweets = timeline.globalObjects?.tweets ?? {};
const tweet = tweets[id];
const tweet: Readonly<LegacyTweetRaw> | undefined = tweets[tweetId];
if (tweet?.user_id_str == null) {
return {
success: false,
err: new Error(`Tweet "${id}" was not found in the timeline object.`),
err: new Error(
`Tweet "${tweetId}" was not found in the timeline object.`,
),
};
}

const users = timeline.globalObjects?.users ?? {};
const user = users[tweet.user_id_str];
const user: Readonly<LegacyUserRaw> | undefined = users[tweet.user_id_str];
if (user?.screen_name == null) {
return {
success: false,
Expand All @@ -259,8 +261,9 @@ function parseTimelineTweet(
const { photos, videos, sensitiveContent } = parseMediaGroups(media);

const tw: Tweet = {
__raw_UNSTABLE: tweet,
conversationId: tweet.conversation_id_str,
id,
id: tweetId,
hashtags: hashtags
.filter(isFieldDefined('text'))
.map((hashtag) => hashtag.text),
Expand All @@ -271,7 +274,7 @@ function parseTimelineTweet(
name: mention.name,
})),
name: user.name,
permanentUrl: `https://twitter.com/${user.screen_name}/status/${id}`,
permanentUrl: `https://twitter.com/${user.screen_name}/status/${tweetId}`,
photos,
replies: tweet.reply_count,
retweets: tweet.retweet_count,
Expand Down
34 changes: 20 additions & 14 deletions src/timeline-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,17 @@ export interface ThreadedConversation {
};
}

function getLegacyTweetId(tweet: Readonly<LegacyTweetRaw>): string | undefined {
if (tweet.id_str) {
return tweet.id_str;
}

return tweet.conversation_id_str;
}

export function parseLegacyTweet(
user?: LegacyUserRaw,
tweet?: LegacyTweetRaw,
user?: Readonly<LegacyUserRaw>,
tweet?: Readonly<LegacyTweetRaw>,
): ParseTweetResult {
if (tweet == null) {
return {
Expand All @@ -121,15 +129,12 @@ export function parseLegacyTweet(
};
}

if (!tweet.id_str) {
if (!tweet.conversation_id_str) {
return {
success: false,
err: new Error('Tweet ID was not found in object.'),
};
}

tweet.id_str = tweet.conversation_id_str;
const tweetId = getLegacyTweetId(tweet);
if (!tweetId) {
return {
success: false,
err: new Error('Tweet ID was not found in object.'),
};
}

const hashtags = tweet.entities?.hashtags ?? [];
Expand All @@ -142,9 +147,10 @@ export function parseLegacyTweet(
const { photos, videos, sensitiveContent } = parseMediaGroups(media);

const tw: Tweet = {
__raw_UNSTABLE: tweet,
bookmarkCount: tweet.bookmark_count,
conversationId: tweet.conversation_id_str,
id: tweet.id_str,
id: tweetId,
hashtags: hashtags
.filter(isFieldDefined('text'))
.map((hashtag) => hashtag.text),
Expand All @@ -155,7 +161,7 @@ export function parseLegacyTweet(
name: mention.name,
})),
name: user.name,
permanentUrl: `https://twitter.com/${user.screen_name}/status/${tweet.id_str}`,
permanentUrl: `https://twitter.com/${user.screen_name}/status/${tweetId}`,
photos,
replies: tweet.reply_count,
retweets: tweet.retweet_count,
Expand Down Expand Up @@ -219,7 +225,7 @@ export function parseLegacyTweet(
tw.views = views;
}

if (pinnedTweets.has(tweet.id_str)) {
if (pinnedTweets.has(tweetId)) {
// TODO: Update tests so this can be assigned at the tweet declaration
tw.isPin = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/tweets.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getScraper } from './test-utils';
import { Mention, Tweet } from './tweets';
import { QueryTweetsResponse } from './timeline-v1';
import { SearchMode } from './search';

test('scraper can get tweet', async () => {
const expected: Tweet = {
Expand Down
3 changes: 2 additions & 1 deletion src/tweets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addApiFeatures, requestApi } from './api';
import { TwitterAuth } from './auth';
import { getUserIdByScreenName } from './profile';
import { QueryTweetsResponse } from './timeline-v1';
import { LegacyTweetRaw, QueryTweetsResponse } from './timeline-v1';
import {
parseTimelineTweetsV2,
TimelineV2,
Expand Down Expand Up @@ -49,6 +49,7 @@ export interface PlaceRaw {
* A parsed Tweet object.
*/
export interface Tweet {
__raw_UNSTABLE?: LegacyTweetRaw;
bookmarkCount?: number;
conversationId?: string;
hashtags: string[];
Expand Down

0 comments on commit 79b4ac2

Please sign in to comment.