Guest User

Untitled

a guest
Mar 6th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. import random
  2. import time
  3. from datetime import datetime, timedelta
  4.  
  5. MIN_DELAY = 10
  6.  
  7. def schedule_posts_for_day():
  8. """Schedule posts for the day based on available images."""
  9. now = datetime.now()
  10. start_time = datetime(now.year, now.month, now.day, 8, 0) # 8 AM
  11. end_time = datetime(now.year, now.month, now.day, 22, 0) # 10 PM
  12.  
  13. # If it's already past 8 AM, start from now
  14. if now > start_time:
  15. start_time = now
  16.  
  17. # If it's already past 10 PM, no posts for today
  18. if now > end_time:
  19. return {}
  20.  
  21. total_seconds = (end_time - start_time).total_seconds()
  22. posted_hashes = load_posted_hashes()
  23.  
  24. # Schedule for each channel
  25. schedule = {}
  26. for channel in WEBHOOKS.keys():
  27. # Get all available images first
  28. available_images = get_available_images(channel, posted_hashes)
  29. post_count = len(available_images)
  30.  
  31. if post_count > 0:
  32. # Create random posting times for all available images
  33. post_times = sorted([start_time + timedelta(seconds=random.randint(0, int(total_seconds)))
  34. for _ in range(post_count)])
  35. schedule[channel] = post_times
  36. logger.info(f"Scheduled {post_count} posts for {channel}")
  37. else:
  38. schedule[channel] = []
  39. logger.info(f"No images available to post for {channel}")
  40.  
  41. return schedule
  42.  
  43. def main():
  44. """Main function to run the bot."""
  45. setup_folders()
  46. posted_hashes = load_posted_hashes()
  47.  
  48. while True:
  49. # Get the current date
  50. today = datetime.now().date()
  51.  
  52. # Schedule posts for today
  53. schedule = schedule_posts_for_day()
  54.  
  55. # Display scheduled posting times
  56. logger.info("=== Today's Posting Schedule ===")
  57. for channel, post_times in schedule.items():
  58. if post_times:
  59. logger.info(f"Channel: {channel}")
  60. for i, post_time in enumerate(post_times, 1):
  61. logger.info(f" Image {i}: {post_time.strftime('%I:%M:%S %p')}")
  62. else:
  63. logger.info(f"Channel: {channel} - No images scheduled")
  64. logger.info("===============================")
  65.  
  66. # If there are no scheduled posts, wait until tomorrow
  67. if not any(schedule.values()):
  68. tomorrow = datetime(today.year, today.month, today.day) + timedelta(days=1, hours=8)
  69. time_until_tomorrow = (tomorrow - datetime.now()).total_seconds()
  70. if time_until_tomorrow > 0:
  71. time.sleep(time_until_tomorrow)
  72. continue
  73.  
  74. # Process each channel
  75. for channel, webhook_url in WEBHOOKS.items():
  76. post_times = schedule.get(channel, [])
  77.  
  78. for post_time in post_times:
  79. # Wait until it's time to post
  80. current_time = datetime.now()
  81. if current_time < post_time:
  82. sleep_time = (post_time - current_time).total_seconds()
  83. time.sleep(sleep_time)
  84.  
  85. # Check if we're still on the same day
  86. if datetime.now().date() != today:
  87. break
  88.  
  89. # Get available images
  90. available_images = get_available_images(channel, posted_hashes)
  91.  
  92. if available_images:
  93. # Select a random image
  94. image_path = random.choice(available_images)
  95.  
  96. # Post the image
  97. success = post_to_discord(webhook_url, image_path)
  98.  
  99. if success:
  100. # Log the hash to prevent duplicates
  101. file_hash = get_file_hash(image_path)
  102. save_hash_to_log(file_hash)
  103. posted_hashes.add(file_hash)
  104.  
  105. # Added minimum delay between posts
  106. time.sleep(MIN_DELAY)
  107. else:
  108. # No images available, wait and check again
  109. time.sleep(RETRY_INTERVAL)
  110.  
  111. # Check again after waiting
  112. available_images = get_available_images(channel, posted_hashes)
  113. if available_images:
  114. image_path = random.choice(available_images)
  115. success = post_to_discord(webhook_url, image_path)
  116.  
  117. if success:
  118. file_hash = get_file_hash(image_path)
  119. save_hash_to_log(file_hash)
  120. posted_hashes.add(file_hash)
  121.  
  122. # Added minimum delay between posts
  123. time.sleep(MIN_DELAY)
  124.  
  125. # Wait until the next day
  126. tomorrow = datetime(today.year, today.month, today.day) + timedelta(days=1, hours=8)
  127. time_until_tomorrow = (tomorrow - datetime.now()).total_seconds()
  128. if time_until_tomorrow > 0:
  129. time.sleep(time_until_tomorrow)
  130.  
  131. if __name__ == "__main__":
  132. try:
  133. main()
  134. except KeyboardInterrupt:
  135. pass
  136. except Exception as e:
  137. # Wait a bit before restarting
  138. time.sleep(10)
  139. main()
  140.  
Advertisement
Add Comment
Please, Sign In to add comment