Ejemplo n.º 1
0
def get_last_posted_tweet(bot: API) -> Story:
    """
    Gets the last tweet posted and parses it.

    :param bot: Twitter bot to fetch the tweet from.
    :return: Story parsed from the latest tweet posted.
    """
    # Gets its own Twitter ID
    twitter_id = bot.me().id
    # Gets its own last tweet
    last_tweet_list = bot.user_timeline(
        id=twitter_id, tweet_mode="extended", count=1)
    # If there are no valid tweets raises an exception
    if (len(last_tweet_list) == 0):
        raise ValueError("No valid tweet found")
    # Else gets latest tweet
    latest_tweet = last_tweet_list[0]
    # Injects UTC timezone into timestamp
    created_at = latest_tweet.created_at.replace(tzinfo=timezone.utc)
    # Parses tweet to retrieve required fields
    story = Story.from_string(pattern=TWITTER_PATTERN, string=latest_tweet.full_text, created_at=created_at)
    # Returns story
    return story