This Python script is a bot designed to monitor a...
This Python script is a bot designed to monitor a specific Twitter account for new tweets and send notifications about them to Discord webhook channels. Here's an overview of its functionality:
Highlights of the Code:
-
Twitter Integration:
- The script uses the
tweety
library to interact with Twitter, allowing the bot to search for tweets and gather information about a specific user's activity (tweets). - It handles session authentication, loading a previous session from a file or logging in when no session exists (
login
function).
- The script uses the
-
Discord Notifications:
- The bot sends new tweet notifications to specified Discord webhook URLs using the
discord
library. - The tweet information (text, media, links, and type) is extracted and formatted into rich Discord message embeds for better presentation.
- The bot sends new tweet notifications to specified Discord webhook URLs using the
-
Tracking and Persistence:
- It maintains a record of already "marked" tweets (those that have been processed) to avoid duplicate notifications. These are stored in the
marked_tweets
folder as files named after the monitored username. - Configuration details like Discord webhook URLs and a pointer to track the last-used webhook URL are stored in a
configs.json
file for persistence across sessions.
- It maintains a record of already "marked" tweets (those that have been processed) to avoid duplicate notifications. These are stored in the
-
Functionality Summary:
- Authentication: Authenticates with Twitter (either via existing session or login credentials).
- Monitoring: Monitors a specific Twitter account for new tweets using a loop with random delay (between 60–120 seconds for throttling).
- Processing: Processes each new tweet to format its details, resolve URLs, and categorize the type of interaction (e.g., posted, reply, retweet, quote retweet).
- Notifications: Sends a Discord notification for new tweets, attaching any images, text, and metadata for clarity.
- Localization: The script localizes UTC tweet timestamps to Jakarta's time zone (WIB) and translates the day names to Indonesian.
Breakdown of the Code Functions:
-
load_configs
/save_configs
:- Load or save the
configs.json
file storing user configuration (e.g., webhook URLs and their most recent usage index).
- Load or save the
-
get_user_config
:- Retrieves or initializes the configuration for a specified username, prompting the user to enter Discord webhook URLs if none exist.
-
load_marked_tweets
/save_marked_tweets
:- Manages local storage of processed tweet IDs in a file to prevent duplicate notifications.
-
get_tweet_type
/process_tweet
:- Determines the type of a tweet (post, reply, retweet, etc.) based on its attributes, and organizes tweet details into a structured tuple for further processing.
-
format_utc_to_jakarta_local
:- Converts UTC timestamps to Jakarta local time (WIB) and translates English day names to Indonesian.
-
extract_original_links
:- Replaces all shortened Twitter
t.co
URLs with their expanded forms in the tweet's text.
- Replaces all shortened Twitter
-
post_discord_notification
:- Formats and sends a new tweet notification to the configured Discord webhook URLs, using Discord embeds.
-
validate_twitter_account
:- Validates whether the provided username corresponds to an actual Twitter account.
-
get_all_tweets_from_page
:- Fetches tweets from a specified account using
tweety
with an option to retrieve multiple pages.
- Fetches tweets from a specified account using
-
monitor_twitter_account
:- The main monitoring loop. Fetches tweets at regular intervals, compares them with already processed tweets, and triggers Discord notifications for new tweets.
-
login
/main
:- Manages authentication and entry point for monitoring (via
main
). If no session exists, it prompts for login credentials.
- Manages authentication and entry point for monitoring (via
Overall Purpose:
The script is primarily designed to monitor a Twitter account and notify Discord channels (via webhooks) of new tweets. It ensures that:
- Only new tweets are processed and notified.
- The system can handle media attachments and distinguish tweet types.
- Notifications are sent to multiple Discord hooks in a balanced manner (using a round-robin approach).
- It localizes time information and adheres to Jakarta timezone formatting.
This script can be useful for organizations, fans, or community managers who want to broadcast updates from a Twitter account to their Discord communities automatically.