Created
November 30, 2025 09:48
-
-
Save osc2nuke/302a1433074f7407978e0e6e376b69cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tweepy | |
| import feedparser | |
| import textwrap | |
| # X API credentials (replace with your own) | |
| API_KEY = "your_api_key" | |
| API_SECRET = "your_api_secret" | |
| ACCESS_TOKEN = "your_access_token" | |
| ACCESS_TOKEN_SECRET = "your_access_token_secret" | |
| CLIENT_ID = "your_client_id" | |
| CLIENT_SECRET = "your_client_secret" | |
| # NOS RSS feed URL | |
| NOS_RSS_URL = "https://feeds.nos.nl/nosnieuwsalgemeen" | |
| def setup_tweepy_client(): | |
| """Set up Tweepy client for X API v2.""" | |
| try: | |
| client = tweepy.Client( | |
| consumer_key=API_KEY, | |
| consumer_secret=API_SECRET, | |
| access_token=ACCESS_TOKEN, | |
| access_token_secret=ACCESS_TOKEN_SECRET | |
| ) | |
| return client | |
| except Exception as e: | |
| print(f"Error setting up Tweepy client: {e}") | |
| return None | |
| def fetch_nos_rss(): | |
| """Fetch the latest news from NOS RSS feed.""" | |
| try: | |
| feed = feedparser.parse(NOS_RSS_URL) | |
| if feed.entries: | |
| latest_article = feed.entries[0] # Get the most recent article | |
| return { | |
| "title": latest_article.get("title", "No title"), | |
| "summary": latest_article.get("summary", "No summary"), | |
| "link": latest_article.get("link", "") | |
| } | |
| else: | |
| print("No articles found in the RSS feed.") | |
| return None | |
| except Exception as e: | |
| print(f"Error fetching RSS feed: {e}") | |
| return None | |
| def create_post(article): | |
| """Create a post text from the article, fitting within 280 characters.""" | |
| title = article["title"] | |
| link = article["link"] | |
| # Shorten summary to fit within character limit | |
| max_text_length = 280 - len(link) - 10 # Reserve space for link and padding | |
| summary = textwrap.shorten(article["summary"], width=max_text_length, placeholder="...") | |
| post = f"{title}\n{summary}\n{link}" | |
| if len(post) > 280: | |
| post = textwrap.shorten(post, width=280, placeholder="...") | |
| return post | |
| def post_to_x(client, post): | |
| """Post the message to X.com.""" | |
| try: | |
| client.create_tweet(text=post) | |
| print("Successfully posted to X!") | |
| print(f"Post content: {post}") | |
| except Exception as e: | |
| print(f"Error posting to X: {e}") | |
| def main(): | |
| # Set up Tweepy client | |
| client = setup_tweepy_client() | |
| if not client: | |
| return | |
| # Fetch NOS RSS feed | |
| article = fetch_nos_rss() | |
| if not article: | |
| return | |
| # Create and post to X | |
| post = create_post(article) | |
| post_to_x(client, post) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment