fix no tweet issue

my-fix-branch
Shane 7 months ago
parent 8323744953
commit 5963712139
  1. 82
      foodie_automator_google.py
  2. 4
      foodie_automator_reddit.py
  3. 4
      foodie_automator_rss.py
  4. 8
      foodie_utils.py

@ -178,24 +178,23 @@ def fetch_duckduckgo_news_context(trend_title, hours=24):
logging.warning(f"DuckDuckGo News context fetch failed for '{trend_title}': {e}") logging.warning(f"DuckDuckGo News context fetch failed for '{trend_title}': {e}")
return trend_title return trend_title
def curate_from_google_trends(geo_list=['US']): def curate_from_google_trends():
original_source = '<a href="https://trends.google.com/">Google Trends</a>' # Fetch Google Trends data
for geo in geo_list: trends = fetch_google_trends()
trends = scrape_google_trends(geo=geo)
if not trends: if not trends:
print(f"No trends available for geo={geo}") print("No Google Trends data available")
logging.info(f"No trends available for geo={geo}") logging.info("No Google Trends data available")
continue return None, None, None
attempts = 0 attempts = 0
max_attempts = 10 max_attempts = 10
while attempts < max_attempts and trends: while attempts < max_attempts and trends:
trend = trends.pop(0) trend = trends.pop(0)
title = trend["title"] title = trend["title"]
link = trend["link"] link = trend.get("link", "https://trends.google.com/")
search_volume = trend["search_volume"] summary = trend.get("summary", "")
print(f"Trying Trend: {title} with search volume: {search_volume} for geo={geo}") source_name = "Google Trends"
logging.info(f"Trying Trend: {title} with search volume: {search_volume} for geo={geo}") original_source = f'<a href="{link}">{source_name}</a>'
if title in posted_titles: if title in posted_titles:
print(f"Skipping already posted trend: {title}") print(f"Skipping already posted trend: {title}")
@ -203,34 +202,40 @@ def curate_from_google_trends(geo_list=['US']):
attempts += 1 attempts += 1
continue continue
image_query, relevance_keywords, skip = smart_image_and_filter(title, "") print(f"Trying Google Trend: {title} from {source_name}")
logging.info(f"Trying Google Trend: {title} from {source_name}")
# Check if the trend should be filtered out
image_query, relevance_keywords, skip = smart_image_and_filter(title, summary)
if skip: if skip:
print(f"Skipping unwanted trend: {title}") print(f"Skipping filtered Google Trend: {title}")
logging.info(f"Skipping unwanted trend: {title}") logging.info(f"Skipping filtered Google Trend: {title}")
attempts += 1 attempts += 1
continue continue
context = fetch_duckduckgo_news_context(title) # Calculate interest score
scoring_content = f"{title}\n\n{context}" scoring_content = f"{title}\n\n{summary}"
interest_score = is_interesting(scoring_content) interest_score = is_interesting(scoring_content)
logging.info(f"Interest score for '{title}' in geo={geo}: {interest_score}") logging.info(f"Interest score for '{title}': {interest_score}")
if interest_score < 6: if interest_score < 6:
print(f"Trend Interest Too Low: {interest_score}") print(f"Google Trends Interest Too Low: {interest_score}")
logging.info(f"Trend Interest Too Low: {interest_score}") logging.info(f"Google Trends Interest Too Low: {interest_score}")
attempts += 1 attempts += 1
continue continue
# Summarize the trend
num_paragraphs = determine_paragraph_count(interest_score) num_paragraphs = determine_paragraph_count(interest_score)
extra_prompt = ( extra_prompt = (
f"Generate exactly {num_paragraphs} paragraphs. " f"Generate exactly {num_paragraphs} paragraphs. "
f"Do not mention Google Trends, Google, or include any links. " f"FOCUS: Summarize ONLY the provided content, explicitly mentioning '{title}' and sticking to its specific topic and details. "
f"Summarize as a standalone food industry trend, focusing on '{title}' and its context." f"Do NOT introduce unrelated concepts. Expand on the core idea with relevant context about its appeal or significance in food trends."
"Do not include emojis in the summary." "Do not include emojis in the summary."
) )
content_to_summarize = scoring_content
final_summary = summarize_with_gpt4o( final_summary = summarize_with_gpt4o(
scoring_content, content_to_summarize,
source_name="Google Trends", source_name,
source_url=link, link,
interest_score=interest_score, interest_score=interest_score,
extra_prompt=extra_prompt extra_prompt=extra_prompt
) )
@ -239,20 +244,26 @@ def curate_from_google_trends(geo_list=['US']):
attempts += 1 attempts += 1
continue continue
final_summary = insert_link_naturally(final_summary, "Google Trends", link) final_summary = insert_link_naturally(final_summary, source_name, link)
# Prepare post data
post_data, author, category, image_url, image_source, uploader, pixabay_url = prepare_post_data(final_summary, title) post_data, author, category, image_url, image_source, uploader, pixabay_url = prepare_post_data(final_summary, title)
if not post_data: if not post_data:
attempts += 1 attempts += 1
continue continue
# Fetch image
image_url, image_source, uploader, page_url = get_flickr_image_via_ddg(image_query, relevance_keywords) image_url, image_source, uploader, page_url = get_flickr_image_via_ddg(image_query, relevance_keywords)
if not image_url: if not image_url:
image_url, image_source, uploader, page_url = get_image(image_query) image_url, image_source, uploader, page_url = get_image(image_query)
# Generate hooks and initial CTA
hook = get_dynamic_hook(post_data["title"]).strip() hook = get_dynamic_hook(post_data["title"]).strip()
cta = select_best_cta(post_data["title"], final_summary, post_url=None) cta = select_best_cta(post_data["title"], final_summary, post_url=None)
post_data["content"] = f"{final_summary}\n\n{cta}" post_data["content"] = f"{final_summary}\n\n{cta}"
# Post to WordPress and tweet
global is_posting global is_posting
is_posting = True is_posting = True
try: try:
@ -266,7 +277,8 @@ def curate_from_google_trends(geo_list=['US']):
image_source=image_source, image_source=image_source,
uploader=uploader, uploader=uploader,
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score interest_score=interest_score,
should_post_tweet=True # Post the X tweet on the first call
) )
finally: finally:
is_posting = False is_posting = False
@ -287,7 +299,8 @@ def curate_from_google_trends(geo_list=['US']):
uploader=uploader, uploader=uploader,
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score, interest_score=interest_score,
post_id=post_id post_id=post_id,
should_post_tweet=False # Skip X tweet on the update call
) )
finally: finally:
is_posting = False is_posting = False
@ -299,18 +312,19 @@ def curate_from_google_trends(geo_list=['US']):
if image_url: if image_url:
save_json_file(USED_IMAGES_FILE, image_url, timestamp) save_json_file(USED_IMAGES_FILE, image_url, timestamp)
used_images.add(image_url)
logging.info(f"Saved image '{image_url}' to {USED_IMAGES_FILE}") logging.info(f"Saved image '{image_url}' to {USED_IMAGES_FILE}")
print(f"***** SUCCESS: Posted '{post_data['title']}' (ID: {post_id}) from trend for geo={geo} *****") print(f"***** SUCCESS: Posted '{post_data['title']}' (ID: {post_id}) from Google Trends *****")
logging.info(f"***** SUCCESS: Posted '{post_data['title']}' (ID: {post_id}) from trend for geo={geo} *****") logging.info(f"***** SUCCESS: Posted '{post_data['title']}' (ID: {post_id}) from Google Trends *****")
return post_data, category, random.randint(0, 1800) return post_data, category, random.randint(0, 1800)
print(f"No interesting trend found for geo={geo}") attempts += 1
logging.info(f"No interesting trend found for geo={geo}") logging.info(f"WP posting failed for '{post_data['title']}'")
print(f"No interesting trend found across regions {geo_list}") print("No interesting Google Trend found after attempts")
logging.info(f"No interesting trend found across regions {geo_list}") logging.info("No interesting Google Trend found after attempts")
return None, None, random.randint(600, 1200) return None, None, random.randint(600, 1800)
def run_google_trends_automator(): def run_google_trends_automator():
logging.info("***** Google Trends Automator Launched *****") logging.info("***** Google Trends Automator Launched *****")

@ -316,7 +316,7 @@ def curate_from_reddit():
uploader=uploader, uploader=uploader,
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score, interest_score=interest_score,
post_tweet=True # Post the X tweet on the first call should_post_tweet=True # Post the X tweet on the first call
) )
finally: finally:
is_posting = False is_posting = False
@ -338,7 +338,7 @@ def curate_from_reddit():
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score, interest_score=interest_score,
post_id=post_id, post_id=post_id,
post_tweet=False # Skip X tweet on the update call should_post_tweet=False # Skip X tweet on the update call
) )
finally: finally:
is_posting = False is_posting = False

@ -308,7 +308,7 @@ def curate_from_rss():
uploader=uploader, uploader=uploader,
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score, interest_score=interest_score,
post_tweet=True # Post the X tweet on the first call should_post_tweet=True # Post the X tweet on the first call
) )
finally: finally:
is_posting = False is_posting = False
@ -330,7 +330,7 @@ def curate_from_rss():
pixabay_url=pixabay_url, pixabay_url=pixabay_url,
interest_score=interest_score, interest_score=interest_score,
post_id=post_id, post_id=post_id,
post_tweet=False # Skip X tweet on the update call should_post_tweet=False # Skip X tweet on the update call
) )
finally: finally:
is_posting = False is_posting = False

@ -620,7 +620,7 @@ def get_wp_tag_id(tag_name, wp_base_url, wp_username, wp_password):
logging.error(f"Failed to get WP tag ID for '{tag_name}': {e}") logging.error(f"Failed to get WP tag ID for '{tag_name}': {e}")
return None return None
def post_to_wp(post_data, category, link, author, image_url, original_source, image_source="Pixabay", uploader=None, pixabay_url=None, interest_score=4, post_id=None, post_tweet=True): def post_to_wp(post_data, category, link, author, image_url, original_source, image_source="Pixabay", uploader=None, pixabay_url=None, interest_score=4, post_id=None, should_post_tweet=True):
wp_base_url = "https://insiderfoodie.com/wp-json/wp/v2" wp_base_url = "https://insiderfoodie.com/wp-json/wp/v2"
logging.info(f"Starting post_to_wp for '{post_data['title']}', image_source: {image_source}") logging.info(f"Starting post_to_wp for '{post_data['title']}', image_source: {image_source}")
@ -725,12 +725,12 @@ def post_to_wp(post_data, category, link, author, image_url, original_source, im
timestamp = datetime.now(timezone.utc).isoformat() timestamp = datetime.now(timezone.utc).isoformat()
save_post_to_recent(post_data["title"], post_url, author["username"], timestamp) save_post_to_recent(post_data["title"], post_url, author["username"], timestamp)
# Post article tweet to X only if post_tweet is True # Post article tweet to X only if should_post_tweet is True
if post_tweet: if should_post_tweet:
try: try:
post = {"title": post_data["title"], "url": post_url} post = {"title": post_data["title"], "url": post_url}
tweet = generate_article_tweet(author, post, author["persona"]) tweet = generate_article_tweet(author, post, author["persona"])
if post_tweet(author, tweet): if post_tweet(author, tweet): # Use the actual post_tweet function
logging.info(f"Successfully posted article tweet for {author['username']} on X") logging.info(f"Successfully posted article tweet for {author['username']} on X")
else: else:
logging.warning(f"Failed to post article tweet for {author['username']} on X") logging.warning(f"Failed to post article tweet for {author['username']} on X")

Loading…
Cancel
Save