Ver3.0 ๐ค OpenAI API์ ํฌ๋กค๋ฌ๋ก ๋ง๋๋ ๋ด์ค์์ฝ ์๋๋ด ์๋ฒฝ ๊ฐ์ด๋

๐ค OpenAI API์ ํฌ๋กค๋ฌ๋ก ๋ง๋๋ ๋ด์ค์์ฝ ์๋๋ด ์๋ฒฝ ๊ฐ์ด๋
๋งค์ผ ์์์ง๋ ๋ด์ค, ์ด์ AI๊ฐ ์๋์ผ๋ก ์ ๋ฆฌํด๋๋ฆฝ๋๋ค! ๐ก
๐ฐ ์ ๋ด์ค์์ฝ ์๋๋ด์ด ํ์ํ ๊น?
์๋
! ์ค๋์ ์ ๋ง ์ค์ฉ์ ์ด๋ฉด์๋ ์ฌ๋ฏธ์๋ ํ๋ก์ ํธ๋ฅผ ํจ๊ป ๋ง๋ค์ด๋ณผ ๊ฑฐ์ผ. ๋ฐ๋ก OpenAI API์ ํฌ๋กค๋ฌ๋ฅผ ๊ฒฐํฉํ ๋ด์ค์์ฝ ์๋๋ด์ด์ง! ๐ฏ
์์ฆ ํ๋ฃจ์ ์์์ง๋ ๋ด์ค๊ฐ ์ผ๋ง๋ ๋ง์์ง ์์? ์ฃผ์ ํฌํธ ์ฌ์ดํธ๋ง ๋ด๋ ์๋ฐฑ, ์์ฒ ๊ฐ์ ๊ธฐ์ฌ๊ฐ ์ค์๊ฐ์ผ๋ก ์ฌ๋ผ์ค๊ณ ์์ด. ์ด๊ฑธ ๋ค ์ฝ์ผ๋ ค๋ฉด ํ๋ฃจ 24์๊ฐ๋ ๋ชจ์๋ผ์ง. ๊ทธ๋์ ์ฐ๋ฆฌ๋ AI์ ํ์ ๋น๋ ค์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด๋ณผ ๊ฑฐ์ผ! ๐
์ด ์๋๋ด์ ์น์์ ๋ด์ค๋ฅผ ์๋์ผ๋ก ์์งํ๊ณ , OpenAI์ ๊ฐ๋ ฅํ ์ธ์ด๋ชจ๋ธ์ ํ์ฉํด์ ํต์ฌ๋ง ์์ ๋ฝ์๋ด๋ ๋๋ํ ์น๊ตฌ์ผ. ๊ฐ๋ฐ์๋ผ๋ฉด ๋๊ตฌ๋ ํ ๋ฒ์ฏค ๋ง๋ค์ด๋ณด๊ณ ์ถ์๋ ํ๋ก์ ํธ์ผ ๊ฑฐ์ผ. ๊ทธ๋ฆฌ๊ณ ์ด๋ฐ ๊ธฐ์ ์ ์ตํ๋๋ฉด ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์๋ํ ์๋น์ค๋ฅผ ์ ๊ณตํ ์๋ ์์ด! ๐ผ
- โ ์น ํฌ๋กค๋ง ์ค์ ๊ฒฝํ
- โ OpenAI API ํ์ฉ ๋ฅ๋ ฅ
- โ ์๋ํ ์์คํ ๊ตฌ์ถ ๋ ธํ์ฐ
- โ ์ค๋ฌด์ ๋ฐ๋ก ์ ์ฉ ๊ฐ๋ฅํ ํฌํธํด๋ฆฌ์ค
- โ AI ์์ด์ ํธ ๊ฐ๋ฐ์ ๊ธฐ์ด
๐ ๏ธ ํ๋ก์ ํธ ์ค๋นํ๊ธฐ
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํด๋ณผ๊น? ๋จผ์ ์ฐ๋ฆฌ๊ฐ ํ์ํ ๋๊ตฌ๋ค์ ์ค๋นํด์ผ ํด. ์๋ฆฌ๋ฅผ ํ๊ธฐ ์ ์ ์ฌ๋ฃ๋ฅผ ์ค๋นํ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ! ๐ณ
์ด ํ๋ก์ ํธ๋ Python์ ๊ธฐ๋ฐ์ผ๋ก ์งํํ ๊ฑฐ์ผ. Python์ด ์น ํฌ๋กค๋ง๊ณผ AI ์์
์ ๊ฐ์ฅ ์ ํฉํ ์ธ์ด๊ฑฐ๋ . ๋ฌผ๋ก ๋ค๋ฅธ ์ธ์ด๋ก๋ ๊ฐ๋ฅํ์ง๋ง, Python์ ํ๋ถํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํ๊ณ๋ฅผ ํ์ฉํ๋ฉด ํจ์ฌ ์ฝ๊ณ ๋น ๋ฅด๊ฒ ๊ฐ๋ฐํ ์ ์์ด! ๐
๋จผ์ ํฐ๋ฏธ๋์ ์ด๊ณ ๋ค์ ๋ช ๋ น์ด๋ค์ ์ ๋ ฅํด์ค:
pip install openai
pip install requests
pip install beautifulsoup4
pip install selenium
pip install python-dotenv
pip install schedule
๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ๋ ์ผ์ ๊ฐ๋จํ ์ค๋ช ํ๋ฉด:
openai: OpenAI API์ ํต์ ํ๊ธฐ ์ํ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
requests: HTTP ์์ฒญ์ ๋ณด๋ด์ ์นํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
beautifulsoup4: HTML์ ํ์ฑํ๊ณ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ ๋๊ตฌ
selenium: ๋์ ์นํ์ด์ง๋ฅผ ํฌ๋กค๋งํ ๋ ํ์ํ ๋ธ๋ผ์ฐ์ ์๋ํ ๋๊ตฌ
python-dotenv: API ํค ๊ฐ์ ๋ฏผ๊ฐํ ์ ๋ณด๋ฅผ ์์ ํ๊ฒ ๊ด๋ฆฌ
schedule: ์๋ํ ์ค์ผ์ค๋ง์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
1๏ธโฃ OpenAI ์น์ฌ์ดํธ(platform.openai.com)์ ์ ์
2๏ธโฃ ๊ณ์ ์์ฑ ๋๋ ๋ก๊ทธ์ธ
3๏ธโฃ API Keys ๋ฉ๋ด๋ก ์ด๋
4๏ธโฃ "Create new secret key" ๋ฒํผ ํด๋ฆญ
5๏ธโฃ ์์ฑ๋ ํค๋ฅผ ์์ ํ ๊ณณ์ ๋ณต์ฌํด์ ๋ณด๊ด
โ ๏ธ ์ฃผ์: API ํค๋ ์ ๋ ๊ณต๊ฐ ์ ์ฅ์์ ์ฌ๋ฆฌ๋ฉด ์ ๋ผ! ๋๊ตฐ๊ฐ ์ ์ฉํ ์ ์๊ฑฐ๋ .
API ํค๋ฅผ ๋ฐ์๋ค๋ฉด, ํ๋ก์ ํธ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์ .env ํ์ผ์ ๋ง๋ค์ด์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ฅํด์ค:
OPENAI_API_KEY=your-api-key-here
NEWS_API_KEY=your-news-api-key-here
์ด๋ ๊ฒ ํ๋ฉด ์ฝ๋์์ ์ง์ ํค๋ฅผ ๋ ธ์ถํ์ง ์๊ณ ๋ ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ด! ๐
๐ท๏ธ ์น ํฌ๋กค๋ฌ ๊ตฌ์ถํ๊ธฐ
์, ์ด์ ๋ด์ค๋ฅผ ์์งํ๋ ํฌ๋กค๋ฌ๋ฅผ ๋ง๋ค์ด๋ณผ ์ฐจ๋ก์ผ! ํฌ๋กค๋ฌ๋ ์ฐ๋ฆฌ ๋ด์ ๋๊ณผ ๊ท ์ญํ ์ ํ๋ ์ค์ํ ๋ถ๋ถ์ด์ง. ์น์ฌ์ดํธ๋ง๋ค ๊ตฌ์กฐ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์, ์ ์ฐํ๊ฒ ๋์ํ ์ ์๋ ํฌ๋กค๋ฌ๋ฅผ ๋ง๋๋ ๊ฒ ํต์ฌ์ด์ผ. ๐ฏ
ํฌ๋กค๋ง ๋ฐฉ์์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋ ์ ์์ด:
๋์ ํฌ๋กค๋ง: JavaScript๋ก ์ฝํ ์ธ ๊ฐ ๋์ ์ผ๋ก ์์ฑ๋๋ ๊ฒฝ์ฐ. Selenium์ด๋ Playwright ๊ฐ์ ๋ธ๋ผ์ฐ์ ์๋ํ ๋๊ตฌ๊ฐ ํ์ํด. ๋๋ฆฌ์ง๋ง ๋ชจ๋ ์ฝํ ์ธ ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ด.
์ฐ๋ฆฌ๋ ๋ ๊ฐ์ง ๋ฐฉ์์ ๋ชจ๋ ๊ตฌํํด๋ณผ ๊ฑฐ์ผ. ๋จผ์ ๊ฐ๋จํ ์ ์ ํฌ๋กค๋ง๋ถํฐ ์์ํด๋ณด์! ๐
๐ ์ ์ ํฌ๋กค๋ง ๊ตฌํ
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import time
class NewsStaticCrawler:
def __init__(self, base_url):
self.base_url = base_url
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
def fetch_page(self, url):
"""์นํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋"""
try:
response = requests.get(url, headers=self.headers, timeout=10)
response.raise_for_status()
return response.text
except requests.RequestException as e:
print(f"ํ์ด์ง ์์ฒญ ์คํจ: {e}")
return None
def parse_news_list(self, html):
"""๋ด์ค ๋ชฉ๋ก์ ํ์ฑํ๋ ๋ฉ์๋"""
soup = BeautifulSoup(html, 'html.parser')
news_items = []
# ์์: ๋ค์ด๋ฒ ๋ด์ค ๊ตฌ์กฐ (์ค์ ๋ก๋ ํ๊ฒ ์ฌ์ดํธ์ ๋ง๊ฒ ์์ ํ์)
articles = soup.select('.news_area')
for article in articles:
try:
title_elem = article.select_one('.news_tit')
title = title_elem.text.strip() if title_elem else "์ ๋ชฉ ์์"
link = title_elem['href'] if title_elem else ""
summary_elem = article.select_one('.news_dsc')
summary = summary_elem.text.strip() if summary_elem else ""
press_elem = article.select_one('.info.press')
press = press_elem.text.strip() if press_elem else "์ถ์ฒ ๋ฏธ์"
news_items.append({
'title': title,
'link': link,
'summary': summary,
'press': press,
'crawled_at': datetime.now().isoformat()
})
except Exception as e:
print(f"๊ธฐ์ฌ ํ์ฑ ์ค ์ค๋ฅ: {e}")
continue
return news_items
def get_article_content(self, url):
"""๊ฐ๋ณ ๊ธฐ์ฌ์ ๋ณธ๋ฌธ์ ๊ฐ์ ธ์ค๋ ๋ฉ์๋"""
html = self.fetch_page(url)
if not html:
return None
soup = BeautifulSoup(html, 'html.parser')
# ๋ณธ๋ฌธ ์ถ์ถ (์ฌ์ดํธ๋ง๋ค ๋ค๋ฆ)
content_elem = soup.select_one('#articleBodyContents')
if content_elem:
# ๋ถํ์ํ ํ๊ทธ ์ ๊ฑฐ
for tag in content_elem(['script', 'style', 'iframe']):
tag.decompose()
return content_elem.get_text(strip=True)
return None
def crawl_news(self, max_articles=10):
"""๋ด์ค๋ฅผ ํฌ๋กค๋งํ๋ ๋ฉ์ธ ๋ฉ์๋"""
print(f"๐ท๏ธ ํฌ๋กค๋ง ์์: {self.base_url}")
html = self.fetch_page(self.base_url)
if not html:
return []
news_list = self.parse_news_list(html)
detailed_news = []
for i, news in enumerate(news_list[:max_articles]):
print(f"๐ฐ {i+1}/{max_articles}: {news['title'][:50]}...")
# ๋ณธ๋ฌธ ๊ฐ์ ธ์ค๊ธฐ
content = self.get_article_content(news['link'])
if content:
news['content'] = content
detailed_news.append(news)
# ์๋ฒ ๋ถํ ๋ฐฉ์ง๋ฅผ ์ํ ๋๋ ์ด
time.sleep(1)
print(f"โ
ํฌ๋กค๋ง ์๋ฃ: {len(detailed_news)}๊ฐ ๊ธฐ์ฌ ์์ง")
return detailed_news
์ด ์ฝ๋๋ ๊ธฐ๋ณธ์ ์ธ ์ ์ ํฌ๋กค๋ฌ์ ๊ตฌ์กฐ์ผ. ์ค์ ๋ก ์ฌ์ฉํ ๋๋ ํ๊ฒ ์น์ฌ์ดํธ์ HTML ๊ตฌ์กฐ๋ฅผ ๋ถ์ํด์ ์ ๋ ํฐ๋ฅผ ์์ ํด์ผ ํด. ํฌ๋กฌ ๊ฐ๋ฐ์ ๋๊ตฌ(F12)๋ฅผ ์ด์ด์ ์์๋ฅผ ๊ฒ์ฌํ๋ฉด ์ฝ๊ฒ ์ฐพ์ ์ ์์ด! ๐
โข User-Agent ์ค์ : ๋ด์ผ๋ก ์ธ์๋์ง ์๋๋ก ๋ธ๋ผ์ฐ์ ์ฒ๋ผ ๋ณด์ด๊ฒ ๋ง๋ค์ด
โข ์์ฒญ ๊ฐ๊ฒฉ ์กฐ์ : ๋๋ฌด ๋น ๋ฅด๊ฒ ์์ฒญํ๋ฉด IP๊ฐ ์ฐจ๋จ๋ ์ ์์ผ๋ ์ ์ ํ ๋๋ ์ด๋ฅผ ๋ฃ์ด์ค
โข ์๋ฌ ์ฒ๋ฆฌ: ๋คํธ์ํฌ ์ค๋ฅ๋ ํ์ฑ ์ค๋ฅ์ ๋๋นํ ์์ธ ์ฒ๋ฆฌ๊ฐ ํ์์ผ
๐ ๋์ ํฌ๋กค๋ง ๊ตฌํ
์ด์ JavaScript๋ก ๋ ๋๋ง๋๋ ํ์ด์ง๋ฅผ ํฌ๋กค๋งํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์. Selenium์ ์ฌ์ฉํ๋ฉด ์ค์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ ์ดํ ์ ์์ด์ ๋์ ์ฝํ ์ธ ๋ ๋ฌธ์ ์์ด ๊ฐ์ ธ์ฌ ์ ์์ด! ๐ญ
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
class NewsDynamicCrawler:
def __init__(self):
self.driver = None
self.setup_driver()
def setup_driver(self):
"""Selenium ๋๋ผ์ด๋ฒ ์ค์ """
chrome_options = Options()
chrome_options.add_argument('--headless') # ๋ธ๋ผ์ฐ์ ์ฐฝ ์จ๊ธฐ๊ธฐ
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)')
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.implicitly_wait(10)
def scroll_to_load(self, scroll_pause_time=2):
"""๋ฌดํ ์คํฌ๋กค ํ์ด์ง๋ฅผ ์ํ ์คํฌ๋กค ํจ์"""
last_height = self.driver.execute_script("return document.body.scrollHeight")
while True:
# ํ์ด์ง ๋๊น์ง ์คํฌ๋กค
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(scroll_pause_time)
# ์๋ก์ด ๋์ด ๊ณ์ฐ
new_height = self.driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
def crawl_dynamic_news(self, url, max_articles=10):
"""๋์ ํ์ด์ง ํฌ๋กค๋ง"""
try:
print(f"๐ ๋์ ํฌ๋กค๋ง ์์: {url}")
self.driver.get(url)
# ํ์ด์ง ๋ก๋ฉ ๋๊ธฐ
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "news-item"))
)
# ํ์์ ์คํฌ๋กค
self.scroll_to_load()
# ๋ด์ค ์์ดํ
์ถ์ถ
news_elements = self.driver.find_elements(By.CLASS_NAME, "news-item")
news_list = []
for i, element in enumerate(news_elements[:max_articles]):
try:
title = element.find_element(By.CLASS_NAME, "title").text
link = element.find_element(By.TAG_NAME, "a").get_attribute("href")
summary = element.find_element(By.CLASS_NAME, "summary").text
news_list.append({
'title': title,
'link': link,
'summary': summary,
'crawled_at': datetime.now().isoformat()
})
print(f"๐ฐ {i+1}/{max_articles}: {title[:50]}...")
except Exception as e:
print(f"์์ ์ถ์ถ ์ค๋ฅ: {e}")
continue
print(f"โ
๋์ ํฌ๋กค๋ง ์๋ฃ: {len(news_list)}๊ฐ ๊ธฐ์ฌ")
return news_list
except Exception as e:
print(f"ํฌ๋กค๋ง ์ค ์ค๋ฅ ๋ฐ์: {e}")
return []
finally:
if self.driver:
self.driver.quit()
def wait_for_element(self, by, value, timeout=10):
"""ํน์ ์์๊ฐ ๋ก๋๋ ๋๊น์ง ๋๊ธฐ"""
try:
element = WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located((by, value))
)
return element
except Exception as e:
print(f"์์ ๋๊ธฐ ์๊ฐ ์ด๊ณผ: {e}")
return None
Selenium์ ๊ฐ๋ ฅํ์ง๋ง ๋ฆฌ์์ค๋ฅผ ๋ง์ด ์ฌ์ฉํด. ๊ทธ๋์ ๊ฐ๋ฅํ๋ฉด ์ ์ ํฌ๋กค๋ง์ ์ฐ์ ์๋ํ๊ณ , ์ ๋ง ํ์ํ ๊ฒฝ์ฐ์๋ง Selenium์ ์ฌ์ฉํ๋ ๊ฒ ์ข์. ๋ํ headless ๋ชจ๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ธ๋ผ์ฐ์ ์ฐฝ์ด ๋จ์ง ์์์ ์๋ฒ ํ๊ฒฝ์์๋ ์คํํ ์ ์์ด! ๐ป
๐ค OpenAI API ์ฐ๋ํ๊ธฐ
๋๋์ด AI์ ํ์ ๋น๋ฆด ์๊ฐ์ด์ผ! OpenAI์ GPT ๋ชจ๋ธ์ ์ฌ์ฉํ๋ฉด ์์งํ ๋ด์ค๋ฅผ ์์ฐ์ค๋ฝ๊ณ ์ ํํ๊ฒ ์์ฝํ ์ ์์ด. ์ด ๋ถ๋ถ์ด ๋ฐ๋ก ์ฐ๋ฆฌ ๋ด์ ๋๋ ์ญํ ์ ํ๋ ํต์ฌ์ด์ง! ๐ง โจ
OpenAI API๋ ์ฌ์ฉํ๊ธฐ ์ ๋ง ๊ฐ๋จํด. ํ์ง๋ง ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๋ ค๋ฉด ํ๋กฌํํธ ์์ง๋์ด๋ง์ด ์ค์ํด. ์ข์ ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ฉด ํจ์ฌ ๋ ๋์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๊ฑฐ๋ !
2๏ธโฃ ๋งฅ๋ฝ ์ ๊ณต: AI์๊ฒ ์ถฉ๋ถํ ๋ฐฐ๊ฒฝ ์ ๋ณด๋ฅผ ์ค
3๏ธโฃ ํ์ ์ง์ : ์ถ๋ ฅ ํ์์ ๋ช ํํ ์ ์ํด
4๏ธโฃ ์์ ์ ๊ณต: Few-shot learning์ผ๋ก ํ์ง ํฅ์
5๏ธโฃ ์ ์ฝ ์กฐ๊ฑด: ๊ธ์ ์, ํค์ค๋งค๋ ๋ฑ์ ๋ช ์ํด
import openai
from dotenv import load_dotenv
import os
import json
# ํ๊ฒฝ ๋ณ์ ๋ก๋
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
class NewsAISummarizer:
def __init__(self, model="gpt-4o-mini"):
self.model = model
self.max_tokens = 500
self.temperature = 0.7
def create_summary_prompt(self, article):
"""์์ฝ์ ์ํ ํ๋กฌํํธ ์์ฑ"""
prompt = f"""๋น์ ์ ์ ๋ฌธ ๋ด์ค ์๋ํฐ์
๋๋ค. ๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ์ฝ๊ณ ํต์ฌ ๋ด์ฉ์ ์์ฝํด์ฃผ์ธ์.
์์ฝ ๊ฐ์ด๋๋ผ์ธ:
- 3-5๋ฌธ์ฅ์ผ๋ก ๊ฐ๊ฒฐํ๊ฒ ์์ฑ
- 5W1H(๋๊ฐ, ์ธ์ , ์ด๋์, ๋ฌด์์, ์, ์ด๋ป๊ฒ)๋ฅผ ํฌํจ
- ๊ฐ๊ด์ ์ด๊ณ ์ค๋ฆฝ์ ์ธ ํค ์ ์ง
- ์ค์ํ ์์น๋ ๋ ์ง๋ ์ ํํ ํฌํจ
- ๋ถํ์ํ ์์์ด ์ ๊ฑฐ
๊ธฐ์ฌ ์ ๋ชฉ: {article['title']}
๊ธฐ์ฌ ๋ณธ๋ฌธ:
{article['content'][:3000]}
์์ฝ:"""
return prompt
def summarize_article(self, article):
"""๋จ์ผ ๊ธฐ์ฌ ์์ฝ"""
try:
prompt = self.create_summary_prompt(article)
response = openai.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "๋น์ ์ ๋ด์ค ์์ฝ ์ ๋ฌธ๊ฐ์
๋๋ค."},
{"role": "user", "content": prompt}
],
max_tokens=self.max_tokens,
temperature=self.temperature
)
summary = response.choices[0].message.content.strip()
return {
'original_title': article['title'],
'original_link': article['link'],
'summary': summary,
'press': article.get('press', ''),
'tokens_used': response.usage.total_tokens,
'summarized_at': datetime.now().isoformat()
}
except Exception as e:
print(f"์์ฝ ์ค ์ค๋ฅ ๋ฐ์: {e}")
return None
def batch_summarize(self, articles, delay=1):
"""์ฌ๋ฌ ๊ธฐ์ฌ ์ผ๊ด ์์ฝ"""
summaries = []
total_tokens = 0
print(f"๐ค AI ์์ฝ ์์: {len(articles)}๊ฐ ๊ธฐ์ฌ")
for i, article in enumerate(articles):
print(f"๐ ์์ฝ ์ค ({i+1}/{len(articles)}): {article['title'][:40]}...")
summary = self.summarize_article(article)
if summary:
summaries.append(summary)
total_tokens += summary['tokens_used']
# API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง
time.sleep(delay)
print(f"โ
์์ฝ ์๋ฃ: {len(summaries)}๊ฐ / ์ด ํ ํฐ: {total_tokens}")
return summaries, total_tokens
def create_digest(self, summaries, topic="์ค๋์ ๋ด์ค"):
"""์ฌ๋ฌ ์์ฝ์ ํ๋์ ๋ค์ด์ ์คํธ๋ก ํตํฉ"""
summaries_text = "\n\n".join([
f"{i+1}. {s['original_title']}\n{s['summary']}"
for i, s in enumerate(summaries)
])
prompt = f"""๋ค์์ ์ค๋์ ์ฃผ์ ๋ด์ค ์์ฝ๋ค์
๋๋ค. ์ด๋ฅผ ๋ฐํ์ผ๋ก ์ ์ฒด์ ์ธ ๋ด์ค ๋ค์ด์ ์คํธ๋ฅผ ์์ฑํด์ฃผ์ธ์.
{summaries_text}
๋ค์ด์ ์คํธ ์์ฑ ๊ฐ์ด๋:
- ์ ์ฒด์ ์ธ ํ๋ฆ๊ณผ ๋งฅ๋ฝ์ ํ์
- ์ฐ๊ด๋ ๋ด์ค๋ค์ ๊ทธ๋ฃนํ
- ์ค์๋ ์์ผ๋ก ์ฌ๋ฐฐ์น
- ๊ฐ๊ฒฐํ๊ณ ์ฝ๊ธฐ ์ฝ๊ฒ ์์ฑ
๋ค์ด์ ์คํธ:"""
try:
response = openai.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "๋น์ ์ ๋ด์ค ํ๋ ์ดํฐ์
๋๋ค."},
{"role": "user", "content": prompt}
],
max_tokens=1000,
temperature=0.7
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"๋ค์ด์ ์คํธ ์์ฑ ์ค๋ฅ: {e}")
return None
์ด ์ฝ๋๋ OpenAI API๋ฅผ ํ์ฉํ ๋ด์ค ์์ฝ ์์คํ ์ ํต์ฌ์ด์ผ. gpt-4o-mini ๋ชจ๋ธ์ ์ฌ์ฉํ๋ฉด ๋น์ฉ ๋๋น ์ฑ๋ฅ์ด ์ข์์ ์ค์ฉ์ ์ด์ผ. ๋ฌผ๋ก ๋ ๋์ ํ์ง์ด ํ์ํ๋ฉด gpt-4๋ฅผ ์ฌ์ฉํ ์๋ ์์ด! ๐ฐ
โข ์บ์ฑ ํ์ฉ: ๊ฐ์ ๊ธฐ์ฌ๋ฅผ ์ค๋ณต ์์ฝํ์ง ์๋๋ก ์บ์๋ฅผ ๊ตฌํํด
โข ๋ฐฐ์น ์ฒ๋ฆฌ: ์ฌ๋ฌ ์์ฝ์ ํ ๋ฒ์ ์์ฒญํ๋ ๊ฒ๋ณด๋ค ๊ฐ๋ณ ์ฒ๋ฆฌ๊ฐ ๋ ํจ์จ์ ์ผ ์ ์์ด
โข ๋ชจ๋ธ ์ ํ: ์์ ์ ๋ฐ๋ผ ์ ์ ํ ๋ชจ๋ธ์ ์ ํํด. ๊ฐ๋จํ ์์ฝ์ mini ๋ชจ๋ธ๋ก๋ ์ถฉ๋ถํด
๐จ ๊ณ ๊ธ ํ๋กฌํํธ ๊ธฐ๋ฒ
๋ ๋์ ์์ฝ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ธฐ ์ํ ๊ณ ๊ธ ํ๋กฌํํธ ๊ธฐ๋ฒ๋ค์ ์์๋ณด์! ์ด๋ฐ ํ ํฌ๋๋ค์ ์ฌ์ฉํ๋ฉด AI์ ์ฑ๋ฅ์ ๊ทน๋ํํ ์ ์์ด. ๐
class AdvancedPromptTemplates:
@staticmethod
def chain_of_thought_summary(article):
"""์ฌ๊ณ ์ ์ฐ์(Chain of Thought) ๊ธฐ๋ฒ"""
return f"""๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ๋จ๊ณ์ ์ผ๋ก ๋ถ์ํ๊ณ ์์ฝํด์ฃผ์ธ์.
๊ธฐ์ฌ: {article['title']}
{article['content'][:2000]}
๋ถ์ ๋จ๊ณ:
1. ์ฃผ์ ์ฌ๊ฑด/์ด์ ํ์
2. ๊ด๋ จ ์ธ๋ฌผ/๊ธฐ๊ด ์๋ณ
3. ์๊ฐ์ ๋ฐฐ๊ฒฝ ํ์ธ
4. ์ํฅ๊ณผ ์๋ฏธ ๋ถ์
5. ํต์ฌ ๋ฉ์์ง ์ถ์ถ
๊ฐ ๋จ๊ณ๋ฅผ ๊ฑฐ์ณ ์ต์ข
์์ฝ์ ์์ฑํด์ฃผ์ธ์."""
@staticmethod
def few_shot_summary(article):
"""Few-shot Learning ๊ธฐ๋ฒ"""
return f"""๋ค์์ ๋ด์ค ์์ฝ์ ์ข์ ์์์
๋๋ค:
์์ 1:
์๋ฌธ: "์ ๋ถ๊ฐ ๋ด๋
๋ ์ต์ ์๊ธ์ ์๊ฐ๋น 9,860์์ผ๋ก ๊ฒฐ์ ํ๋ค..."
์์ฝ: ์ ๋ถ๋ 2024๋
์ต์ ์๊ธ์ ์๊ฐ๋น 9,860์์ผ๋ก ํ์ ํ๋ค. ์ด๋ ์ฌํด ๋๋น 2.5% ์ธ์๋ ๊ธ์ก์ผ๋ก, ์ฝ 330๋ง ๋ช
์ ๊ทผ๋ก์์๊ฒ ์ํฅ์ ๋ฏธ์น ์ ๋ง์ด๋ค.
์์ 2:
์๋ฌธ: "์ผ์ฑ์ ์๊ฐ ์ฐจ์ธ๋ ๋ฐ๋์ฒด ๊ธฐ์ ๊ฐ๋ฐ์ ์ฑ๊ณตํ๋ค๊ณ ๋ฐํํ๋ค..."
์์ฝ: ์ผ์ฑ์ ์๊ฐ 3๋๋
ธ ๊ณต์ ๊ธฐ์ ๊ฐ๋ฐ์ ์ฑ๊ณตํ๋ค. ์ด ๊ธฐ์ ์ ๊ธฐ์กด ๋๋น ์ ๋ ฅ ์๋น๋ฅผ 30% ์ค์ด๊ณ ์ฑ๋ฅ์ 15% ํฅ์์ํฌ ์ ์์ด, ๊ธ๋ก๋ฒ ๋ฐ๋์ฒด ์์ฅ์์ ๊ฒฝ์๋ ฅ์ ๋์ผ ๊ฒ์ผ๋ก ๊ธฐ๋๋๋ค.
์ด์ ๋ค์ ๊ธฐ์ฌ๋ฅผ ์์ ๊ฐ์ ์คํ์ผ๋ก ์์ฝํด์ฃผ์ธ์:
{article['title']}
{article['content'][:2000]}"""
@staticmethod
def structured_summary(article):
"""๊ตฌ์กฐํ๋ ์ถ๋ ฅ ์์ฒญ"""
return f"""๋ค์ ๋ด์ค๋ฅผ JSON ํ์์ผ๋ก ๊ตฌ์กฐํํ์ฌ ์์ฝํด์ฃผ์ธ์.
๊ธฐ์ฌ: {article['content'][:2000]}
์ถ๋ ฅ ํ์:
{{
"headline": "ํ ์ค ํค๋๋ผ์ธ",
"summary": "3-4๋ฌธ์ฅ ์์ฝ",
"key_points": ["ํต์ฌ ํฌ์ธํธ 1", "ํต์ฌ ํฌ์ธํธ 2", "ํต์ฌ ํฌ์ธํธ 3"],
"entities": {{
"people": ["๊ด๋ จ ์ธ๋ฌผ"],
"organizations": ["๊ด๋ จ ๊ธฐ๊ด"],
"locations": ["๊ด๋ จ ์ฅ์"]
}},
"category": "์นดํ
๊ณ ๋ฆฌ",
"sentiment": "๊ธ์ /์ค๋ฆฝ/๋ถ์ "
}}"""
@staticmethod
def audience_targeted_summary(article, audience="์ผ๋ฐ์ธ"):
"""ํ๊ฒ ๋
์ ๋ง์ถค ์์ฝ"""
audience_styles = {
"์ผ๋ฐ์ธ": "์ฝ๊ณ ๊ฐ๋จํ ์ฉ์ด๋ก ์ค๋ช
",
"์ ๋ฌธ๊ฐ": "์ ๋ฌธ ์ฉ์ด์ ์์ธํ ๋ถ์ ํฌํจ",
"ํ์": "๊ต์ก์ ๊ด์ ์์ ์ดํดํ๊ธฐ ์ฝ๊ฒ",
"ํฌ์์": "๊ฒฝ์ ์ ์ํฅ๊ณผ ์์ฅ ์ ๋ง ์ค์ฌ"
}
style = audience_styles.get(audience, audience_styles["์ผ๋ฐ์ธ"])
return f"""๋ค์ ๋ด์ค๋ฅผ {audience}์ ์ํด ์์ฝํด์ฃผ์ธ์.
์คํ์ผ: {style}
๊ธฐ์ฌ: {article['content'][:2000]}
์์ฝ:"""
์ด๋ฐ ๊ณ ๊ธ ๊ธฐ๋ฒ๋ค์ ์ฌ์ฉํ๋ฉด ๋จ์ํ ์์ฝ์ ๋์ด์ ๋ ํ๋ถํ๊ณ ์ ์ฉํ ์ ๋ณด๋ฅผ ์ ๊ณตํ ์ ์์ด. ํนํ ๊ตฌ์กฐํ๋ ์ถ๋ ฅ์ ๋์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ฑฐ๋ ๋ค๋ฅธ ์์คํ ๊ณผ ์ฐ๋ํ ๋ ์ ๋ง ์ ์ฉํด! ๐
๐ ์๋ํ ์์คํ ๊ตฌ์ถํ๊ธฐ
์ด์ ํฌ๋กค๋ฌ์ AI ์์ฝ๊ธฐ๋ฅผ ๋ง๋ค์์ผ๋, ์ด๊ฑธ ์๋์ผ๋ก ์คํ๋๊ฒ ๋ง๋ค์ด์ผ ํด! ๋งค์ผ ์ ํด์ง ์๊ฐ์ ์๋์ผ๋ก ๋ด์ค๋ฅผ ์์งํ๊ณ ์์ฝํ๋ ์์คํ ์ ๊ตฌ์ถํด๋ณด์. ์ด๊ฒ ๋ฐ๋ก ์ง์ง '๋ด'๋ค์ด ๋ชจ์ต์ด์ง! ๐คโฐ
์๋ฌ ํธ๋ค๋ง: ์คํจํด๋ ๊ณ์ ๋์ํ๋๋ก
๋ก๊น : ๋ชจ๋ ์์ ๊ธฐ๋ก ๋จ๊ธฐ๊ธฐ
์๋ฆผ: ์์ ์๋ฃ๋ ์ค๋ฅ ๋ฐ์ ์ ์๋ฆผ
๋ฐ์ดํฐ ์ ์ฅ: ๊ฒฐ๊ณผ๋ฅผ ์์ ํ๊ฒ ๋ณด๊ด
import schedule
import time
import logging
from datetime import datetime
import json
import sqlite3
from pathlib import Path
# ๋ก๊น
์ค์
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('news_bot.log'),
logging.StreamHandler()
]
)
class NewsAutomationBot:
def __init__(self, config_path='config.json'):
self.config = self.load_config(config_path)
self.crawler = None
self.summarizer = None
self.db_path = 'news_summaries.db'
self.setup_database()
def load_config(self, config_path):
"""์ค์ ํ์ผ ๋ก๋"""
try:
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
logging.warning("์ค์ ํ์ผ์ด ์์ต๋๋ค. ๊ธฐ๋ณธ ์ค์ ์ ์ฌ์ฉํฉ๋๋ค.")
return self.get_default_config()
def get_default_config(self):
"""๊ธฐ๋ณธ ์ค์ """
return {
"news_sources": [
{"name": "๋ค์ด๋ฒ๋ด์ค", "url": "https://news.naver.com", "type": "static"},
{"name": "๋ค์๋ด์ค", "url": "https://news.daum.net", "type": "static"}
],
"schedule": {
"times": ["09:00", "15:00", "21:00"],
"max_articles": 10
},
"notification": {
"enabled": True,
"email": "your-email@example.com"
}
}
def setup_database(self):
"""๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๊ธฐํ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS news_summaries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
original_link TEXT,
summary TEXT,
press TEXT,
category TEXT,
crawled_at TEXT,
summarized_at TEXT,
tokens_used INTEGER
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS bot_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
status TEXT,
articles_processed INTEGER,
total_tokens INTEGER,
error_message TEXT
)
''')
conn.commit()
conn.close()
logging.info("โ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๊ธฐํ ์๋ฃ")
def save_summaries(self, summaries):
"""์์ฝ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
for summary in summaries:
cursor.execute('''
INSERT INTO news_summaries
(title, original_link, summary, press, summarized_at, tokens_used)
VALUES (?, ?, ?, ?, ?, ?)
''', (
summary['original_title'],
summary['original_link'],
summary['summary'],
summary.get('press', ''),
summary['summarized_at'],
summary['tokens_used']
))
conn.commit()
conn.close()
logging.info(f"๐พ {len(summaries)}๊ฐ ์์ฝ ์ ์ฅ ์๋ฃ")
def log_execution(self, status, articles_count, tokens, error=None):
"""์คํ ๋ก๊ทธ ์ ์ฅ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO bot_logs
(timestamp, status, articles_processed, total_tokens, error_message)
VALUES (?, ?, ?, ?, ?)
''', (
datetime.now().isoformat(),
status,
articles_count,
tokens,
error
))
conn.commit()
conn.close()
def run_news_cycle(self):
"""๋ด์ค ์์ง ๋ฐ ์์ฝ ์ฌ์ดํด ์คํ"""
try:
logging.info("๐ ๋ด์ค ๋ด ์คํ ์์")
# ํฌ๋กค๋ฌ ์ด๊ธฐํ
crawler = NewsStaticCrawler(self.config['news_sources'][0]['url'])
summarizer = NewsAISummarizer()
# ๋ด์ค ์์ง
articles = crawler.crawl_news(
max_articles=self.config['schedule']['max_articles']
)
if not articles:
logging.warning("โ ๏ธ ์์ง๋ ๊ธฐ์ฌ๊ฐ ์์ต๋๋ค")
return
# AI ์์ฝ
summaries, total_tokens = summarizer.batch_summarize(articles)
# ๊ฒฐ๊ณผ ์ ์ฅ
self.save_summaries(summaries)
# ๋ค์ด์ ์คํธ ์์ฑ
digest = summarizer.create_digest(summaries)
if digest:
self.save_digest(digest)
# ๋ก๊ทธ ๊ธฐ๋ก
self.log_execution('SUCCESS', len(summaries), total_tokens)
# ์๋ฆผ ๋ฐ์ก
if self.config['notification']['enabled']:
self.send_notification(summaries, digest)
logging.info(f"โ
๋ด์ค ๋ด ์คํ ์๋ฃ: {len(summaries)}๊ฐ ์์ฝ")
except Exception as e:
logging.error(f"โ ์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
self.log_execution('ERROR', 0, 0, str(e))
def save_digest(self, digest):
"""๋ค์ด์ ์คํธ๋ฅผ ํ์ผ๋ก ์ ์ฅ"""
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"digest_{timestamp}.txt"
with open(filename, 'w', encoding='utf-8') as f:
f.write(f"=== ๋ด์ค ๋ค์ด์ ์คํธ ===\n")
f.write(f"์์ฑ ์๊ฐ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
f.write(digest)
logging.info(f"๐ ๋ค์ด์ ์คํธ ์ ์ฅ: {filename}")
def send_notification(self, summaries, digest):
"""์๋ฆผ ๋ฐ์ก (์ด๋ฉ์ผ, ์ฌ๋ ๋ฑ)"""
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ ๋ก๊ทธ๋ก ๋์ฒด
logging.info(f"๐ง ์๋ฆผ ๋ฐ์ก: {len(summaries)}๊ฐ ๋ด์ค ์์ฝ ์๋ฃ")
# ์ค์ ๋ก๋ ์ด๋ฉ์ผ์ด๋ ์ฌ๋ API๋ฅผ ์ฌ์ฉ
def start_scheduler(self):
"""์ค์ผ์ค๋ฌ ์์"""
logging.info("โฐ ์ค์ผ์ค๋ฌ ์์")
# ์ค์ ๋ ์๊ฐ๋ง๋ค ์คํ
for time_str in self.config['schedule']['times']:
schedule.every().day.at(time_str).do(self.run_news_cycle)
logging.info(f"๐
์ค์ผ์ค ๋ฑ๋ก: ๋งค์ผ {time_str}")
# ์ฆ์ ํ ๋ฒ ์คํ
self.run_news_cycle()
# ๋ฌดํ ๋ฃจํ๋ก ์ค์ผ์ค ์ฒดํฌ
while True:
schedule.run_pending()
time.sleep(60) # 1๋ถ๋ง๋ค ์ฒดํฌ
# ๋ด ์คํ
if __name__ == "__main__":
bot = NewsAutomationBot()
bot.start_scheduler()
์ด ์๋ํ ์์คํ ์ ์ค์ ํ ์๊ฐ๋ง๋ค ์๋์ผ๋ก ๋ด์ค๋ฅผ ์์งํ๊ณ ์์ฝํด. ๊ทธ๋ฆฌ๊ณ ๋ชจ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ณ ๋ก๊ทธ๋ฅผ ๋จ๊ฒจ์ ๋์ค์ ๋ถ์ํ ์ ์์ด. ์ ๋ง ์์ ํ ์๋ํ ๋ด์ด์ง! ๐
โข ์๋ฌ ๋ชจ๋ํฐ๋ง: ์ ๊ธฐ์ ์ผ๋ก ๋ก๊ทธ๋ฅผ ํ์ธํ๊ณ ์ค๋ฅ์ ๋์ํด์ผ ํด
โข API ๋น์ฉ: OpenAI API ์ฌ์ฉ๋์ ๋ชจ๋ํฐ๋งํ๊ณ ์์ฐ์ ๊ด๋ฆฌํด
โข ์ ์๊ถ: ๋ด์ค ์ฝํ ์ธ ์ฌ์ฉ ์ ์ ์๊ถ ๋ฌธ์ ๋ฅผ ๊ณ ๋ คํด์ผ ํด
๐ ๋ฐ์ดํฐ ์ ์ฅ ๋ฐ ๊ด๋ฆฌ
์์งํ๊ณ ์์ฝํ ๋ด์ค๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ ๊ฒ๋ ์ค์ํด! ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค๊ณ๋ถํฐ ๋ฐฑ์ ์ ๋ต๊น์ง ์์๋ณด์. ๐พ
๐๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค๊ณ
์์์ SQLite๋ฅผ ์ฌ์ฉํ์ง๋ง, ๋ ํฐ ๊ท๋ชจ์ ์๋น์ค๋ผ๋ฉด PostgreSQL์ด๋ MongoDB๋ฅผ ๊ณ ๋ คํ ์ ์์ด. ๊ฐ๊ฐ์ ์ฅ๋จ์ ์ ์ดํด๋ณด์:
| ๋ฐ์ดํฐ๋ฒ ์ด์ค | ์ฅ์ | ๋จ์ | ์ ํฉํ ๊ฒฝ์ฐ |
|---|---|---|---|
| SQLite | ์ค์น ๋ถํ์, ๊ฐ๋ณ๊ณ ๋น ๋ฆ | ๋์ ์ ์ ์ ํ, ํ์ฅ์ฑ ๋ฎ์ | ๊ฐ์ธ ํ๋ก์ ํธ, ํ๋กํ ํ์ |
| PostgreSQL | ๊ฐ๋ ฅํ ๊ธฐ๋ฅ, ํ์ฅ์ฑ ์ฐ์ | ์ค์ ๋ณต์ก, ๋ฆฌ์์ค ๋ง์ด ์ฌ์ฉ | ์ค๋ํ ์๋น์ค, ๋ณต์กํ ์ฟผ๋ฆฌ |
| MongoDB | ์ ์ฐํ ์คํค๋ง, JSON ์นํ์ | ํธ๋์ญ์ ์ ํ์ | ๋น์ ํ ๋ฐ์ดํฐ, ๋น ๋ฅธ ๊ฐ๋ฐ |
| MySQL | ๋๋ฆฌ ์ฌ์ฉ๋จ, ์์ ์ | ์ผ๋ถ ๊ณ ๊ธ ๊ธฐ๋ฅ ๋ถ์กฑ | ์น ์๋น์ค, ๋ฒ์ฉ ๋ชฉ์ |
# MongoDB๋ฅผ ์ฌ์ฉํ ์์
from pymongo import MongoClient
from datetime import datetime
class MongoNewsStorage:
def __init__(self, connection_string="mongodb://localhost:27017/"):
self.client = MongoClient(connection_string)
self.db = self.client['news_bot']
self.summaries = self.db['summaries']
self.logs = self.db['logs']
# ์ธ๋ฑ์ค ์์ฑ
self.summaries.create_index([('title', 'text'), ('summary', 'text')])
self.summaries.create_index('summarized_at')
def save_summary(self, summary_data):
"""์์ฝ ์ ์ฅ"""
document = {
'title': summary_data['original_title'],
'link': summary_data['original_link'],
'summary': summary_data['summary'],
'press': summary_data.get('press', ''),
'tokens_used': summary_data['tokens_used'],
'summarized_at': datetime.fromisoformat(summary_data['summarized_at']),
'created_at': datetime.now()
}
result = self.summaries.insert_one(document)
return result.inserted_id
def search_summaries(self, keyword, limit=10):
"""ํค์๋๋ก ์์ฝ ๊ฒ์"""
results = self.summaries.find(
{'$text': {'$search': keyword}},
{'score': {'$meta': 'textScore'}}
).sort([('score', {'$meta': 'textScore'})]).limit(limit)
return list(results)
def get_daily_stats(self, date=None):
"""์ผ์ผ ํต๊ณ"""
if date is None:
date = datetime.now()
start_of_day = date.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = date.replace(hour=23, minute=59, second=59, microsecond=999999)
pipeline = [
{
'$match': {
'summarized_at': {
'$gte': start_of_day,
'$lte': end_of_day
}
}
},
{
'$group': {
'_id': None,
'total_articles': {'$sum': 1},
'total_tokens': {'$sum': '$tokens_used'},
'presses': {'$addToSet': '$press'}
}
}
]
result = list(self.summaries.aggregate(pipeline))
return result[0] if result else None
MongoDB๋ JSON ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ AI๊ฐ ์์ฑํ ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ์ ์ ๋ง ํธ๋ฆฌํด! ๊ทธ๋ฆฌ๊ณ ์ ๋ฌธ ๊ฒ์(Full-text search) ๊ธฐ๋ฅ๋ ๋ด์ฅ๋์ด ์์ด์ ๋์ค์ ๊ฒ์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ธฐ๋ ์ฌ์. ๐
๐จ ๊ฒฐ๊ณผ ์๊ฐํ ๋ฐ ๋์๋ณด๋
์์งํ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์๊ฐํํ๋ฉด ํจ์ฌ ๋ ์ ์ฉํด! ๊ฐ๋จํ ์น ๋์๋ณด๋๋ฅผ ๋ง๋ค์ด์ ์์ฝ๋ ๋ด์ค๋ฅผ ํ์ธํ๊ณ , ํต๊ณ๋ ๋ณผ ์ ์๊ฒ ํด๋ณด์. ๐
from flask import Flask, render_template, jsonify
import sqlite3
from datetime import datetime, timedelta
app = Flask(__name__)
class NewsDashboard:
def __init__(self, db_path='news_summaries.db'):
self.db_path = db_path
def get_recent_summaries(self, limit=20):
"""์ต๊ทผ ์์ฝ ๊ฐ์ ธ์ค๊ธฐ"""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM news_summaries
ORDER BY summarized_at DESC
LIMIT ?
''', (limit,))
results = [dict(row) for row in cursor.fetchall()]
conn.close()
return results
def get_statistics(self, days=7):
"""ํต๊ณ ๋ฐ์ดํฐ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
since_date = (datetime.now() - timedelta(days=days)).isoformat()
# ์ด ๊ธฐ์ฌ ์
cursor.execute('''
SELECT COUNT(*) as total
FROM news_summaries
WHERE summarized_at > ?
''', (since_date,))
total_articles = cursor.fetchone()[0]
# ์ด ํ ํฐ ์ฌ์ฉ๋
cursor.execute('''
SELECT SUM(tokens_used) as total_tokens
FROM news_summaries
WHERE summarized_at > ?
''', (since_date,))
total_tokens = cursor.fetchone()[0] or 0
# ์ธ๋ก ์ฌ๋ณ ํต๊ณ
cursor.execute('''
SELECT press, COUNT(*) as count
FROM news_summaries
WHERE summarized_at > ?
GROUP BY press
ORDER BY count DESC
LIMIT 10
''', (since_date,))
press_stats = cursor.fetchall()
# ์ผ๋ณ ํต๊ณ
cursor.execute('''
SELECT DATE(summarized_at) as date, COUNT(*) as count
FROM news_summaries
WHERE summarized_at > ?
GROUP BY DATE(summarized_at)
ORDER BY date DESC
''', (since_date,))
daily_stats = cursor.fetchall()
conn.close()
return {
'total_articles': total_articles,
'total_tokens': total_tokens,
'press_stats': press_stats,
'daily_stats': daily_stats
}
dashboard = NewsDashboard()
@app.route('/')
def index():
"""๋ฉ์ธ ํ์ด์ง"""
summaries = dashboard.get_recent_summaries(20)
stats = dashboard.get_statistics(7)
return render_template('dashboard.html', summaries=summaries, stats=stats)
@app.route('/api/summaries')
def api_summaries():
"""API ์๋ํฌ์ธํธ"""
limit = request.args.get('limit', 20, type=int)
summaries = dashboard.get_recent_summaries(limit)
return jsonify(summaries)
@app.route('/api/stats')
def api_stats():
"""ํต๊ณ API"""
days = request.args.get('days', 7, type=int)
stats = dashboard.get_statistics(days)
return jsonify(stats)
if __name__ == '__main__':
app.run(debug=True, port=5000)
Flask๋ฅผ ์ฌ์ฉํ๋ฉด ์ ๋ง ๊ฐ๋จํ๊ฒ ์น ๋์๋ณด๋๋ฅผ ๋ง๋ค ์ ์์ด! ์ฌ๊ธฐ์ Chart.js๋ D3.js ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ์ง ๊ทธ๋ํ๋ ๊ทธ๋ฆด ์ ์์ง. ๐โจ
โข ์นดํ ๊ณ ๋ฆฌ ํํฐ: ์ ์น, ๊ฒฝ์ , ์ฌํ ๋ฑ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ํํฐ๋ง
โข ํค์๋ ํด๋ผ์ฐ๋: ์์ฃผ ๋ฑ์ฅํ๋ ํค์๋๋ฅผ ์๊ฐํ
โข ํธ๋ ๋ ๋ถ์: ์๊ฐ์ ๋ฐ๋ฅธ ์ฃผ์ ๋ณํ ์ถ์
โข ๊ฐ์ฑ ๋ถ์: ๊ธ์ /๋ถ์ ๋ด์ค ๋น์จ ํ์
๐ ๋ฐฐํฌ ๋ฐ ์ด์
์ด์ ์์ฑ๋ ๋ด์ ์ค์ ๋ก ๋ฐฐํฌํ๊ณ ์ด์ํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์! ๋ก์ปฌ์์๋ง ๋๋ฆฌ๋ ๊ฒ๊ณผ ์ค์ ์๋น์ค๋ก ์ด์ํ๋ ๊ฑด ์์ ํ ๋ค๋ฅธ ์ฐจ์์ด๊ฑฐ๋ . ๐
โ๏ธ ํด๋ผ์ฐ๋ ๋ฐฐํฌ ์ต์
Google Cloud Run: ์ปจํ ์ด๋ ๊ธฐ๋ฐ, ์๋ ์ค์ผ์ผ๋ง
Heroku: ๊ฐ๋จํ ๋ฐฐํฌ, ์ด๋ณด์ ์นํ์
DigitalOcean: ์ ๋ ดํ๊ณ ์ฌ์ฉํ๊ธฐ ์ฌ์
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด: ์ง์์ 24์๊ฐ ์ด์, ๋น์ฉ ์ ๊ฐ
# Docker๋ฅผ ์ฌ์ฉํ ๋ฐฐํฌ ์์
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
# ์์คํ
ํจํค์ง ์ค์น
RUN apt-get update && apt-get install -y \
chromium \
chromium-driver \
&& rm -rf /var/lib/apt/lists/*
# Python ํจํค์ง ์ค์น
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ๋ณต์ฌ
COPY . .
# ํ๊ฒฝ ๋ณ์ ์ค์
ENV PYTHONUNBUFFERED=1
# ๋ด ์คํ
CMD ["python", "news_bot.py"]
# docker-compose.yml
version: '3.8'
services:
news-bot:
build: .
container_name: news_automation_bot
restart: always
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
volumes:
- ./data:/app/data
- ./logs:/app/logs
networks:
- news-network
mongodb:
image: mongo:latest
container_name: news_mongodb
restart: always
ports:
- "27017:27017"
volumes:
- mongodb_data:/data/db
networks:
- news-network
dashboard:
build:
context: .
dockerfile: Dockerfile.dashboard
container_name: news_dashboard
restart: always
ports:
- "5000:5000"
depends_on:
- mongodb
networks:
- news-network
networks:
news-network:
driver: bridge
volumes:
mongodb_data:
Docker๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋ค ํ๊ฒฝ์์๋ ๋์ผํ๊ฒ ์คํํ ์ ์์ด์ ์ ๋ง ํธ๋ฆฌํด! ๊ฐ๋ฐ ํ๊ฒฝ๊ณผ ํ๋ก๋์ ํ๊ฒฝ์ ์ฐจ์ด๋ก ์ธํ ๋ฌธ์ ๋ฅผ ์์ ํ ์์จ ์ ์์ง. ๐ณ
๐ ๋ชจ๋ํฐ๋ง ๋ฐ ์๋ฆผ
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import requests
class NotificationManager:
def __init__(self, config):
self.config = config
def send_email(self, subject, body):
"""์ด๋ฉ์ผ ์๋ฆผ"""
try:
msg = MIMEMultipart()
msg['From'] = self.config['email']['from']
msg['To'] = self.config['email']['to']
msg['Subject'] = subject
msg.attach(MIMEText(body, 'html'))
server = smtplib.SMTP(self.config['email']['smtp_server'], 587)
server.starttls()
server.login(
self.config['email']['username'],
self.config['email']['password']
)
server.send_message(msg)
server.quit()
logging.info("๐ง ์ด๋ฉ์ผ ๋ฐ์ก ์๋ฃ")
except Exception as e:
logging.error(f"์ด๋ฉ์ผ ๋ฐ์ก ์คํจ: {e}")
def send_slack(self, message):
"""์ฌ๋ ์๋ฆผ"""
try:
webhook_url = self.config['slack']['webhook_url']
payload = {
'text': message,
'username': 'News Bot',
'icon_emoji': ':newspaper:'
}
response = requests.post(webhook_url, json=payload)
response.raise_for_status()
logging.info("๐ฌ ์ฌ๋ ๋ฉ์์ง ๋ฐ์ก ์๋ฃ")
except Exception as e:
logging.error(f"์ฌ๋ ๋ฐ์ก ์คํจ: {e}")
def send_telegram(self, message):
"""ํ
๋ ๊ทธ๋จ ์๋ฆผ"""
try:
bot_token = self.config['telegram']['bot_token']
chat_id = self.config['telegram']['chat_id']
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {
'chat_id': chat_id,
'text': message,
'parse_mode': 'HTML'
}
response = requests.post(url, json=payload)
response.raise_for_status()
logging.info("๐ฑ ํ
๋ ๊ทธ๋จ ๋ฉ์์ง ๋ฐ์ก ์๋ฃ")
except Exception as e:
logging.error(f"ํ
๋ ๊ทธ๋จ ๋ฐ์ก ์คํจ: {e}")
def format_summary_notification(self, summaries):
"""์์ฝ ์๋ฆผ ํฌ๋งทํ
"""
html = "<h2>๐ฐ ์ค๋์ ๋ด์ค ์์ฝ</h2>"
html += f"<p>์ด {len(summaries)}๊ฐ์ ๋ด์ค๊ฐ ์์ฝ๋์์ต๋๋ค.</p>"
html += "<hr>"
for i, summary in enumerate(summaries[:5], 1):
html += f"<h3>{i}. {summary['original_title']}</h3>"
html += f"<p>{summary['summary']}</p>"
html += f"<p><a href='{summary['original_link']}'>์๋ฌธ ๋ณด๊ธฐ</a></p>"
html += "<hr>"
return html
์๋ฆผ ์์คํ ์ ๊ตฌ์ถํ๋ฉด ๋ด์ด ์ ๋๋ก ๋์ํ๋์ง ์ค์๊ฐ์ผ๋ก ํ์ธํ ์ ์์ด. ํนํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ ๋ ์ฆ์ ์ ์ ์์ด์ ๋น ๋ฅด๊ฒ ๋์ํ ์ ์์ง! ๐
โก ์ฑ๋ฅ ์ต์ ํ ๋ฐ ๊ณ ๊ธ ๊ธฐ๋ฅ
๊ธฐ๋ณธ ๊ธฐ๋ฅ์ด ์์ฑ๋์๋ค๋ฉด, ์ด์ ์ฑ๋ฅ์ ์ต์ ํํ๊ณ ๊ณ ๊ธ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ณผ ์ฐจ๋ก์ผ! ๋ ๋น ๋ฅด๊ณ , ๋ ๋๋ํ๊ณ , ๋ ์ ์ฉํ ๋ด์ผ๋ก ๋ง๋ค์ด๋ณด์. ๐
โ๏ธ ์ฑ๋ฅ ์ต์ ํ ๊ธฐ๋ฒ
๋น๋๊ธฐ ์ฒ๋ฆฌ: ์ฌ๋ฌ ์์ ์ ๋์์ ์คํ
๋ฐฐ์น ์ฒ๋ฆฌ: ์ฌ๋ฌ ์์ฒญ์ ๋ฌถ์ด์ ์ฒ๋ฆฌ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ๋ฑ์ฑ: ์ฟผ๋ฆฌ ์๋ ๊ฐ์
๋ฆฌ์์ค ํ๋ง: ์ฐ๊ฒฐ ์ฌ์ฌ์ฉ์ผ๋ก ์ค๋ฒํค๋ ๊ฐ์
import asyncio
import aiohttp
from functools import lru_cache
import hashlib
class OptimizedNewsCrawler:
def __init__(self):
self.session = None
self.cache = {}
async def create_session(self):
"""๋น๋๊ธฐ ์ธ์
์์ฑ"""
if self.session is None:
self.session = aiohttp.ClientSession()
return self.session
@lru_cache(maxsize=1000)
def get_cache_key(self, url):
"""URL์ ์บ์ ํค๋ก ๋ณํ"""
return hashlib.md5(url.encode()).hexdigest()
async def fetch_with_cache(self, url, cache_ttl=3600):
"""์บ์๋ฅผ ํ์ฉํ ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ"""
cache_key = self.get_cache_key(url)
# ์บ์ ํ์ธ
if cache_key in self.cache:
cached_data, timestamp = self.cache[cache_key]
if time.time() - timestamp < cache_ttl:
logging.info(f"๐พ ์บ์ ํํธ: {url}")
return cached_data
# ์๋ก ๊ฐ์ ธ์ค๊ธฐ
session = await self.create_session()
async with session.get(url) as response:
html = await response.text()
self.cache[cache_key] = (html, time.time())
return html
async def fetch_multiple(self, urls):
"""์ฌ๋ฌ URL์ ๋์์ ๊ฐ์ ธ์ค๊ธฐ"""
tasks = [self.fetch_with_cache(url) for url in urls]
results = await asyncio.gather(*tasks, return_exceptions=True)
return results
async def parallel_crawl(self, news_sources, max_articles=10):
"""๋ณ๋ ฌ ํฌ๋กค๋ง"""
all_articles = []
for source in news_sources:
try:
html = await self.fetch_with_cache(source['url'])
articles = self.parse_news_list(html)
# ๊ฐ ๊ธฐ์ฌ์ ๋ณธ๋ฌธ์ ๋ณ๋ ฌ๋ก ๊ฐ์ ธ์ค๊ธฐ
article_urls = [a['link'] for a in articles[:max_articles]]
contents = await self.fetch_multiple(article_urls)
for article, content in zip(articles, contents):
if not isinstance(content, Exception):
article['content'] = self.extract_content(content)
all_articles.append(article)
except Exception as e:
logging.error(f"ํฌ๋กค๋ง ์ค๋ฅ: {e}")
return all_articles
async def close(self):
"""์ธ์
์ข
๋ฃ"""
if self.session:
await self.session.close()
# ์ฌ์ฉ ์์
async def main():
crawler = OptimizedNewsCrawler()
news_sources = [
{'url': 'https://news.example1.com'},
{'url': 'https://news.example2.com'},
{'url': 'https://news.example3.com'}
]
articles = await crawler.parallel_crawl(news_sources, max_articles=10)
await crawler.close()
return articles
# ์คํ
articles = asyncio.run(main())
๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ์ฌ์ฉํ๋ฉด ํฌ๋กค๋ง ์๋๊ฐ ์์ฒญ๋๊ฒ ๋นจ๋ผ์ ธ! 10๊ฐ์ ํ์ด์ง๋ฅผ ์์ฐจ์ ์ผ๋ก ๊ฐ์ ธ์ค๋ฉด 10์ด๊ฐ ๊ฑธ๋ฆฌ์ง๋ง, ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌํ๋ฉด 1-2์ด๋ฉด ์ถฉ๋ถํด. ์ด๊ฒ ๋ฐ๋ก ๋น๋๊ธฐ์ ๋ง๋ฒ์ด์ผ! โจ
๐ง ๊ณ ๊ธ AI ๊ธฐ๋ฅ
class AdvancedAIFeatures:
def __init__(self, openai_api_key):
self.api_key = openai_api_key
openai.api_key = openai_api_key
def sentiment_analysis(self, text):
"""๊ฐ์ฑ ๋ถ์"""
prompt = f"""๋ค์ ๋ด์ค ๊ธฐ์ฌ์ ๊ฐ์ฑ์ ๋ถ์ํด์ฃผ์ธ์.
๊ธฐ์ฌ: {text[:1000]}
๋ค์ ํ์์ผ๋ก ๋ต๋ณํด์ฃผ์ธ์:
- ์ ์ฒด ๊ฐ์ฑ: ๊ธ์ /์ค๋ฆฝ/๋ถ์
- ๊ฐ์ฑ ์ ์: -1.0 ~ 1.0
- ์ฃผ์ ๊ฐ์ : ๊ธฐ์จ, ๋ถ๋
ธ, ์ฌํ, ๋๋ผ์ ๋ฑ
- ์ด์ : ๊ฐ๋จํ ์ค๋ช
"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "๋น์ ์ ๊ฐ์ฑ ๋ถ์ ์ ๋ฌธ๊ฐ์
๋๋ค."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
def extract_entities(self, text):
"""๊ฐ์ฒด๋ช
์ธ์ (NER)"""
prompt = f"""๋ค์ ๋ด์ค์์ ์ฃผ์ ๊ฐ์ฒด๋ฅผ ์ถ์ถํด์ฃผ์ธ์.
๊ธฐ์ฌ: {text[:1000]}
JSON ํ์์ผ๋ก ๋ต๋ณ:
{{
"people": ["์ธ๋ฌผ1", "์ธ๋ฌผ2"],
"organizations": ["๊ธฐ๊ด1", "๊ธฐ๊ด2"],
"locations": ["์ฅ์1", "์ฅ์2"],
"dates": ["๋ ์ง1", "๋ ์ง2"],
"money": ["๊ธ์ก1", "๊ธ์ก2"]
}}"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
def categorize_news(self, title, summary):
"""๋ด์ค ์นดํ
๊ณ ๋ฆฌ ๋ถ๋ฅ"""
categories = [
"์ ์น", "๊ฒฝ์ ", "์ฌํ", "๋ฌธํ", "๊ตญ์ ",
"๊ณผํ๊ธฐ์ ", "์คํฌ์ธ ", "์ฐ์", "๊ฑด๊ฐ", "๊ต์ก"
]
prompt = f"""๋ค์ ๋ด์ค๋ฅผ ๊ฐ์ฅ ์ ํฉํ ์นดํ
๊ณ ๋ฆฌ๋ก ๋ถ๋ฅํด์ฃผ์ธ์.
์ ๋ชฉ: {title}
์์ฝ: {summary}
์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก: {', '.join(categories)}
๊ฐ์ฅ ์ ํฉํ ์นดํ
๊ณ ๋ฆฌ 1๊ฐ๋ง ๋ต๋ณํด์ฃผ์ธ์."""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=20
)
return response.choices[0].message.content.strip()
def generate_headline(self, summary):
"""์์ฝ์์ ํค๋๋ผ์ธ ์์ฑ"""
prompt = f"""๋ค์ ๋ด์ค ์์ฝ์ ๋ฐํ์ผ๋ก ๋งค๋ ฅ์ ์ธ ํค๋๋ผ์ธ์ ๋ง๋ค์ด์ฃผ์ธ์.
์์ฝ: {summary}
ํค๋๋ผ์ธ ์์ฑ ๊ท์น:
- 15-20์ ์ด๋ด
- ํต์ฌ ๋ด์ฉ ํฌํจ
- ํด๋ฆญ์ ์ ๋ํ๋ ํํ
- ๊ณผ์ฅ ๊ธ์ง
ํค๋๋ผ์ธ:"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=50
)
return response.choices[0].message.content.strip()
def compare_articles(self, articles):
"""์ฌ๋ฌ ๊ธฐ์ฌ ๋น๊ต ๋ถ์"""
articles_text = "\n\n".join([
f"๊ธฐ์ฌ {i+1}: {a['title']}\n{a['summary']}"
for i, a in enumerate(articles)
])
prompt = f"""๋ค์ ์ฌ๋ฌ ๋ด์ค ๊ธฐ์ฌ๋ค์ ๋น๊ต ๋ถ์ํด์ฃผ์ธ์.
{articles_text}
๋ถ์ ๋ด์ฉ:
1. ๊ณตํต ์ฃผ์
2. ๊ด์ ์ ์ฐจ์ด
3. ์ถ๊ฐ ์ ๋ณด๊ฐ ์๋ ๊ธฐ์ฌ
4. ๊ฐ์ฅ ๊ฐ๊ด์ ์ธ ๊ธฐ์ฌ
5. ์ข
ํฉ ์๊ฒฌ"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=800
)
return response.choices[0].message.content
์ด๋ฐ ๊ณ ๊ธ ๊ธฐ๋ฅ๋ค์ ์ถ๊ฐํ๋ฉด ๋จ์ํ ์์ฝ ๋ด์ ๋์ด์ ์ง์ง ๋๋ํ ๋ด์ค ๋ถ์ ์์คํ ์ด ๋ผ! ๊ฐ์ฑ ๋ถ์์ผ๋ก ๋ด์ค์ ๋ถ์๊ธฐ๋ฅผ ํ์ ํ๊ณ , ๊ฐ์ฒด๋ช ์ธ์์ผ๋ก ์ฃผ์ ์ธ๋ฌผ๊ณผ ๊ธฐ๊ด์ ์ถ์ถํ๊ณ , ์๋ ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅ๊น์ง... ์ ๋ง ๊ฐ๋ ฅํ์ง? ๐ช
๐ ๋ณด์ ๋ฐ ์ค๋ฆฌ์ ๊ณ ๋ ค์ฌํญ
๊ธฐ์ ์ ์ผ๋ก ์๋ฒฝํ ๋ด์ ๋ง๋ค์๋ค๊ณ ํด๋, ๋ณด์๊ณผ ์ค๋ฆฌ๋ฅผ ๋ฌด์ํ๋ฉด ํฐ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ด. ์ฑ ์๊ฐ ์๋ ๊ฐ๋ฐ์๊ฐ ๋๊ธฐ ์ํด ๊ผญ ์์์ผ ํ ๋ด์ฉ๋ค์ด์ผ! ๐ก๏ธ
โข ๋ด์ค ์ฝํ ์ธ ๋ ์ ์๊ถ์ผ๋ก ๋ณดํธ๋ฐ๋ ์ ์๋ฌผ์ด์ผ
โข ๋จ์ ๋งํฌ ๊ณต์ ๋ ๊ด์ฐฎ์ง๋ง, ์ ๋ฌธ ๋ณต์ ๋ ๋ฌธ์ ๊ฐ ๋ ์ ์์ด
โข ์์ฝ์ '๊ณต์ ์ด์ฉ(Fair Use)' ๋ฒ์ ๋ด์์ ๊ฐ๋ฅํ์ง๋ง, ์์ ์ ์ด์ฉ์ ์ฃผ์ํด์ผ ํด
โข ์ถ์ฒ๋ฅผ ๋ช ํํ ํ์ํ๊ณ , ์๋ฌธ ๋งํฌ๋ฅผ ํจ๊ป ์ ๊ณตํ๋ ๊ฒ ์ข์
๊ฐ์ธ์ ๋ณด ๋ณดํธ
โข API ํค, ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋น๋ฐ๋ฒํธ ๋ฑ์ ์ ๋ ๊ณต๊ฐํ๋ฉด ์ ๋ผ
โข ํ๊ฒฝ ๋ณ์๋ ๋น๋ฐ ๊ด๋ฆฌ ์๋น์ค๋ฅผ ์ฌ์ฉํด
โข ์์งํ ๋ฐ์ดํฐ์ ๊ฐ์ธ์ ๋ณด๊ฐ ํฌํจ๋์ด ์๋ค๋ฉด ์ ์ ํ ์ฒ๋ฆฌํด์ผ ํด
์น์ฌ์ดํธ ์ด์ฉ ๊ท์น
โข robots.txt๋ฅผ ํ์ธํ๊ณ ์ค์ํด
โข ๊ณผ๋ํ ์์ฒญ์ผ๋ก ์๋ฒ์ ๋ถ๋ด์ ์ฃผ์ง ๋ง
โข ์ด์ฉ ์ฝ๊ด์ ์๋ฐํ์ง ์๋๋ก ์ฃผ์ํด
import os
from cryptography.fernet import Fernet
import keyring
class SecureConfigManager:
def __init__(self):
self.cipher_key = self.get_or_create_key()
self.cipher = Fernet(self.cipher_key)
def get_or_create_key(self):
"""์ํธํ ํค ๊ฐ์ ธ์ค๊ธฐ ๋๋ ์์ฑ"""
key = keyring.get_password("news_bot", "cipher_key")
if key is None:
key = Fernet.generate_key().decode()
keyring.set_password("news_bot", "cipher_key", key)
return key.encode()
def encrypt_api_key(self, api_key):
"""API ํค ์ํธํ"""
return self.cipher.encrypt(api_key.encode()).decode()
def decrypt_api_key(self, encrypted_key):
"""API ํค ๋ณตํธํ"""
return self.cipher.decrypt(encrypted_key.encode()).decode()
def save_secure_config(self, config):
"""๋ณด์ ์ค์ ์ ์ฅ"""
# ๋ฏผ๊ฐํ ์ ๋ณด ์ํธํ
if 'openai_api_key' in config:
config['openai_api_key'] = self.encrypt_api_key(config['openai_api_key'])
if 'database_password' in config:
config['database_password'] = self.encrypt_api_key(config['database_password'])
# ์ํธํ๋ ์ค์ ์ ์ฅ
with open('config.encrypted.json', 'w') as f:
json.dump(config, f)
def load_secure_config(self):
"""๋ณด์ ์ค์ ๋ก๋"""
with open('config.encrypted.json', 'r') as f:
config = json.load(f)
# ๋ณตํธํ
if 'openai_api_key' in config:
config['openai_api_key'] = self.decrypt_api_key(config['openai_api_key'])
if 'database_password' in config:
config['database_password'] = self.decrypt_api_key(config['database_password'])
return config
class RateLimiter:
"""์์ฒญ ์๋ ์ ํ"""
def __init__(self, max_requests_per_minute=60):
self.max_requests = max_requests_per_minute
self.requests = []
def can_make_request(self):
"""์์ฒญ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ"""
now = time.time()
# 1๋ถ ์ด๋ด์ ์์ฒญ๋ง ์ ์ง
self.requests = [req_time for req_time in self.requests
if now - req_time < 60]
if len(self.requests) < self.max_requests:
self.requests.append(now)
return True
return False
def wait_if_needed(self):
"""ํ์์ ๋๊ธฐ"""
while not self.can_make_request():
time.sleep(1)
class EthicalCrawler:
"""์ค๋ฆฌ์ ํฌ๋กค๋ง์ ์ํ ํด๋์ค"""
def __init__(self, user_agent, respect_robots_txt=True):
self.user_agent = user_agent
self.respect_robots_txt = respect_robots_txt
self.rate_limiter = RateLimiter(max_requests_per_minute=30)
def check_robots_txt(self, url):
"""robots.txt ํ์ธ"""
if not self.respect_robots_txt:
return True
from urllib.robotparser import RobotFileParser
from urllib.parse import urlparse
parsed = urlparse(url)
robots_url = f"{parsed.scheme}://{parsed.netloc}/robots.txt"
rp = RobotFileParser()
rp.set_url(robots_url)
try:
rp.read()
return rp.can_fetch(self.user_agent, url)
except:
# robots.txt๋ฅผ ์ฝ์ ์ ์์ผ๋ฉด ํ์ฉ์ผ๋ก ๊ฐ์ฃผ
return True
def crawl_with_ethics(self, url):
"""์ค๋ฆฌ์ ํฌ๋กค๋ง"""
# robots.txt ํ์ธ
if not self.check_robots_txt(url):
logging.warning(f"robots.txt์ ์ํด ์ฐจ๋จ๋จ: {url}")
return None
# ์๋ ์ ํ
self.rate_limiter.wait_if_needed()
# ํฌ๋กค๋ง ์คํ
try:
response = requests.get(url, headers={'User-Agent': self.user_agent})
return response.text
except Exception as e:
logging.error(f"ํฌ๋กค๋ง ์ค๋ฅ: {e}")
return None
๋ณด์๊ณผ ์ค๋ฆฌ๋ ์ ํ์ด ์๋๋ผ ํ์์ผ! ํนํ ๋ค๋ฅธ ์ฌ๋๋ค์ด ์ฌ์ฉํ ์๋น์ค๋ฅผ ๋ง๋ ๋ค๋ฉด ๋์ฑ ์ ๊ฒฝ ์จ์ผ ํด. ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์๋น์ค๋ฅผ ์ ๊ณตํ ๋๋ ์ด๋ฐ ๋ถ๋ถ๋ค์ด ์ ๋ง ์ค์ํ๊ฑฐ๋ ! ๐
๐ผ ์ค์ ํ์ฉ ์ฌ๋ก ๋ฐ ์์ตํ
์ด์ ์์ฑ๋ ๋ด์ค์์ฝ ๋ด์ ์ด๋ป๊ฒ ํ์ฉํ๊ณ ์์ตํํ ์ ์๋์ง ์์๋ณด์! ๊ธฐ์ ๋ง ์๋ ๊ฒ๋ณด๋ค ์ค์ ๋ก ๊ฐ์น๋ฅผ ์ฐฝ์ถํ๋ ๊ฒ ๋ ์ค์ํ๋๊น! ๐ฐ
๋งค์ผ ์์นจ ๊ตฌ๋ ์๋ค์๊ฒ ๋ง์ถคํ ๋ด์ค ์์ฝ์ ์ด๋ฉ์ผ๋ก ๋ฐ์กํด. Substack์ด๋ Mailchimp๋ฅผ ํ์ฉํ๋ฉด ์ฝ๊ฒ ์์ํ ์ ์์ด.
2. ํ ๋ ๊ทธ๋จ/์ฌ๋ ๋ด
ํน์ ์ฃผ์ ๋ ํค์๋์ ๋ํ ๋ด์ค๋ฅผ ์ค์๊ฐ์ผ๋ก ์๋ ค์ฃผ๋ ๋ด์ ๋ง๋ค์ด. ๊ธฐ์ ์ด๋ ํ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์ง.
3. ์ ๊ณ๋ณ ๋ด์ค ํ๋ ์ด์
IT, ๊ธ์ต, ๋ถ๋์ฐ ๋ฑ ํน์ ์ฐ์ ์ ํนํ๋ ๋ด์ค ์์ฝ ์๋น์ค๋ฅผ ์ ๊ณตํด. ์ ๋ฌธ๊ฐ๋ค์ด ๊ธฐ๊บผ์ด ๋น์ฉ์ ์ง๋ถํ ๊ฑฐ์ผ.
4. ๊ธฐ์ ์ฉ ๋ฏธ๋์ด ๋ชจ๋ํฐ๋ง
๊ธฐ์ ์ ๋ธ๋๋๋ ๊ฒฝ์์ฌ์ ๋ํ ๋ด์ค๋ฅผ ์๋์ผ๋ก ๋ชจ๋ํฐ๋งํ๊ณ ๋ฆฌํฌํธ๋ฅผ ์์ฑํด์ค.
5. ์ฝํ ์ธ ํฌ๋ฆฌ์์ดํฐ ์ง์
์ ํ๋ฒ๋ ๋ธ๋ก๊ฑฐ๋ค์๊ฒ ํธ๋ ๋ ๋ด์ค ์ ๋ณด๋ฅผ ์ ๊ณตํด์ ์ฝํ ์ธ ์์ด๋์ด๋ฅผ ์ป์ ์ ์๊ฒ ๋์์ค.
๐ต ์์ตํ ์ ๋ต
| ์์ต ๋ชจ๋ธ | ์ค๋ช | ์์ ์์ต | ๋์ด๋ |
|---|---|---|---|
| ๊ตฌ๋ ์๋น์ค | ์ ์ ์ก์ ๋ก ํ๋ฆฌ๋ฏธ์ ๊ธฐ๋ฅ ์ ๊ณต | ์ 5,000~50,000์/์ธ | ์ค๊ฐ |
| API ํ๋งค | ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค์๊ฒ API ์ ๊ณต | ์ฌ์ฉ๋ ๊ธฐ๋ฐ | ๋์ |
| ๋ง์ถค ๊ฐ๋ฐ | ๊ธฐ์ ์ฉ ์ปค์คํฐ๋ง์ด์ง ์๋น์ค | ํ๋ก์ ํธ๋น 100๋ง์~ | ์ค๊ฐ |
| ๊ด๊ณ ์์ต | ๋ฌด๋ฃ ์๋น์ค์ ๊ด๊ณ ์ฝ์ | ํธ๋ํฝ ๊ธฐ๋ฐ | ๋ฎ์ |
| ๋ฐ์ดํฐ ํ๋งค | ์์ง๋ ๋ด์ค ๋ฐ์ดํฐ ํ๋งค | ๋ณ๋์ | ๋์ |
class NewsSubscriptionService:
"""๊ตฌ๋
์๋น์ค ๊ด๋ฆฌ"""
def __init__(self):
self.plans = {
'free': {
'name': '๋ฌด๋ฃ',
'price': 0,
'features': {
'daily_summaries': 5,
'categories': ['์ ์น', '๊ฒฝ์ '],
'email_delivery': True,
'api_access': False
}
},
'basic': {
'name': '๋ฒ ์ด์ง',
'price': 9900,
'features': {
'daily_summaries': 20,
'categories': 'all',
'email_delivery': True,
'api_access': False,
'custom_keywords': 5
}
},
'pro': {
'name': 'ํ๋ก',
'price': 29900,
'features': {
'daily_summaries': 'unlimited',
'categories': 'all',
'email_delivery': True,
'api_access': True,
'custom_keywords': 'unlimited',
'sentiment_analysis': True,
'priority_support': True
}
}
}
def check_user_limit(self, user_id, feature):
"""์ฌ์ฉ์ ์ ํ ํ์ธ"""
user_plan = self.get_user_plan(user_id)
plan_features = self.plans[user_plan]['features']
if feature in plan_features:
limit = plan_features[feature]
if limit == 'unlimited' or limit is True:
return True
# ์ฌ์ฉ๋ ํ์ธ
usage = self.get_user_usage(user_id, feature)
return usage < limit
return False
def generate_invoice(self, user_id, plan):
"""์ฒญ๊ตฌ์ ์์ฑ"""
user = self.get_user(user_id)
plan_info = self.plans[plan]
invoice = {
'user_id': user_id,
'user_email': user['email'],
'plan': plan,
'amount': plan_info['price'],
'billing_date': datetime.now().isoformat(),
'next_billing_date': (datetime.now() + timedelta(days=30)).isoformat()
}
return invoice
๊ตฌ๋ ์๋น์ค๋ฅผ ์ด์ํ๋ ค๋ฉด ๊ฒฐ์ ์์คํ ๋ ํ์ํด. Stripe๋ ํ ์คํ์ด๋จผ์ธ ๊ฐ์ ์๋น์ค๋ฅผ ์ฐ๋ํ๋ฉด ์ฝ๊ฒ ๊ตฌํํ ์ ์์ด. ๊ทธ๋ฆฌ๊ณ ์ฌ๋ฅ๋ท์์๋ ์ด๋ฐ ์๋ํ ์๋น์ค๋ฅผ ํ๋งคํ ์ ์์ผ๋, ํ๋ซํผ์ ํ์ฉํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๐ช
๐ ํ์ต ๋ฆฌ์์ค ๋ฐ ๋ค์ ๋จ๊ณ
์ฌ๊ธฐ๊น์ง ๋ฐ๋ผ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ํ์ง๋ง ํ์ต์ ์ฌ๊ธฐ์ ๋์ด ์๋์ผ. ๊ณ์ ๋ฐ์ ํ๊ณ ๋ ๋์ ๊ฐ๋ฐ์๊ฐ ๋๊ธฐ ์ํ ๋ฆฌ์์ค๋ค์ ์๊ฐํ ๊ฒ! ๐
โข OpenAI API Documentation: platform.openai.com/docs
โข Beautiful Soup Documentation: www.crummy.com/software/BeautifulSoup/bs4/doc/
โข Selenium Documentation: selenium-python.readthedocs.io
์จ๋ผ์ธ ๊ฐ์
โข "Web Scraping with Python" - Udemy
โข "OpenAI API ๋ง์คํฐํ๊ธฐ" - ์ธํ๋ฐ
โข "Python ์๋ํ ์์ ์ ๋ณต" - ๋ ธ๋ง๋์ฝ๋
์ปค๋ฎค๋ํฐ
โข Reddit r/webscraping
โข Stack Overflow
โข ์ํ์ฝ๋ฉ ์ปค๋ฎค๋ํฐ
โข ๊ฐ๋ฐ์ ์นดํ (๋ค์ด๋ฒ, ๋์ค์ฝ๋)
๐ ๋ค์ ๋จ๊ณ ํ๋ก์ ํธ ์์ด๋์ด
โ ๋ค๊ตญ์ด ๋ด์ค ์ง์ (๋ฒ์ญ ๊ธฐ๋ฅ ์ถ๊ฐ)
โ ์์ฑ ์์ฝ (TTS ์ฐ๋)
โ ์ด๋ฏธ์ง ๋ถ์ (๋ด์ค ์ฌ์ง ์ค๋ช )
โ ํฉํธ์ฒดํฌ ๊ธฐ๋ฅ
โ ๊ด๋ จ ๋ด์ค ์ถ์ฒ
โ ๋ฉํฐ๋ชจ๋ฌ ๋ถ์ (ํ ์คํธ + ์ด๋ฏธ์ง)
โ ํธ๋ ๋ ์์ธก
โ ๊ฐ์ง๋ด์ค ํ์ง
โ ์๋ ํ์บ์คํธ ์์ฑ
โ ์ธํฐ๋ํฐ๋ธ ์ฑ๋ด
โ ๋ชจ๋ฐ์ผ ์ฑ ๊ฐ๋ฐ
โ ์์ ๋ฏธ๋์ด ์ฐ๋
โ ์ปค๋ฎค๋ํฐ ๊ธฐ๋ฅ
โ ๋ง์ผํ๋ ์ด์ค
โ ํ์ดํธ๋ผ๋ฒจ ์๋ฃจ์
# ๋ค๊ตญ์ด ์ง์ ์์
from googletrans import Translator
class MultilingualNewsBot:
def __init__(self):
self.translator = Translator()
self.supported_languages = ['ko', 'en', 'ja', 'zh-cn', 'es']
def translate_summary(self, summary, target_lang='en'):
"""์์ฝ ๋ฒ์ญ"""
try:
result = self.translator.translate(summary, dest=target_lang)
return result.text
except Exception as e:
logging.error(f"๋ฒ์ญ ์ค๋ฅ: {e}")
return summary
def multilingual_summary(self, article):
"""๋ค๊ตญ์ด ์์ฝ ์์ฑ"""
# ํ๊ตญ์ด๋ก ์์ฝ
korean_summary = self.summarize_article(article)
# ์ฌ๋ฌ ์ธ์ด๋ก ๋ฒ์ญ
translations = {}
for lang in self.supported_languages:
if lang != 'ko':
translations[lang] = self.translate_summary(korean_summary, lang)
return {
'ko': korean_summary,
**translations
}
# ์์ฑ ์์ฝ ์์
from gtts import gTTS
import os
class VoiceNewsBot:
def __init__(self):
self.output_dir = 'voice_summaries'
os.makedirs(self.output_dir, exist_ok=True)
def text_to_speech(self, text, filename, lang='ko'):
"""ํ
์คํธ๋ฅผ ์์ฑ์ผ๋ก ๋ณํ"""
try:
tts = gTTS(text=text, lang=lang, slow=False)
filepath = os.path.join(self.output_dir, filename)
tts.save(filepath)
logging.info(f"๐ ์์ฑ ํ์ผ ์์ฑ: {filepath}")
return filepath
except Exception as e:
logging.error(f"TTS ์ค๋ฅ: {e}")
return None
def create_news_podcast(self, summaries):
"""๋ด์ค ํ์บ์คํธ ์์ฑ"""
intro = "์๋
ํ์ธ์. ์ค๋์ ์ฃผ์ ๋ด์ค๋ฅผ ์ ํด๋๋ฆฝ๋๋ค."
full_script = intro + "\n\n"
for i, summary in enumerate(summaries, 1):
full_script += f"{i}๋ฒ์งธ ๋ด์ค. {summary['original_title']}. "
full_script += f"{summary['summary']}\n\n"
outro = "์ด์ ์ค๋์ ๋ด์ค์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค."
full_script += outro
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"news_podcast_{timestamp}.mp3"
return self.text_to_speech(full_script, filename)
์ด๋ฐ ๊ณ ๊ธ ๊ธฐ๋ฅ๋ค์ ํ๋์ฉ ์ถ๊ฐํ๋ค ๋ณด๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ๋ด์ค ํ๋ซํผ์ ๋ง๋ค ์ ์์ด! ์ค์ํ ๊ฑด ํ ๋ฒ์ ๋ค ํ๋ ค๊ณ ํ์ง ๋ง๊ณ , ํ๋์ฉ ์ฐจ๊ทผ์ฐจ๊ทผ ๊ตฌํํ๋ ๊ฑฐ์ผ. ๐ฏ
๐ ํธ๋ฌ๋ธ์ํ ๊ฐ์ด๋
๊ฐ๋ฐํ๋ค ๋ณด๋ฉด ๋ฐ๋์ ๋ฌธ์ ๊ฐ ์๊ฒจ. ์ฌ๊ธฐ ์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋์ด! ๐ง
์์ธ: API ํธ์ถ ํ์ ์ ํ ์ด๊ณผ
ํด๊ฒฐ: ์์ฒญ ๊ฐ ๋๋ ์ด ์ถ๊ฐ, ๋ฐฐ์น ํฌ๊ธฐ ์ค์ด๊ธฐ, ์ ๋ฃ ํ๋ ์ ๊ทธ๋ ์ด๋
๋ฌธ์ 2: "Connection timeout"
์์ธ: ๋คํธ์ํฌ ๋ถ์์ ๋๋ ์๋ฒ ์๋ต ์ง์ฐ
ํด๊ฒฐ: timeout ๊ฐ ์ฆ๊ฐ, ์ฌ์๋ ๋ก์ง ์ถ๊ฐ, ํ๋ก์ ์ฌ์ฉ
๋ฌธ์ 3: "Element not found"
์์ธ: ์น์ฌ์ดํธ ๊ตฌ์กฐ ๋ณ๊ฒฝ ๋๋ ๋์ ๋ก๋ฉ
ํด๊ฒฐ: ์ ๋ ํฐ ์ ๋ฐ์ดํธ, ๋๊ธฐ ์๊ฐ ์ถ๊ฐ, ๋์ ํฌ๋กค๋ง ์ฌ์ฉ
๋ฌธ์ 4: "IP blocked"
์์ธ: ๊ณผ๋ํ ์์ฒญ์ผ๋ก IP ์ฐจ๋จ
ํด๊ฒฐ: ์์ฒญ ๊ฐ๊ฒฉ ๋๋ฆฌ๊ธฐ, ํ๋ก์ ๋กํ ์ด์ , User-Agent ๋ณ๊ฒฝ
๋ฌธ์ 5: "Out of memory"
์์ธ: ๋๋์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
ํด๊ฒฐ: ๋ฐฐ์น ์ฒ๋ฆฌ, ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์ ์ฌ์ฉ, ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
class RobustNewsCrawler:
"""์์ ์ ์ธ ํฌ๋กค๋ฌ (์๋ฌ ์ฒ๋ฆฌ ๊ฐํ)"""
def __init__(self, max_retries=3):
self.max_retries = max_retries
def fetch_with_retry(self, url, retry_count=0):
"""์ฌ์๋ ๋ก์ง์ด ์๋ ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ"""
try:
response = requests.get(
url,
headers={'User-Agent': self.get_random_user_agent()},
timeout=10
)
response.raise_for_status()
return response.text
except requests.Timeout:
if retry_count < self.max_retries:
logging.warning(f"ํ์์์ ๋ฐ์. ์ฌ์๋ {retry_count + 1}/{self.max_retries}")
time.sleep(2 ** retry_count) # ์ง์ ๋ฐฑ์คํ
return self.fetch_with_retry(url, retry_count + 1)
else:
logging.error(f"์ต๋ ์ฌ์๋ ํ์ ์ด๊ณผ: {url}")
return None
except requests.HTTPError as e:
if e.response.status_code == 429: # Too Many Requests
wait_time = int(e.response.headers.get('Retry-After', 60))
logging.warning(f"Rate limit. {wait_time}์ด ๋๊ธฐ")
time.sleep(wait_time)
return self.fetch_with_retry(url, retry_count + 1)
else:
logging.error(f"HTTP ์ค๋ฅ {e.response.status_code}: {url}")
return None
except Exception as e:
logging.error(f"์์์น ๋ชปํ ์ค๋ฅ: {e}")
return None
def get_random_user_agent(self):
"""๋๋ค User-Agent ๋ฐํ"""
user_agents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
]
return random.choice(user_agents)
def safe_parse(self, html, selector):
"""์์ ํ ํ์ฑ"""
try:
soup = BeautifulSoup(html, 'html.parser')
elements = soup.select(selector)
return elements if elements else []
except Exception as e:
logging.error(f"ํ์ฑ ์ค๋ฅ: {e}")
return []
# ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ธ ์ฒ๋ฆฌ
def process_large_dataset(articles, batch_size=10):
"""๋์ฉ๋ ๋ฐ์ดํฐ ๋ฐฐ์น ์ฒ๋ฆฌ"""
for i in range(0, len(articles), batch_size):
batch = articles[i:i + batch_size]
# ๋ฐฐ์น ์ฒ๋ฆฌ
process_batch(batch)
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
import gc
gc.collect()
logging.info(f"์ฒ๋ฆฌ ์๋ฃ: {i + len(batch)}/{len(articles)}")
์๋ฌ ์ฒ๋ฆฌ๋ฅผ ์ ํด๋๋ฉด ๋ด์ด ํจ์ฌ ์์ ์ ์ผ๋ก ๋์ํด. ํนํ 24์๊ฐ ๋์๊ฐ๋ ์๋ํ ์์คํ ์์๋ ์๋ฌ ์ฒ๋ฆฌ๊ฐ ์ ๋ง ์ค์ํ๊ฑฐ๋ ! ๐ช
๐ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ ๋ง ๊ธด ์ฌ์ ์ด์์ง? ์ฐ๋ฆฌ๋ OpenAI API์ ํฌ๋กค๋ฌ๋ฅผ ๊ฒฐํฉํด์ ์์ ํ ๋ด์ค์์ฝ ์๋๋ด์ ๋ง๋ค์ด๋ดค์ด. ์น ํฌ๋กค๋ง๋ถํฐ AI ์์ฝ, ์๋ํ, ๋ฐฐํฌ, ์์ตํ๊น์ง... ์ ๋ง ๋ง์ ๊ฑธ ๋ค๋ค์ง! ๐
์ด ํ๋ก์ ํธ๋ฅผ ํตํด ๋ฐฐ์ด ๊ธฐ์ ๋ค์ ๋จ์ํ ๋ด์ค ๋ด์๋ง ์ ์ฉ๋๋ ๊ฒ ์๋์ผ. ์น ํฌ๋กค๋ง, API ์ฐ๋, ์๋ํ ์์คํ
๊ตฌ์ถ ๊ฐ์ ์คํฌ๋ค์ ์ ๋ง ๋ค์ํ ํ๋ก์ ํธ์ ํ์ฉํ ์ ์์ด. ์๋ฅผ ๋ค๋ฉด:
โข ๐ ๋ถ๋์ฐ ๋งค๋ฌผ ์๋ ์์ง ์์คํ
โข ๐ ์ผํ๋ชฐ ๊ฐ๊ฒฉ ๋น๊ต ์๋น์ค
โข ๐ ๋ธ๋ก๊ทธ ์๋ ํฌ์คํ ๋๊ตฌ
โข ๐ง ์ด๋ฉ์ผ ์๋ ๋ถ๋ฅ ๋ฐ ์์ฝ
โข ๐ฌ ์ํ/๋๋ผ๋ง ๋ฆฌ๋ทฐ ์์ง ๋ฐ ๋ถ์
โข ๐ฑ ์์ ๋ฏธ๋์ด ํธ๋ ๋ ๋ถ์
โข ๐ ์จ๋ผ์ธ ๊ฐ์ ์๋ ์์ฝ
๊ฐ์ฅ ์ค์ํ ๊ฑด ์ง์ ๋ง๋ค์ด๋ณด๋ ๊ฒ์ด์ผ! ์ด ๊ธ์ ์ฝ๊ธฐ๋ง ํ์ง ๋ง๊ณ , ์ค์ ๋ก ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์คํํด๋ณด๋ฉด์ ๋ฐฐ์์ผ ํด. ์ฒ์์ ์๋ฌ๋ ๋ง์ด ๋๊ณ ๋งํ๋ ๋ถ๋ถ๋ ์๊ฒ ์ง๋ง, ๊ทธ๊ฒ ๋ฐ๋ก ์ฑ์ฅํ๋ ๊ณผ์ ์ด์ผ! ๐ช
๊ทธ๋ฆฌ๊ณ ํผ์์ ๋ค ํ๋ ค๊ณ ํ์ง ๋ง. ๋งํ๋ฉด ์ปค๋ฎค๋ํฐ์ ์ง๋ฌธํ๊ณ , ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ ์ฐธ๊ณ ํ๊ณ , ํ์
๋ ํด๋ด. ๊ฐ๋ฐ์ ํผ์ ํ๋ ๊ฒ ์๋๋ผ ํจ๊ป ํ๋ ๊ฑฐ๋๊น! ๐ค
๋ง์ง๋ง์ผ๋ก, ์ด๋ฐ ์๋ํ ๊ธฐ์ ์ ์ตํ๋๋ฉด ์ ๋ง ๋ง์ ๊ธฐํ๊ฐ ์๊ฒจ. ํ๋ฆฌ๋์๋ก ์ผํ ์๋ ์๊ณ , ์์ ๋ง์ ์๋น์ค๋ฅผ ๋ง๋ค ์๋ ์๊ณ , ๊ธฐ์
์์ ๋์ ํ๊ฐ๋ฅผ ๋ฐ์ ์๋ ์์ด. ํนํ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ด๋ฐ ๊ธฐ์ ์ ํ์ฉํ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ฉด ์ข์ ์์ต๋ ์ป์ ์ ์์ง! ๐ผ
์, ์ด์ ๋น์ ์ฐจ๋ก์ผ! ์ด ๊ฐ์ด๋๋ฅผ ๋ฐํ์ผ๋ก ์์ ๋ง์ ๋ฉ์ง ๋ด์ค์์ฝ ๋ด์ ๋ง๋ค์ด๋ด. ๊ทธ๋ฆฌ๊ณ ๋ ๋์๊ฐ์ ์ธ์์ ์๋ ์๋ก์ด ์๋น์ค๋ฅผ ๋ง๋ค์ด๋ณด๋ ๊ฑด ์ด๋? ๋น์ ์ด๋ผ๋ฉด ์ถฉ๋ถํ ํ ์ ์์ด! ๐
ํ์ด์ ๋น์ด! ๊ทธ๋ฆฌ๊ณ ๋ฉ์ง ํ๋ก์ ํธ๋ฅผ ๋ง๋ค๋ฉด ๊ผญ ๊ณต์ ํด์ค. ๋ค๋ฅธ ์ฌ๋๋ค์๊ฒ๋ ์๊ฐ์ ์ค ์ ์์ ํ
๋๊น! ํ์ดํ
! ๐
โข ๋ธ๋ก๊ทธ์ ๊ฐ๋ฐ ๊ณผ์ ์ ๊ธฐ๋กํ๋ฉด ๋์ค์ ํฐ ์์ฐ์ด ๋ผ
โข ์คํ์์ค๋ก ๊ณต๊ฐํ๋ฉด ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค๊ณผ ํ์ ํ ์ ์์ด
โข ์ ๊ธฐ์ ์ผ๋ก ์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ๊ณ ๊ฐ์ ํด
โข ์๋ก์ด ๊ธฐ์ ์ด ๋์ค๋ฉด ์ ๊ทน์ ์ผ๋ก ์๋ํด๋ด
โข ๋ฌด์๋ณด๋ค ์ฆ๊ธฐ๋ฉด์ ๊ฐ๋ฐํ๋ ๊ฒ ๊ฐ์ฅ ์ค์ํด! ๐
๐ ์ฌ๊ธฐ๊น์ง ์ฝ์ด์ฃผ์ ์ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค!
์ด์ ๋น์ ๋ AI ์๋ํ ์ ๋ฌธ๊ฐ์ ๊ธธ๋ก ํ ๊ฑธ์ ๋ ๋์๊ฐ์ด์.
๊ณ์ํด์ ๋ฐฐ์ฐ๊ณ , ๋ง๋ค๊ณ , ๊ณต์ ํ์ธ์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.