seawolf2357 commited on
Commit
fa5c0c1
ยท
verified ยท
1 Parent(s): 7524905

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
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 and transcript:
76
- replies = await generate_replies(comments, transcript)
77
- await create_thread_and_send_replies(message, video_id, comments, replies, self.session)
 
 
 
 
 
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
- async def get_best_available_transcript(video_id, max_retries=3, delay=5):
 
 
 
 
 
 
 
 
 
 
 
 
110
  for attempt in range(max_retries):
 
 
 
 
 
 
 
 
111
  try:
112
- transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
 
 
 
113
  except Exception as e:
114
- logging.warning(f'ํ•œ๊ตญ์–ด ์ž๋ง‰ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜: {e}')
115
- try:
116
- transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
117
- except Exception as e:
118
- logging.warning(f'์˜์–ด ์ž๋ง‰ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜: {e}')
119
- try:
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
- formatter = TextFormatter()
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 = []