You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
2.2 KiB
44 lines
2.2 KiB
from foodie_config import OPENAI_API_KEY, LIGHT_TASK_MODEL |
|
from openai import OpenAI |
|
import logging |
|
import random |
|
from urllib.parse import quote |
|
|
|
client = OpenAI(api_key=OPENAI_API_KEY) |
|
|
|
def get_dynamic_hook(article_title): |
|
try: |
|
response = client.chat.completions.create( |
|
model=LIGHT_TASK_MODEL, |
|
messages=[ |
|
{"role": "system", "content": ( |
|
"Generate a short, catchy hook (under 100 characters) for a tweet based on this article title about food topics. " |
|
"Make it bold and quirky with Upworthy/Buzzfeed flair (e.g., 'This food twist is wild!'), avoiding clichés like 'game-changer'. " |
|
"Do not include emojis in the hook. " |
|
"Return only the hook text." |
|
)}, |
|
{"role": "user", "content": article_title} |
|
], |
|
max_tokens=30 |
|
) |
|
hook = response.choices[0].message.content.strip().replace('**', '') |
|
logging.info(f"Generated dynamic hook: {hook}") |
|
return hook |
|
except Exception as e: |
|
logging.error(f"Dynamic hook generation failed: {e}") |
|
return "This food scoop will blow your mind!" |
|
|
|
def select_best_cta(article_title, article_summary, post_url): |
|
# Use the provided post_url if available, otherwise a placeholder to be updated later |
|
share_url_base = post_url if post_url else "https://insiderfoodie.com/placeholder" |
|
share_url = f"https://x.com/intent/tweet?url={quote(share_url_base)}&text={quote(get_dynamic_hook(article_title))}" |
|
cta_options = [ |
|
f"Can’t Get Enough? Share This Now On <a href='{share_url}'><i class='tsi tsi-twitter'></i></a>!", |
|
f"Obsessed Yet? Spread the Word On <a href='{share_url}'><i class='tsi tsi-twitter'></i></a>!", |
|
f"This Blew Your Mind, Right? Tweet It On <a href='{share_url}'><i class='tsi tsi-twitter'></i></a>!", |
|
f"Ready to Spill the Tea? Share On <a href='{share_url}'><i class='tsi tsi-twitter'></i></a>!", |
|
f"Too Wild to Keep Quiet? Post It On <a href='{share_url}'><i class='tsi tsi-twitter'></i></a>!" |
|
] |
|
selected_cta = random.choice(cta_options) |
|
logging.info(f"Selected random CTA: {selected_cta}") |
|
return selected_cta |