Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- from datetime import datetime, timedelta
- MIN_DELAY = 10
- def schedule_posts_for_day():
- """Schedule posts for the day based on available images."""
- now = datetime.now()
- start_time = datetime(now.year, now.month, now.day, 8, 0) # 8 AM
- end_time = datetime(now.year, now.month, now.day, 22, 0) # 10 PM
- # If it's already past 8 AM, start from now
- if now > start_time:
- start_time = now
- # If it's already past 10 PM, no posts for today
- if now > end_time:
- return {}
- total_seconds = (end_time - start_time).total_seconds()
- posted_hashes = load_posted_hashes()
- # Schedule for each channel
- schedule = {}
- for channel in WEBHOOKS.keys():
- # Get all available images first
- available_images = get_available_images(channel, posted_hashes)
- post_count = len(available_images)
- if post_count > 0:
- # Create random posting times for all available images
- post_times = sorted([start_time + timedelta(seconds=random.randint(0, int(total_seconds)))
- for _ in range(post_count)])
- schedule[channel] = post_times
- logger.info(f"Scheduled {post_count} posts for {channel}")
- else:
- schedule[channel] = []
- logger.info(f"No images available to post for {channel}")
- return schedule
- def main():
- """Main function to run the bot."""
- setup_folders()
- posted_hashes = load_posted_hashes()
- while True:
- # Get the current date
- today = datetime.now().date()
- # Schedule posts for today
- schedule = schedule_posts_for_day()
- # Display scheduled posting times
- logger.info("=== Today's Posting Schedule ===")
- for channel, post_times in schedule.items():
- if post_times:
- logger.info(f"Channel: {channel}")
- for i, post_time in enumerate(post_times, 1):
- logger.info(f" Image {i}: {post_time.strftime('%I:%M:%S %p')}")
- else:
- logger.info(f"Channel: {channel} - No images scheduled")
- logger.info("===============================")
- # If there are no scheduled posts, wait until tomorrow
- if not any(schedule.values()):
- tomorrow = datetime(today.year, today.month, today.day) + timedelta(days=1, hours=8)
- time_until_tomorrow = (tomorrow - datetime.now()).total_seconds()
- if time_until_tomorrow > 0:
- time.sleep(time_until_tomorrow)
- continue
- # Process each channel
- for channel, webhook_url in WEBHOOKS.items():
- post_times = schedule.get(channel, [])
- for post_time in post_times:
- # Wait until it's time to post
- current_time = datetime.now()
- if current_time < post_time:
- sleep_time = (post_time - current_time).total_seconds()
- time.sleep(sleep_time)
- # Check if we're still on the same day
- if datetime.now().date() != today:
- break
- # Get available images
- available_images = get_available_images(channel, posted_hashes)
- if available_images:
- # Select a random image
- image_path = random.choice(available_images)
- # Post the image
- success = post_to_discord(webhook_url, image_path)
- if success:
- # Log the hash to prevent duplicates
- file_hash = get_file_hash(image_path)
- save_hash_to_log(file_hash)
- posted_hashes.add(file_hash)
- # Added minimum delay between posts
- time.sleep(MIN_DELAY)
- else:
- # No images available, wait and check again
- time.sleep(RETRY_INTERVAL)
- # Check again after waiting
- available_images = get_available_images(channel, posted_hashes)
- if available_images:
- image_path = random.choice(available_images)
- success = post_to_discord(webhook_url, image_path)
- if success:
- file_hash = get_file_hash(image_path)
- save_hash_to_log(file_hash)
- posted_hashes.add(file_hash)
- # Added minimum delay between posts
- time.sleep(MIN_DELAY)
- # Wait until the next day
- tomorrow = datetime(today.year, today.month, today.day) + timedelta(days=1, hours=8)
- time_until_tomorrow = (tomorrow - datetime.now()).total_seconds()
- if time_until_tomorrow > 0:
- time.sleep(time_until_tomorrow)
- if __name__ == "__main__":
- try:
- main()
- except KeyboardInterrupt:
- pass
- except Exception as e:
- # Wait a bit before restarting
- time.sleep(10)
- main()
Advertisement
Add Comment
Please, Sign In to add comment