This commit is contained in:
2025-05-12 15:11:41 +10:00
parent e972714ada
commit 8c7049fa4c
5 changed files with 123 additions and 161 deletions
+28 -11
View File
@@ -42,7 +42,7 @@ LOCK_FILE = "/home/shane/foodie_automator/locks/foodie_automator_reddit.lock"
def signal_handler(sig, frame):
logging.info("Received termination signal, marking script as stopped...")
update_system_activity(SCRIPT_NAME, "stopped") # Added to mark as stopped
update_system_activity(SCRIPT_NAME, "stopped")
if is_posting:
logging.info("Currently posting, will exit after completion.")
else:
@@ -242,7 +242,17 @@ def fetch_reddit_posts():
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT
)
feeds = ['FoodPorn', 'restaurant', 'FoodIndustry', 'food']
feeds = [
"food",
"FoodPorn",
"spicy"
"spicy",
"KoreanFood",
"JapaneseFood",
"DessertPorn",
"ChineseFood",
"IndianFood"
]
articles = []
cutoff_date = datetime.now(timezone.utc) - timedelta(hours=EXPIRATION_HOURS)
@@ -298,7 +308,8 @@ def curate_from_reddit(posted_titles_data, posted_titles, used_images_data, used
posts = fetch_reddit_posts()
if not posts:
logging.info("No Reddit posts available")
return None, None, False
sleep_time = random.randint(1200, 1800) # 2030 minutes
return None, None, sleep_time
attempts = 0
max_attempts = 10
@@ -466,13 +477,16 @@ def curate_from_reddit(posted_titles_data, posted_titles, used_images_data, used
logging.info(f"Saved image '{image_url}' to {USED_IMAGES_FILE}")
logging.info(f"***** SUCCESS: Posted '{post_data['title']}' (ID: {post_id or 'N/A'}) from Reddit *****")
return post_data, category, True
sleep_time = random.randint(1200, 1800) # 2030 minutes
return post_data, category, sleep_time
logging.info("No interesting Reddit post found after attempts")
return None, None, False
sleep_time = random.randint(1200, 1800) # 2030 minutes
return None, None, sleep_time
except Exception as e:
logging.error(f"Unexpected error in curate_from_reddit: {e}", exc_info=True)
return None, None, False
sleep_time = random.randint(1200, 1800) # 2030 minutes
return None, None, sleep_time
def run_reddit_automator():
lock_fd = None
@@ -485,16 +499,19 @@ def run_reddit_automator():
posted_titles = set(entry["title"] for entry in posted_titles_data)
used_images_data = load_json_file(USED_IMAGES_FILE, IMAGE_EXPIRATION_DAYS)
used_images = set(entry["title"] for entry in used_images_data if "title" in entry)
post_data, category, should_continue = curate_from_reddit(posted_titles_data, posted_titles, used_images_data, used_images)
post_data, category, sleep_time = curate_from_reddit(posted_titles_data, posted_titles, used_images_data, used_images)
if not post_data:
logging.info("No postable Reddit article found")
logging.info("Completed Reddit run")
update_system_activity(SCRIPT_NAME, "stopped") # Record stop
return post_data, category, should_continue
logging.info(f"Run completed, sleep_time: {sleep_time} seconds")
return post_data, category, sleep_time
except Exception as e:
logging.error(f"Fatal error in run_reddit_automator: {e}", exc_info=True)
update_system_activity(SCRIPT_NAME, "stopped") # Record stop on error
return None, None, False
sleep_time = random.randint(1200, 1800) # 2030 minutes
logging.info(f"Run completed, sleep_time: {sleep_time} seconds")
return None, None, sleep_time
finally:
if lock_fd:
fcntl.flock(lock_fd, fcntl.LOCK_UN)
@@ -503,5 +520,5 @@ def run_reddit_automator():
if __name__ == "__main__":
setup_logging()
post_data, category, should_continue = run_reddit_automator()
logging.info(f"Run completed, should_continue: {should_continue}")
post_data, category, sleep_time = run_reddit_automator()
logging.info(f"Run completed, sleep_time: {sleep_time} seconds")