def get_assignments(user=None, state=None): queryset = SocialAssignments.objects if user is not None: queryset = queryset.filter(user=user) if state is not None: queryset = queryset.filter(state=state) queryset = queryset.order_by("-id").values() assignments = [a for a in queryset] assignmentsById = {} tweetIds = [a["social_id"] for a in assignments] for a in assignments: tweetIds.append(a["social_id"]) for tweetId in a["thread_tweets"]: tweetIds.append(tweetId) tweets = {} for tweet in get_tweets_by_ids(tweetIds): tweets[tweet["tweet_id"]] = TweetsSerializer(tweet).data for assignment in assignments: assignmentsById[assignment["id"]] = SocialAssignmentSerializer(assignment).data return {"assignments": assignmentsById, "tweets": tweets}
def fetch_tweets(startIndex, stopIndex, sinceId=None, maxId=None, columnIds=[]): columns = [] tweets = {} for social_column in get_social_columns(SocialPlatformChoice.TWITTER, columnIds): column_tweets = get_tweets_for_column(social_column, sinceId, maxId, startIndex, stopIndex) column_tweet_ids = [] for tweet in column_tweets: tweets[tweet["tweet_id"]] = TweetsSerializer(tweet).data column_tweet_ids.append(tweet["tweet_id"]) columns.append({ "id": social_column.id, "tweet_ids": column_tweet_ids, }) return { "columns": columns, "tweets": tweets, }
def reply_to_tweet(inReplyToTweetId, replyText): try: api = get_tweepy_api_auth() status = api.update_status(status=replyText, in_reply_to_status_id=inReplyToTweetId) reply, created = save_tweet(status._json, source=TweetSource.REPLYING, status=TweetStatus.OK) websockets.send_channel_message("tweets.update_tweets", { "tweets": { reply.tweet_id: TweetsSerializer(reply).data }, }) except tweepy.RateLimitError as e: logger.warning( "Got a RateLimitError from Tweepy while sending a reply to {}". format(inReplyToTweetId)) raise e except tweepy.TweepError as e: logger.warning( "Got a TweepError {} from Tweepy while sending a reply to {}". format(e.api_code, inReplyToTweetId)) raise e
def resolve_tweet_thread_for_parent(parent): replies = get_status_replies(parent) relationships = build_relationships(replies) # Collect up Django Tweet objects to send back up tweets = {} tweets[parent["id_str"]] = TweetsSerializer( get_tweet_from_db(parent["id_str"])).data # Replies are already saved by local caching in the get_status_replies() call stack for tweet in Tweets.objects.filter( tweet_id__in=[t["id_str"] for t in replies]).all(): tweets[tweet.tweet_id] = TweetsSerializer(tweet).data return tweets[parent["id_str"]], tweets, relationships
def retweet_tweet(tweetId): def ws_send_updated_tweet(tweet): websockets.send_channel_message("tweets.update_tweets", { "tweets": { tweet.tweet_id: TweetsSerializer(tweet).data }, }) tweet = Tweets.objects.get(tweet_id=tweetId) # The client shouldn't be able to retweet a tweet they've already retweeted. # Fail quietly and send out new state to all connected clients. if tweet.data["retweeted"] is True: ws_send_updated_tweet(tweet) return try: api = get_tweepy_api_auth() status = api.retweet(tweetId) tweet.data = status._json["retweeted_status"] tweet.save() retweet, created = save_tweet(status._json, source=TweetSource.RETWEETING, status=TweetStatus.OK) websockets.send_channel_message( "tweets.update_tweets", { "tweets": { tweet.tweet_id: TweetsSerializer(tweet).data, retweet.tweet_id: TweetsSerializer(retweet).data }, }) except tweepy.TweepError as e: if e.api_code == 327: # The tweet was already retweeted somewhere else (e.g. another Twitter client). Update local state tweet and respond as if we succeeded. tweet.data["retweeted"] = True tweet.save() ws_send_updated_tweet(tweet) else: # Uh oh, some other error code was returned # NB: tweepy.api can return certain errors via retry_errors raise e
def notify_of_saved_tweets(tweets): if len(tweets) > 0: response = { "tweets": {}, } for tweet in tweets: response["tweets"][tweet.tweet_id] = TweetsSerializer(tweet).data websockets.send_channel_message("tweets.new_tweets", response)
def fetch_tweets_for_columns(columnPositions, columnIds=[]): columns = [] tweets = {} for social_column in get_social_columns(SocialPlatformChoice.TWITTER, columnIds): hasColumnPosition = columnPositions is not None and str( social_column.id) in columnPositions if hasColumnPosition is True: sinceId = int(columnPositions[str( social_column.id)]["stopTweet"]) - 1 column_tweets = get_tweets_for_column_by_tweet_ids( social_column, sinceId) else: column_tweets = get_tweets_for_column(social_column, startIndex=0, stopIndex=20) column_tweet_ids = [] column_tweet_ids_buffered = [] for tweet in column_tweets: tweets[tweet["tweet_id"]] = TweetsSerializer(tweet).data if hasColumnPosition is True and int(tweet["tweet_id"]) > int( columnPositions[str(social_column.id)]["firstTweet"]): column_tweet_ids_buffered.append(tweet["tweet_id"]) else: column_tweet_ids.append(tweet["tweet_id"]) columns.append({ "id": social_column.id, "tweet_ids": column_tweet_ids, "tweet_ids_buffered": column_tweet_ids_buffered, }) return { "columns": columns, "tweets": tweets, }
def ws_send_updated_tweet(tweet): websockets.send_channel_message("tweets.update_tweets", { "tweets": { tweet.tweet_id: TweetsSerializer(tweet).data }, })