Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Twitter fetchHomeTimeline rework #1021

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 52 additions & 59 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,74 +218,67 @@ export class ClientBase extends EventEmitter {
await this.populateTimeline();
}

async fetchHomeTimeline(count: number): Promise<Tweet[]> {
elizaLogger.debug("fetching home timeline");
async fetchOwnPosts(count: number): Promise<Tweet[]> {
elizaLogger.debug("fetching own posts");
const homeTimeline = await this.twitterClient.getUserTweets(
this.profile.id,
count
);

// console.dir(homeTimeline, { depth: Infinity });

return homeTimeline.tweets;
// .filter((t) => t.__typename !== "TweetWithVisibilityResults")
// .map((tweet) => {
// // console.log("tweet is", tweet);
// const obj = {
// id: tweet.id,
// name:
// tweet.name ??
// tweet. ?.user_results?.result?.legacy.name,
// username:
// tweet.username ??
// tweet.core?.user_results?.result?.legacy.screen_name,
// text: tweet.text ?? tweet.legacy?.full_text,
// inReplyToStatusId:
// tweet.inReplyToStatusId ??
// tweet.legacy?.in_reply_to_status_id_str,
// createdAt: tweet.createdAt ?? tweet.legacy?.created_at,
// userId: tweet.userId ?? tweet.legacy?.user_id_str,
// conversationId:
// tweet.conversationId ??
// tweet.legacy?.conversation_id_str,
// hashtags: tweet.hashtags ?? tweet.legacy?.entities.hashtags,
// mentions:
// tweet.mentions ?? tweet.legacy?.entities.user_mentions,
// photos:
// tweet.photos ??
// tweet.legacy?.entities.media?.filter(
// (media) => media.type === "photo"
// ) ??
// [],
// thread: [],
// urls: tweet.urls ?? tweet.legacy?.entities.urls,
// videos:
// tweet.videos ??
// tweet.legacy?.entities.media?.filter(
// (media) => media.type === "video"
// ) ??
// [],
// };
// // console.log("obj is", obj);
// return obj;
// });
}

async fetchFeedTimeline(count: number): Promise<string> {
async fetchHomeTimeline(count: number): Promise<Tweet[]> {
elizaLogger.debug("fetching home timeline");
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
return homeTimeline
.filter(tweet => tweet.text || tweet.legacy?.full_text)
.sort((a, b) => {
const timestampA = new Date(a.createdAt ?? a.legacy?.created_at).getTime();
const timestampB = new Date(b.createdAt ?? b.legacy?.created_at).getTime();
return timestampB - timestampA;
})
.slice(0, count)
.map(tweet =>
`@${tweet.username || tweet.core?.user_results?.result?.legacy?.screen_name}: ${tweet.text ?? tweet.legacy?.full_text ?? ''}`
)
.join('\n');

elizaLogger.debug(homeTimeline, { depth: Infinity });
const processedTimeline = homeTimeline
.filter((t) => t.__typename !== "TweetWithVisibilityResults") // what's this about?
.map((tweet) => {
//console.log("tweet is", tweet);
const obj = {
id: tweet.id,
name:
tweet.name ??
tweet?.user_results?.result?.legacy.name,
username:
tweet.username ??
tweet.core?.user_results?.result?.legacy.screen_name,
text: tweet.text ?? tweet.legacy?.full_text,
inReplyToStatusId:
tweet.inReplyToStatusId ??
tweet.legacy?.in_reply_to_status_id_str ??
null,
timestamp: new Date(tweet.legacy?.created_at).getTime() / 1000,
createdAt: tweet.createdAt ?? tweet.legacy?.created_at ?? tweet.core?.user_results?.result?.legacy.created_at,
userId: tweet.userId ?? tweet.legacy?.user_id_str,
conversationId:
tweet.conversationId ??
tweet.legacy?.conversation_id_str,
permanentUrl: `https://x.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
hashtags: tweet.hashtags ?? tweet.legacy?.entities.hashtags,
mentions:
tweet.mentions ?? tweet.legacy?.entities.user_mentions,
photos:
tweet.photos ??
tweet.legacy?.entities.media?.filter(
(media) => media.type === "photo"
) ??
[],
thread: tweet.thread || [],
urls: tweet.urls ?? tweet.legacy?.entities.urls,
videos:
tweet.videos ??
tweet.legacy?.entities.media?.filter(
(media) => media.type === "video"
) ??
[],
};
//console.log("obj is", obj);
return obj;
});
//elizaLogger.debug("process homeTimeline", processedTimeline);
return processedTimeline;
}

async fetchTimelineForActions(count: number): Promise<Tweet[]> {
Expand Down
Loading