fantaxy commited on
Commit
c30c215
ยท
verified ยท
1 Parent(s): ee0bc84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
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": 1
 
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
- if data['photos']:
593
- return data['photos'][0]['src']['large2x']
 
 
 
 
 
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 += f"\n\n![๊ด€๋ จ ์ด๋ฏธ์ง€]({image_url})"
662
  else:
663
- blog_content += "\n\n๊ด€๋ จ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
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"![๊ด€๋ จ ์ด๋ฏธ์ง€]({image_url})\n\n{blog_content}"
669
  else:
670
+ blog_content = "๊ด€๋ จ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n\n" + blog_content
671
 
672
  # ํฌ์ŠคํŒ… ์ „์†ก
673
  send_result = send_to_blogger(title, blog_content)