Skip to content

Commit

Permalink
Merge pull request #1021 from odilitime/twitter-search-switch
Browse files Browse the repository at this point in the history
chore: Twitter fetchHomeTimeline rework
  • Loading branch information
odilitime authored Dec 12, 2024
2 parents ffd5e0a + bbbbad7 commit 3f8317b
Showing 1 changed file with 52 additions and 59 deletions.
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

0 comments on commit 3f8317b

Please sign in to comment.