Update app.py
Browse files
app.py
CHANGED
@@ -563,13 +563,12 @@ PEXELS_API_URL = "https://api.pexels.com/v1/search"
|
|
563 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
564 |
|
565 |
def get_image_url(topic):
|
566 |
-
# ์ฃผ์ ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
567 |
try:
|
568 |
translated = translator(topic, max_length=40)[0]['translation_text']
|
569 |
keywords = ' '.join(translated.split()[:2])
|
570 |
except Exception as e:
|
571 |
logger.error(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
572 |
-
keywords = ' '.join(topic.split()[:2])
|
573 |
|
574 |
logger.debug(f"๊ฒ์ ํค์๋: {keywords}")
|
575 |
|
@@ -578,7 +577,8 @@ def get_image_url(topic):
|
|
578 |
}
|
579 |
params = {
|
580 |
"query": keywords,
|
581 |
-
"per_page":
|
|
|
582 |
}
|
583 |
|
584 |
try:
|
@@ -589,10 +589,15 @@ def get_image_url(topic):
|
|
589 |
data = response.json()
|
590 |
logger.debug(f"Pexels API ์๋ต ๋ฐ์ดํฐ: {data}")
|
591 |
|
592 |
-
|
593 |
-
|
|
|
|
|
|
|
|
|
|
|
594 |
else:
|
595 |
-
logger.warning(f"'{keywords}' ๊ด๋ จ ์ด๋ฏธ์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
596 |
return None
|
597 |
else:
|
598 |
logger.error(f"Pexels API ์ค๋ฅ: {response.status_code}")
|
@@ -601,6 +606,8 @@ def get_image_url(topic):
|
|
601 |
logger.error(f"์ด๋ฏธ์ง ๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
602 |
return None
|
603 |
|
|
|
|
|
604 |
def convert_markdown_images(content):
|
605 |
logger.debug("์๋ณธ ๋ด์ฉ: %s", content)
|
606 |
|
@@ -657,10 +664,10 @@ def process_all_titles(category, style, topic, num_titles):
|
|
657 |
image_url = get_image_url(topic)
|
658 |
|
659 |
if image_url:
|
660 |
-
# ์ด๋ฏธ์ง URL์ ๋ธ๋ก๊ทธ ๋ด์ฉ
|
661 |
-
blog_content
|
662 |
else:
|
663 |
-
blog_content
|
664 |
|
665 |
# ํฌ์คํ
์ ์ก
|
666 |
send_result = send_to_blogger(title, blog_content)
|
|
|
563 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
564 |
|
565 |
def get_image_url(topic):
|
|
|
566 |
try:
|
567 |
translated = translator(topic, max_length=40)[0]['translation_text']
|
568 |
keywords = ' '.join(translated.split()[:2])
|
569 |
except Exception as e:
|
570 |
logger.error(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
571 |
+
keywords = ' '.join(topic.split()[:2])
|
572 |
|
573 |
logger.debug(f"๊ฒ์ ํค์๋: {keywords}")
|
574 |
|
|
|
577 |
}
|
578 |
params = {
|
579 |
"query": keywords,
|
580 |
+
"per_page": 10,
|
581 |
+
"orientation": "landscape" # ๊ฐ๋ก ๋ฐฉํฅ ์ด๋ฏธ์ง๋ง ๊ฒ์
|
582 |
}
|
583 |
|
584 |
try:
|
|
|
589 |
data = response.json()
|
590 |
logger.debug(f"Pexels API ์๋ต ๋ฐ์ดํฐ: {data}")
|
591 |
|
592 |
+
suitable_photos = [
|
593 |
+
photo for photo in data['photos']
|
594 |
+
if photo['width'] / photo['height'] in [16/9, 4/3, 1] # 16:9, 4:3, 1:1 ๋น์จ๋ง ์ ํ
|
595 |
+
]
|
596 |
+
|
597 |
+
if suitable_photos:
|
598 |
+
return suitable_photos[0]['src']['large2x']
|
599 |
else:
|
600 |
+
logger.warning(f"'{keywords}' ๊ด๋ จ ์ ์ ํ ๋น์จ์ ์ด๋ฏธ์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
601 |
return None
|
602 |
else:
|
603 |
logger.error(f"Pexels API ์ค๋ฅ: {response.status_code}")
|
|
|
606 |
logger.error(f"์ด๋ฏธ์ง ๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
607 |
return None
|
608 |
|
609 |
+
|
610 |
+
|
611 |
def convert_markdown_images(content):
|
612 |
logger.debug("์๋ณธ ๋ด์ฉ: %s", content)
|
613 |
|
|
|
664 |
image_url = get_image_url(topic)
|
665 |
|
666 |
if image_url:
|
667 |
+
# ์ด๋ฏธ์ง URL์ ๋ธ๋ก๊ทธ ๋ด์ฉ ์๋จ์ ์ถ๊ฐ
|
668 |
+
blog_content = f"\n\n{blog_content}"
|
669 |
else:
|
670 |
+
blog_content = "๊ด๋ จ ์ด๋ฏธ์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.\n\n" + blog_content
|
671 |
|
672 |
# ํฌ์คํ
์ ์ก
|
673 |
send_result = send_to_blogger(title, blog_content)
|