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

feat: get liked tweets from a specific time #13

Merged
merged 8 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 35 additions & 0 deletions bot/services/liked_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ def get_liked_tweets(user_id: str) -> list[tweepy.Tweet]:
return all_tweets


def get_liked_tweets_since(user_id: str, since: int | None = None):
all_tweets: list[tweepy.Tweet] = []
next_token = None

for _ in count(1):
tweets = retry_function_if_fail(
TwitterClient.client.get_liked_tweets,
id=user_id,
tweet_fields=FetchConfigs.tweet_fields,
max_results=5,
pagination_token=next_token,
)
tweets_list = tweets.data
tweets_meta = tweets.meta

tweets_list = tweets_list if tweets_list is not None else []
all_tweets += tweets_list

if since: # check if there is a tweet that was created before the `since`
tweets_time = list(
map(lambda tweet: tweet.created_at.timestamp() * 1000, tweets_list)
)
times_before_since = list(filter(lambda time: time < since, tweets_time))
if len(times_before_since) > 1:
return all_tweets
scientiststwin marked this conversation as resolved.
Show resolved Hide resolved

if "next_token" not in tweets_meta:
# when we don't have "next_token" in meta object, there is no more data
break
else:
next_token = tweets_meta["next_token"]

return all_tweets


def get_likers_of_tweet(tweet_id: str) -> list[tweepy.User]:
all_liker_users: list[tweepy.User] = []
next_token = None
Expand Down
7 changes: 5 additions & 2 deletions bot/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
save_user_likes_neo4j,
save_user_profile_neo4j,
)
from bot.services.liked_tweet import get_liked_tweets, get_likers_of_tweet
from bot.services.liked_tweet import get_liked_tweets_since, get_likers_of_tweet
from bot.services.quote_tweet import get_quotes_of_tweet
from bot.services.reply_tweet import (
get_all_replies_of_tweet,
Expand All @@ -19,6 +19,7 @@
from bot.services.retweet_tweet import get_retweets_of_tweet
from bot.services.user_info import get_twitter_user, get_twitter_users
from bot.services.user_tweet import get_mentioned_tweets_by_username, get_user_tweets
from bot.utils.get_epoch import get_x_days_ago_UTC_timestamp


def extract_and_save_tweets(
Expand Down Expand Up @@ -170,6 +171,8 @@ def extract_and_save_liker_users(user_id: str):


def extract_and_save_liked_tweets(user_id: str):
liked_tweets = get_liked_tweets(user_id=user_id)
liked_tweets = get_liked_tweets_since(
user_id=user_id, since=get_x_days_ago_UTC_timestamp(7)
)
save_user_likes_neo4j(user_id=user_id, tweets_liked=liked_tweets)
save_tweets_in_neo4j(liked_tweets)