|
|
|
|
@ -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}") |
|
|
|
|
return trend_title |
|
|
|
|
|
|
|
|
|
def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
original_source = '<a href="https://trends.google.com/">Google Trends</a>' |
|
|
|
|
for geo in geo_list: |
|
|
|
|
trends = scrape_google_trends(geo=geo) |
|
|
|
|
def curate_from_google_trends(): |
|
|
|
|
# Fetch Google Trends data |
|
|
|
|
trends = fetch_google_trends() |
|
|
|
|
if not trends: |
|
|
|
|
print(f"No trends available for geo={geo}") |
|
|
|
|
logging.info(f"No trends available for geo={geo}") |
|
|
|
|
continue |
|
|
|
|
print("No Google Trends data available") |
|
|
|
|
logging.info("No Google Trends data available") |
|
|
|
|
return None, None, None |
|
|
|
|
|
|
|
|
|
attempts = 0 |
|
|
|
|
max_attempts = 10 |
|
|
|
|
while attempts < max_attempts and trends: |
|
|
|
|
trend = trends.pop(0) |
|
|
|
|
title = trend["title"] |
|
|
|
|
link = trend["link"] |
|
|
|
|
search_volume = trend["search_volume"] |
|
|
|
|
print(f"Trying Trend: {title} with search volume: {search_volume} for geo={geo}") |
|
|
|
|
logging.info(f"Trying Trend: {title} with search volume: {search_volume} for geo={geo}") |
|
|
|
|
link = trend.get("link", "https://trends.google.com/") |
|
|
|
|
summary = trend.get("summary", "") |
|
|
|
|
source_name = "Google Trends" |
|
|
|
|
original_source = f'<a href="{link}">{source_name}</a>' |
|
|
|
|
|
|
|
|
|
if title in posted_titles: |
|
|
|
|
print(f"Skipping already posted trend: {title}") |
|
|
|
|
@ -203,34 +202,40 @@ def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
attempts += 1 |
|
|
|
|
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: |
|
|
|
|
print(f"Skipping unwanted trend: {title}") |
|
|
|
|
logging.info(f"Skipping unwanted trend: {title}") |
|
|
|
|
print(f"Skipping filtered Google Trend: {title}") |
|
|
|
|
logging.info(f"Skipping filtered Google Trend: {title}") |
|
|
|
|
attempts += 1 |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
context = fetch_duckduckgo_news_context(title) |
|
|
|
|
scoring_content = f"{title}\n\n{context}" |
|
|
|
|
# Calculate interest score |
|
|
|
|
scoring_content = f"{title}\n\n{summary}" |
|
|
|
|
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: |
|
|
|
|
print(f"Trend Interest Too Low: {interest_score}") |
|
|
|
|
logging.info(f"Trend Interest Too Low: {interest_score}") |
|
|
|
|
print(f"Google Trends Interest Too Low: {interest_score}") |
|
|
|
|
logging.info(f"Google Trends Interest Too Low: {interest_score}") |
|
|
|
|
attempts += 1 |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
# Summarize the trend |
|
|
|
|
num_paragraphs = determine_paragraph_count(interest_score) |
|
|
|
|
extra_prompt = ( |
|
|
|
|
f"Generate exactly {num_paragraphs} paragraphs. " |
|
|
|
|
f"Do not mention Google Trends, Google, or include any links. " |
|
|
|
|
f"Summarize as a standalone food industry trend, focusing on '{title}' and its context." |
|
|
|
|
f"FOCUS: Summarize ONLY the provided content, explicitly mentioning '{title}' and sticking to its specific topic and details. " |
|
|
|
|
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." |
|
|
|
|
) |
|
|
|
|
content_to_summarize = scoring_content |
|
|
|
|
final_summary = summarize_with_gpt4o( |
|
|
|
|
scoring_content, |
|
|
|
|
source_name="Google Trends", |
|
|
|
|
source_url=link, |
|
|
|
|
content_to_summarize, |
|
|
|
|
source_name, |
|
|
|
|
link, |
|
|
|
|
interest_score=interest_score, |
|
|
|
|
extra_prompt=extra_prompt |
|
|
|
|
) |
|
|
|
|
@ -239,20 +244,26 @@ def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
attempts += 1 |
|
|
|
|
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) |
|
|
|
|
if not post_data: |
|
|
|
|
attempts += 1 |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
# Fetch image |
|
|
|
|
image_url, image_source, uploader, page_url = get_flickr_image_via_ddg(image_query, relevance_keywords) |
|
|
|
|
if not image_url: |
|
|
|
|
image_url, image_source, uploader, page_url = get_image(image_query) |
|
|
|
|
|
|
|
|
|
# Generate hooks and initial CTA |
|
|
|
|
hook = get_dynamic_hook(post_data["title"]).strip() |
|
|
|
|
cta = select_best_cta(post_data["title"], final_summary, post_url=None) |
|
|
|
|
|
|
|
|
|
post_data["content"] = f"{final_summary}\n\n{cta}" |
|
|
|
|
|
|
|
|
|
# Post to WordPress and tweet |
|
|
|
|
global is_posting |
|
|
|
|
is_posting = True |
|
|
|
|
try: |
|
|
|
|
@ -266,7 +277,8 @@ def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
image_source=image_source, |
|
|
|
|
uploader=uploader, |
|
|
|
|
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: |
|
|
|
|
is_posting = False |
|
|
|
|
@ -287,7 +299,8 @@ def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
uploader=uploader, |
|
|
|
|
pixabay_url=pixabay_url, |
|
|
|
|
interest_score=interest_score, |
|
|
|
|
post_id=post_id |
|
|
|
|
post_id=post_id, |
|
|
|
|
should_post_tweet=False # Skip X tweet on the update call |
|
|
|
|
) |
|
|
|
|
finally: |
|
|
|
|
is_posting = False |
|
|
|
|
@ -299,18 +312,19 @@ def curate_from_google_trends(geo_list=['US']): |
|
|
|
|
|
|
|
|
|
if image_url: |
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
|
print(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 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 Google Trends *****") |
|
|
|
|
return post_data, category, random.randint(0, 1800) |
|
|
|
|
|
|
|
|
|
print(f"No interesting trend found for geo={geo}") |
|
|
|
|
logging.info(f"No interesting trend found for geo={geo}") |
|
|
|
|
attempts += 1 |
|
|
|
|
logging.info(f"WP posting failed for '{post_data['title']}'") |
|
|
|
|
|
|
|
|
|
print(f"No interesting trend found across regions {geo_list}") |
|
|
|
|
logging.info(f"No interesting trend found across regions {geo_list}") |
|
|
|
|
return None, None, random.randint(600, 1200) |
|
|
|
|
print("No interesting Google Trend found after attempts") |
|
|
|
|
logging.info("No interesting Google Trend found after attempts") |
|
|
|
|
return None, None, random.randint(600, 1800) |
|
|
|
|
|
|
|
|
|
def run_google_trends_automator(): |
|
|
|
|
logging.info("***** Google Trends Automator Launched *****") |
|
|
|
|
|