balanced author selection logic

main
Shane 7 months ago
parent 4da83f1d4b
commit f6ab7e78d3
  1. 38
      foodie_automator_google.py
  2. 38
      foodie_automator_reddit.py
  3. 52
      foodie_automator_rss.py
  4. 50
      foodie_utils.py

@ -314,14 +314,36 @@ def curate_from_google_trends(geo_list=['US']):
final_summary = insert_link_naturally(final_summary, source_name, link) final_summary = insert_link_naturally(final_summary, source_name, link)
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic) # Balanced author selection
if not post_data: x_post_counts = load_json_file('/home/shane/foodie_automator/x_post_counts.json', expiration_hours=24*30)
attempts += 1 monthly_counts = {entry['username']: entry['monthly_count'] for entry in x_post_counts}
continue low_post_authors = [u for u, c in monthly_counts.items() if c < 3]
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic) if low_post_authors:
if not image_url: author_username = random.choice(low_post_authors)
image_url, image_source, uploader, page_url = get_image(image_query) author = next(a for a in AUTHORS if a['username'] == author_username)
logging.info(f"Prioritizing low-post author: {author_username}")
post_data = {
"title": generate_title_from_summary(final_summary),
"content": final_summary,
"status": "publish",
"author": author_username,
"categories": [generate_category_from_summary(final_summary)]
}
category = post_data["categories"][0]
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic)
if not image_url:
image_url, image_source, uploader, page_url = get_image(image_query)
if not image_url:
logging.warning(f"All image uploads failed for '{title}' - posting without image")
image_source = None
uploader = None
page_url = None
else:
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic)
if not post_data:
attempts += 1
continue
hook = get_dynamic_hook(post_data["title"]).strip() hook = get_dynamic_hook(post_data["title"]).strip()

@ -346,14 +346,36 @@ def curate_from_reddit():
final_summary = insert_link_naturally(final_summary, source_name, link) final_summary = insert_link_naturally(final_summary, source_name, link)
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic) # Balanced author selection
if not post_data: x_post_counts = load_json_file('/home/shane/foodie_automator/x_post_counts.json', expiration_hours=24*30)
attempts += 1 monthly_counts = {entry['username']: entry['monthly_count'] for entry in x_post_counts}
continue low_post_authors = [u for u, c in monthly_counts.items() if c < 3]
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic) if low_post_authors:
if not image_url: author_username = random.choice(low_post_authors)
image_url, image_source, uploader, page_url = get_image(image_query) author = next(a for a in AUTHORS if a['username'] == author_username)
logging.info(f"Prioritizing low-post author: {author_username}")
post_data = {
"title": generate_title_from_summary(final_summary),
"content": final_summary,
"status": "publish",
"author": author_username,
"categories": [generate_category_from_summary(final_summary)]
}
category = post_data["categories"][0]
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic)
if not image_url:
image_url, image_source, uploader, page_url = get_image(image_query)
if not image_url:
logging.warning(f"All image uploads failed for '{title}' - posting without image")
image_source = None
uploader = None
page_url = None
else:
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic)
if not post_data:
attempts += 1
continue
hook = get_dynamic_hook(post_data["title"]).strip() hook = get_dynamic_hook(post_data["title"]).strip()

@ -335,25 +335,43 @@ def curate_from_rss():
final_summary = insert_link_naturally(final_summary, source_name, link) final_summary = insert_link_naturally(final_summary, source_name, link)
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic) # Insert balanced author selection logic here
if not post_data: x_post_counts = load_json_file('/home/shane/foodie_automator/x_post_counts.json', expiration_hours=24*30)
print(f"Post data preparation failed for '{title}'") monthly_counts = {entry['username']: entry['monthly_count'] for entry in x_post_counts}
logging.info(f"Post data preparation failed for '{title}'") low_post_authors = [u for u, c in monthly_counts.items() if c < 3] # Authors with <3 posts
attempts += 1
continue if low_post_authors:
author_username = random.choice(low_post_authors)
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic) author = next(a for a in AUTHORS if a['username'] == author_username)
if not image_url: logging.info(f"Prioritizing low-post author: {author_username}")
print(f"Flickr image fetch failed for '{image_query}', trying fallback") post_data = {
logging.warning(f"Flickr image fetch failed for '{image_query}', trying fallback") "title": generate_title_from_summary(final_summary),
image_url, image_source, uploader, page_url = get_image(image_query) "content": final_summary,
"status": "publish",
"author": author_username,
"categories": [generate_category_from_summary(final_summary)]
}
category = post_data["categories"][0]
image_url, image_source, uploader, page_url = get_flickr_image(image_query, relevance_keywords, main_topic)
if not image_url: if not image_url:
print(f"All image uploads failed for '{title}' - posting without image") print(f"Flickr image fetch failed for '{image_query}', trying fallback")
logging.warning(f"All image uploads failed for '{title}' - posting without image") logging.warning(f"Flickr image fetch failed for '{image_query}', trying fallback")
image_source = None image_url, image_source, uploader, page_url = get_image(image_query)
uploader = None if not image_url:
page_url = None print(f"All image uploads failed for '{title}' - posting without image")
logging.warning(f"All image uploads failed for '{title}' - posting without image")
image_source = None
uploader = None
page_url = None
else:
post_data, author, category, image_url, image_source, uploader, page_url = prepare_post_data(final_summary, title, main_topic)
if not post_data:
print(f"Post data preparation failed for '{title}'")
logging.info(f"Post data preparation failed for '{title}'")
attempts += 1
continue
# ... (rest of the function: image fetching, posting logic, etc.)
hook = get_dynamic_hook(post_data["title"]).strip() hook = get_dynamic_hook(post_data["title"]).strip()
share_prompt = get_viral_share_prompt(post_data["title"], final_summary) share_prompt = get_viral_share_prompt(post_data["title"], final_summary)

@ -1137,29 +1137,37 @@ def get_flickr_image(search_query, relevance_keywords, main_topic):
logging.warning(f"No valid Flickr image found for query '{search_query}' after all attempts.") logging.warning(f"No valid Flickr image found for query '{search_query}' after all attempts.")
return None, None, None, None return None, None, None, None
def select_best_author(summary): def select_best_author(content, interest_score):
try: try:
response = client.chat.completions.create( x_post_counts = load_json_file('/home/shane/foodie_automator/x_post_counts.json', expiration_hours=24*30)
model=LIGHT_TASK_MODEL, monthly_counts = {entry['username']: entry['monthly_count'] for entry in x_post_counts}
messages=[
{"role": "system", "content": ( best_score = -1
"Based on this restaurant/food industry trend summary, pick the most suitable author from: " best_author = None
"owenjohnson, javiermorales, aishapatel, trangnguyen, keishareid, lilamoreau. " for author, persona in PERSONA_CONFIGS.items():
"Consider their expertise: owenjohnson (global dining trends), javiermorales (food critique), " prompt = persona["prompt"]
"aishapatel (emerging food trends), trangnguyen (cultural dining), keishareid (soul food heritage), " current_score = interest_score
"lilamoreau (global street food). Return only the username." if "trend" in prompt.lower():
)}, current_score += 2
{"role": "user", "content": summary} elif "recipe" in prompt.lower():
], current_score += 1
max_tokens=20
) # Penalize authors with high post counts
author = response.choices[0].message.content.strip() post_count = monthly_counts.get(author, 0)
valid_authors = ["owenjohnson", "javiermorales", "aishapatel", "trangnguyen", "keishareid", "lilamoreau"] current_score -= post_count * 0.5
logging.info(f"Selected author: {author}")
return author if author in valid_authors else "owenjohnson" if current_score > best_score:
best_score = current_score
best_author = author
if not best_author:
best_author = min(monthly_counts, key=monthly_counts.get, default=random.choice(list(PERSONA_CONFIGS.keys())))
logging.info(f"Selected author: {best_author} with adjusted score: {best_score}")
return best_author
except Exception as e: except Exception as e:
logging.error(f"Author selection failed: {e}") logging.error(f"Error in select_best_author: {e}")
return "owenjohnson" return random.choice(list(PERSONA_CONFIGS.keys()))
def prepare_post_data(summary, title, main_topic=None): def prepare_post_data(summary, title, main_topic=None):
try: try:

Loading…
Cancel
Save