Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -70,18 +70,25 @@ class MyClient(discord.Client):
|
|
70 |
try:
|
71 |
video_id = extract_video_id(message.content)
|
72 |
if video_id:
|
73 |
-
transcript = await get_best_available_transcript(video_id)
|
74 |
comments = await get_video_comments(video_id)
|
75 |
-
if comments
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
78 |
else:
|
79 |
-
await message.channel.send("
|
80 |
else:
|
81 |
await message.channel.send("์ ํจํ ์ ํ๋ธ ๋น๋์ค URL์ ์ ๊ณตํด ์ฃผ์ธ์.")
|
82 |
finally:
|
83 |
self.is_processing = False
|
84 |
|
|
|
|
|
85 |
def is_message_in_specific_channel(self, message):
|
86 |
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
87 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
@@ -106,34 +113,43 @@ def extract_video_id(url):
|
|
106 |
logging.debug(f'์ถ์ถ๋ ๋น๋์ค ID: {video_id}')
|
107 |
return video_id
|
108 |
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
for attempt in range(max_retries):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
try:
|
112 |
-
|
|
|
|
|
|
|
113 |
except Exception as e:
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
logging.
|
119 |
-
|
120 |
-
transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
|
121 |
-
transcript = transcripts.find_manually_created_transcript().fetch()
|
122 |
-
except Exception as e:
|
123 |
-
if attempt < max_retries - 1:
|
124 |
-
logging.error(f'๋์ฒด ์๋ง ๊ฐ์ ธ์ค๊ธฐ ์ค๋ฅ: {e}')
|
125 |
-
await asyncio.sleep(delay)
|
126 |
-
continue
|
127 |
-
else:
|
128 |
-
logging.error(f'๋ชจ๋ ์๋ง ๊ฐ์ ธ์ค๊ธฐ ์๋ ์คํจ: {e}')
|
129 |
-
return None
|
130 |
|
131 |
-
|
132 |
-
transcript_text = formatter.format_transcript(transcript)
|
133 |
-
logging.debug(f'๊ฐ์ ธ์จ ์๋ง: {transcript_text}')
|
134 |
-
return transcript_text
|
135 |
|
136 |
-
return None
|
137 |
|
138 |
async def get_video_comments(video_id):
|
139 |
comments = []
|
|
|
70 |
try:
|
71 |
video_id = extract_video_id(message.content)
|
72 |
if video_id:
|
73 |
+
transcript, language = await get_best_available_transcript(video_id)
|
74 |
comments = await get_video_comments(video_id)
|
75 |
+
if comments:
|
76 |
+
if transcript:
|
77 |
+
replies = await generate_replies(comments, transcript)
|
78 |
+
await create_thread_and_send_replies(message, video_id, comments, replies, self.session)
|
79 |
+
else:
|
80 |
+
await message.channel.send("์๋ง์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ๋๊ธ๋ง์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๋ณ์ ์์ฑํฉ๋๋ค.")
|
81 |
+
replies = await generate_replies(comments, "")
|
82 |
+
await create_thread_and_send_replies(message, video_id, comments, replies, self.session)
|
83 |
else:
|
84 |
+
await message.channel.send("๋๊ธ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.")
|
85 |
else:
|
86 |
await message.channel.send("์ ํจํ ์ ํ๋ธ ๋น๋์ค URL์ ์ ๊ณตํด ์ฃผ์ธ์.")
|
87 |
finally:
|
88 |
self.is_processing = False
|
89 |
|
90 |
+
|
91 |
+
|
92 |
def is_message_in_specific_channel(self, message):
|
93 |
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
94 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
|
|
113 |
logging.debug(f'์ถ์ถ๋ ๋น๋์ค ID: {video_id}')
|
114 |
return video_id
|
115 |
|
116 |
+
import asyncio
|
117 |
+
import aiohttp
|
118 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
119 |
+
|
120 |
+
async def get_best_available_transcript(video_id, max_retries=5, delay=10):
|
121 |
+
async def fetch_transcript(language):
|
122 |
+
try:
|
123 |
+
transcript = await asyncio.to_thread(YouTubeTranscriptApi.get_transcript, video_id, languages=[language])
|
124 |
+
return transcript, language
|
125 |
+
except Exception as e:
|
126 |
+
logging.warning(f'{language} ์๋ง ๊ฐ์ ธ์ค๊ธฐ ์ค๋ฅ: {e}')
|
127 |
+
return None, None
|
128 |
+
|
129 |
for attempt in range(max_retries):
|
130 |
+
ko_transcript, ko_lang = await fetch_transcript('ko')
|
131 |
+
if ko_transcript:
|
132 |
+
return ko_transcript, ko_lang
|
133 |
+
|
134 |
+
en_transcript, en_lang = await fetch_transcript('en')
|
135 |
+
if en_transcript:
|
136 |
+
return en_transcript, en_lang
|
137 |
+
|
138 |
try:
|
139 |
+
transcripts = await asyncio.to_thread(YouTubeTranscriptApi.list_transcripts, video_id)
|
140 |
+
manual_transcript = await asyncio.to_thread(transcripts.find_manually_created_transcript)
|
141 |
+
transcript = await asyncio.to_thread(manual_transcript.fetch)
|
142 |
+
return transcript, manual_transcript.language_code
|
143 |
except Exception as e:
|
144 |
+
if attempt < max_retries - 1:
|
145 |
+
logging.error(f'๋ชจ๋ ์๋ง ๊ฐ์ ธ์ค๊ธฐ ์๋ ์คํจ (์๋ {attempt + 1}/{max_retries}): {e}')
|
146 |
+
await asyncio.sleep(delay)
|
147 |
+
else:
|
148 |
+
logging.error(f'์ต์ข
์๋ง ๊ฐ์ ธ์ค๊ธฐ ์คํจ: {e}')
|
149 |
+
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
+
return None, None
|
|
|
|
|
|
|
152 |
|
|
|
153 |
|
154 |
async def get_video_comments(video_id):
|
155 |
comments = []
|