Created
October 8, 2025 02:51
-
-
Save WomB0ComB0/a3c9ea9185bda8b60e4d6891e903e3b0 to your computer and use it in GitHub Desktop.
post holidays (I don't keep track of holidays)
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 requests | |
| from datetime import datetime, timedelta | |
| import time | |
| import os | |
| # ===== CONFIGURATION ===== | |
| # X (Twitter) API credentials - get from https://developer.twitter.com/ | |
| TWITTER_API_KEY = os.getenv('TWITTER_API_KEY', 'your_api_key_here') | |
| TWITTER_API_SECRET = os.getenv('TWITTER_API_SECRET', 'your_api_secret_here') | |
| TWITTER_ACCESS_TOKEN = os.getenv('TWITTER_ACCESS_TOKEN', 'your_access_token_here') | |
| TWITTER_ACCESS_SECRET = os.getenv('TWITTER_ACCESS_SECRET', 'your_access_secret_here') | |
| TWITTER_BEARER_TOKEN = os.getenv('TWITTER_BEARER_TOKEN', 'your_bearer_token_here') | |
| # Calendarific API key - get free key from https://calendarific.com/ | |
| CALENDARIFIC_API_KEY = os.getenv('CALENDARIFIC_API_KEY', 'your_calendarific_key_here') | |
| class HolidayPoster: | |
| def __init__(self): | |
| # Initialize Twitter API v2 client | |
| self.client = tweepy.Client( | |
| bearer_token=TWITTER_BEARER_TOKEN, | |
| consumer_key=TWITTER_API_KEY, | |
| consumer_secret=TWITTER_API_SECRET, | |
| access_token=TWITTER_ACCESS_TOKEN, | |
| access_token_secret=TWITTER_ACCESS_SECRET | |
| ) | |
| def get_holidays(self, country='US', year=None, month=None, day=None): | |
| """Fetch holidays using Calendarific API""" | |
| if year is None: | |
| year = datetime.now().year | |
| if month is None: | |
| month = datetime.now().month | |
| if day is None: | |
| day = datetime.now().day | |
| url = 'https://calendarific.com/api/v2/holidays' | |
| params = { | |
| 'api_key': CALENDARIFIC_API_KEY, | |
| 'country': country, | |
| 'year': year, | |
| 'month': month, | |
| 'day': day | |
| } | |
| try: | |
| response = requests.get(url, params=params) | |
| response.raise_for_status() | |
| data = response.json() | |
| if data['meta']['code'] == 200: | |
| return data['response']['holidays'] | |
| return [] | |
| except Exception as e: | |
| print(f"Error fetching holidays: {e}") | |
| return [] | |
| def format_holiday_tweet(self, holidays): | |
| """Format holidays into a tweet""" | |
| if not holidays: | |
| return None | |
| date_str = datetime.now().strftime("%B %d, %Y") | |
| # Categorize holidays | |
| international = [] | |
| religious = [] | |
| national = [] | |
| observances = [] | |
| for holiday in holidays: | |
| name = holiday['name'] | |
| types = holiday.get('type', []) | |
| if 'National holiday' in types: | |
| national.append(name) | |
| elif any(t in types for t in ['Christian', 'Muslim', 'Jewish', 'Hindu', 'Buddhist']): | |
| religious.append(name) | |
| elif 'Observance' in types: | |
| observances.append(name) | |
| else: | |
| international.append(name) | |
| # Build tweet | |
| tweet = f"ποΈ Today's Holidays - {date_str}\n\n" | |
| if national: | |
| tweet += "ποΈ National:\n" + "\n".join(f"β’ {h}" for h in national[:2]) + "\n\n" | |
| if religious: | |
| tweet += "ποΈ Religious:\n" + "\n".join(f"β’ {h}" for h in religious[:2]) + "\n\n" | |
| if international: | |
| tweet += "π International:\n" + "\n".join(f"β’ {h}" for h in international[:2]) + "\n\n" | |
| if observances: | |
| tweet += "π Observances:\n" + "\n".join(f"β’ {h}" for h in observances[:2]) + "\n\n" | |
| # Truncate if too long (Twitter limit is 280 chars) | |
| if len(tweet) > 270: | |
| tweet = tweet[:267] + "..." | |
| tweet += "\n#Holidays #TodayInHistory" | |
| return tweet | |
| def post_tweet(self, text): | |
| """Post a tweet to X""" | |
| try: | |
| response = self.client.create_tweet(text=text) | |
| print(f"β Tweet posted successfully! Tweet ID: {response.data['id']}") | |
| return True | |
| except Exception as e: | |
| print(f"β Error posting tweet: {e}") | |
| return False | |
| def run_daily(self): | |
| """Main function to fetch and post holidays""" | |
| print(f"π Fetching holidays for {datetime.now().strftime('%Y-%m-%d')}...") | |
| # Fetch holidays for multiple countries to get diverse holidays | |
| all_holidays = [] | |
| countries = ['US', 'GB', 'IN', 'FR', 'JP'] # Mix of countries | |
| for country in countries: | |
| holidays = self.get_holidays(country=country) | |
| all_holidays.extend(holidays) | |
| # Remove duplicates based on name | |
| unique_holidays = [] | |
| seen_names = set() | |
| for h in all_holidays: | |
| if h['name'] not in seen_names: | |
| unique_holidays.append(h) | |
| seen_names.add(h['name']) | |
| print(f"π Found {len(unique_holidays)} unique holidays") | |
| if unique_holidays: | |
| tweet_text = self.format_holiday_tweet(unique_holidays) | |
| if tweet_text: | |
| print(f"\nπ Tweet preview:\n{tweet_text}\n") | |
| self.post_tweet(tweet_text) | |
| else: | |
| print("βΉοΈ No holidays found for today") | |
| def main(): | |
| """Run the holiday poster""" | |
| poster = HolidayPoster() | |
| # Run once | |
| poster.run_daily() | |
| # Optional: Schedule to run daily | |
| # Uncomment below to run every 24 hours | |
| # while True: | |
| # poster.run_daily() | |
| # print(f"β° Sleeping for 24 hours...") | |
| # time.sleep(86400) # Sleep for 24 hours | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment