|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import requests |
| 5 | +import time |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +# Try to load from .env file for local development |
| 9 | +current_dir = pathlib.Path(__file__).parent.absolute() |
| 10 | +env_path = current_dir.parent.parent / '.env' |
| 11 | +if env_path.exists(): |
| 12 | + load_dotenv(dotenv_path=env_path) |
| 13 | + |
| 14 | +def year_progress_bar(): |
| 15 | + """Generate year progress bar and message for Threads post.""" |
| 16 | + today = datetime.datetime.utcnow() |
| 17 | + year_start = datetime.datetime(today.year, 1, 1) |
| 18 | + year_end = datetime.datetime(today.year + 1, 1, 1) |
| 19 | + |
| 20 | + total_days = (year_end - year_start).days |
| 21 | + days_passed = (today - year_start).days |
| 22 | + |
| 23 | + percent_passed = (days_passed / total_days) * 100 |
| 24 | + |
| 25 | + bar_length = 20 |
| 26 | + filled_length = int(bar_length * days_passed // total_days) |
| 27 | + bar = '█' * filled_length + '·' * (bar_length - filled_length) |
| 28 | + |
| 29 | + main_post = ( |
| 30 | + f"{percent_passed:.2f}% of {today.year} has passed.\n" |
| 31 | + f"[{bar}]" |
| 32 | + ) |
| 33 | + |
| 34 | + promo_post = ( |
| 35 | + f"Keep track of your time ⏳\n" |
| 36 | + f"https://theyearprogress.app" |
| 37 | + ) |
| 38 | + |
| 39 | + return main_post, promo_post |
| 40 | + |
| 41 | +def create_threads_media_container(user_id, access_token, text, media_type="TEXT"): |
| 42 | + """Create a Threads media container.""" |
| 43 | + url = f"https://graph.threads.net/v1.0/{user_id}/threads" |
| 44 | + |
| 45 | + params = { |
| 46 | + "media_type": media_type, |
| 47 | + "text": text, |
| 48 | + "access_token": access_token |
| 49 | + } |
| 50 | + |
| 51 | + print(f"Creating Threads media container with text: {text}") |
| 52 | + response = requests.post(url, params=params) |
| 53 | + |
| 54 | + if response.status_code != 200: |
| 55 | + print(f"Error creating Threads media container: {response.text}") |
| 56 | + raise Exception(f"Failed to create Threads media container: {response.text}") |
| 57 | + |
| 58 | + return response.json()["id"] |
| 59 | + |
| 60 | +def publish_threads_media(user_id, access_token, creation_id): |
| 61 | + """Publish a Threads media container.""" |
| 62 | + url = f"https://graph.threads.net/v1.0/{user_id}/threads_publish" |
| 63 | + |
| 64 | + params = { |
| 65 | + "creation_id": creation_id, |
| 66 | + "access_token": access_token |
| 67 | + } |
| 68 | + |
| 69 | + print(f"Publishing Threads media container with ID: {creation_id}") |
| 70 | + response = requests.post(url, params=params) |
| 71 | + |
| 72 | + if response.status_code != 200: |
| 73 | + print(f"Error publishing Threads media: {response.text}") |
| 74 | + raise Exception(f"Failed to publish Threads media: {response.text}") |
| 75 | + |
| 76 | + return response.json()["id"] |
| 77 | + |
| 78 | +def post_to_threads(main_message, promo_message): |
| 79 | + """Post main message and promo reply to Threads.""" |
| 80 | + try: |
| 81 | + user_id = os.getenv('THREADS_USER_ID') |
| 82 | + access_token = os.getenv('THREADS_ACCESS_TOKEN') |
| 83 | + |
| 84 | + print(f"Attempting to post to Threads with credentials:") |
| 85 | + print(f"THREADS_USER_ID: {user_id[:5] if user_id else 'Not set'}...") |
| 86 | + print(f"THREADS_ACCESS_TOKEN: {access_token[:5] if access_token else 'Not set'}...") |
| 87 | + |
| 88 | + # Create and publish main post |
| 89 | + main_container_id = create_threads_media_container(user_id, access_token, main_message) |
| 90 | + print(f"Main post container created with ID: {main_container_id}") |
| 91 | + |
| 92 | + # Wait for processing (recommended by Meta) |
| 93 | + print("Waiting 30 seconds for server processing...") |
| 94 | + time.sleep(30) |
| 95 | + |
| 96 | + # Publish main post |
| 97 | + main_post_id = publish_threads_media(user_id, access_token, main_container_id) |
| 98 | + print(f"Main post published successfully! Post ID: {main_post_id}") |
| 99 | + |
| 100 | + # Create and publish promo reply |
| 101 | + # For replies, we need to use the reply_control_web_id parameter |
| 102 | + # This is not directly supported in the API yet, so we'll just post a separate message |
| 103 | + promo_container_id = create_threads_media_container(user_id, access_token, promo_message) |
| 104 | + print(f"Promo post container created with ID: {promo_container_id}") |
| 105 | + |
| 106 | + # Wait for processing |
| 107 | + print("Waiting 30 seconds for server processing...") |
| 108 | + time.sleep(30) |
| 109 | + |
| 110 | + # Publish promo post |
| 111 | + promo_post_id = publish_threads_media(user_id, access_token, promo_container_id) |
| 112 | + print(f"Promo post published successfully! Post ID: {promo_post_id}") |
| 113 | + |
| 114 | + return main_post_id, promo_post_id |
| 115 | + except Exception as e: |
| 116 | + print(f"Error posting to Threads: {str(e)}") |
| 117 | + print(f"Error type: {type(e).__name__}") |
| 118 | + if hasattr(e, 'response') and hasattr(e.response, 'text'): |
| 119 | + print(f"Response text: {e.response.text}") |
| 120 | + raise |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main_message, promo_message = year_progress_bar() |
| 124 | + print(f"Generated main message: {main_message}") |
| 125 | + print(f"Generated promo message: {promo_message}") |
| 126 | + post_to_threads(main_message, promo_message) |
0 commit comments