Create youtube_worksheet.py
Browse files- youtube_worksheet.py +64 -0
youtube_worksheet.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
2 |
+
import google.generativeai as genai
|
3 |
+
from docx import Document
|
4 |
+
import re
|
5 |
+
|
6 |
+
class YouTubeWorksheet:
|
7 |
+
def __init__(self, api_key):
|
8 |
+
# Gemini API ์ด๊ธฐํ
|
9 |
+
genai.configure(api_key=api_key)
|
10 |
+
self.model = genai.GenerativeModel('gemini-1.5-pro')
|
11 |
+
|
12 |
+
def get_video_id(self, url):
|
13 |
+
# YouTube URL์์ video ID ์ถ์ถ
|
14 |
+
video_id = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url)
|
15 |
+
return video_id.group(1) if video_id else None
|
16 |
+
|
17 |
+
def get_transcript(self, url):
|
18 |
+
video_id = self.get_video_id(url)
|
19 |
+
if not video_id:
|
20 |
+
return None
|
21 |
+
try:
|
22 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
23 |
+
return ' '.join([entry['text'] for entry in transcript])
|
24 |
+
except Exception as e:
|
25 |
+
print(f"์๋ง ์ถ์ถ ์ค๋ฅ: {e}")
|
26 |
+
return None
|
27 |
+
|
28 |
+
def create_worksheet(self, transcript):
|
29 |
+
prompt = f"""
|
30 |
+
๋ค์ ํ
์คํธ๋ฅผ ๋ฌธ์ฅ๋ณ๋ก ๋๋๊ณ , ๊ฐ ๋ฌธ์ฅ์ ๋ํด:
|
31 |
+
1. ๋น์นธ ๋ฌธ์ ๋ง๋ค๊ธฐ (์ค์ ๋จ์ด๋ฅผ ___๋ก ๋์ฒด)
|
32 |
+
2. ํ๊ตญ์ด๋ก ๋ฒ์ญํ๊ธฐ
|
33 |
+
|
34 |
+
ํ
์คํธ: {transcript}
|
35 |
+
|
36 |
+
ํ ํ์์ผ๋ก ์ถ๋ ฅ:
|
37 |
+
์๋ฌธ์ฅ|๋น์นธ ๋ฌธ์ |ํ๊ตญ์ด ๋ฒ์ญ
|
38 |
+
"""
|
39 |
+
|
40 |
+
response = self.model.generate_content(prompt)
|
41 |
+
return response.text
|
42 |
+
|
43 |
+
def save_to_docx(self, content, output_file="worksheet.docx"):
|
44 |
+
doc = Document()
|
45 |
+
doc.add_heading('YouTube ํ์ต ํ๋์ง', 0)
|
46 |
+
|
47 |
+
# ํ ์์ฑ ๋ฐ ๋ด์ฉ ์ถ๊ฐ
|
48 |
+
rows = content.strip().split('\n')[2:] # ํค๋ ์ ์ธ
|
49 |
+
table = doc.add_table(rows=len(rows)+1, cols=3)
|
50 |
+
table.style = 'Table Grid'
|
51 |
+
|
52 |
+
# ํค๋ ์ถ๊ฐ
|
53 |
+
headers = ['์๋ฌธ์ฅ', '๋น์นธ ๋ฌธ์ ', 'ํ๊ตญ์ด ๋ฒ์ญ']
|
54 |
+
for i, header in enumerate(headers):
|
55 |
+
table.cell(0, i).text = header
|
56 |
+
|
57 |
+
# ๋ด์ฉ ์ถ๊ฐ
|
58 |
+
for i, row in enumerate(rows):
|
59 |
+
cells = row.split('|')
|
60 |
+
for j, cell in enumerate(cells):
|
61 |
+
table.cell(i+1, j).text = cell.strip()
|
62 |
+
|
63 |
+
doc.save(output_file)
|
64 |
+
return output_file
|