seawolf2357 commited on
Commit
9a9804f
β€’
1 Parent(s): 56d2287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -6,7 +6,7 @@ import asyncio
6
  import aiohttp
7
  from huggingface_hub import InferenceClient
8
  from googleapiclient.discovery import build
9
- from youtube_transcript_api import YouTubeTranscriptApi
10
  from youtube_transcript_api.formatters import TextFormatter
11
  from dotenv import load_dotenv
12
  import json
@@ -98,9 +98,12 @@ async def get_best_available_transcript(video_id):
98
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
99
  formatter = TextFormatter()
100
  return formatter.format_transcript(transcript)
 
 
 
101
  except Exception as e:
102
- logging.error(f"Failed to retrieve transcript: {e}")
103
- return None
104
 
105
  async def get_video_comments(video_id):
106
  try:
@@ -133,7 +136,7 @@ async def generate_reply(comment, transcript):
133
  return response_json.get("generated_text", "").strip()
134
  except json.JSONDecodeError:
135
  logging.error(f"Failed to decode JSON: {response}")
136
- return "닡변을 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
137
  except Exception as e:
138
  logging.error(f"Error generating reply: {e}")
139
  return "닡변을 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
@@ -144,25 +147,26 @@ async def create_thread_and_send_reply(message, video_id, comment, reply):
144
  embed = discord.Embed(description=f"**λŒ“κΈ€**: {comment[0]}\n**λ‹΅κΈ€**: {reply}")
145
  await thread.send(embed=embed)
146
  webhook_data = {"video_id": video_id, "replies": [{"comment": comment[0], "reply": reply, "comment_id": comment[1]}]}
147
- await send_webhook_data(webhook_data, self.session)
148
  except discord.HTTPException as e:
149
  if e.code == 160004:
150
  logging.error("A thread has already been created for this message")
151
  else:
152
  logging.error(f"Error in thread creation and reply sending: {e}")
153
 
154
- async def send_webhook_data(data, session):
155
- data_json = json.dumps(data)
156
- for i in range(0, len(data_json), MAX_CHUNK_SIZE):
157
- chunk = data_json[i:i+MAX_CHUNK_SIZE]
158
- try:
159
- async with session.post(WEBHOOK_URL, json=json.loads(chunk)) as response:
160
- if response.status == 200:
161
- logging.info("Webhook data sent successfully.")
162
- else:
163
- logging.error(f"Failed to send webhook data: HTTP {response.status}")
164
- except aiohttp.ClientError as e:
165
- logging.error(f"HTTP error occurred while sending webhook data: {e}")
 
166
 
167
  if __name__ == "__main__":
168
  discord_client = MyClient(intents=intents)
 
6
  import aiohttp
7
  from huggingface_hub import InferenceClient
8
  from googleapiclient.discovery import build
9
+ from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound
10
  from youtube_transcript_api.formatters import TextFormatter
11
  from dotenv import load_dotenv
12
  import json
 
98
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
99
  formatter = TextFormatter()
100
  return formatter.format_transcript(transcript)
101
+ except NoTranscriptFound:
102
+ logging.error(f"No transcript found for video ID {video_id}")
103
+ return "μžλ§‰μ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."
104
  except Exception as e:
105
+ logging.error(f"Failed to retrieve transcript for video ID {video_id}: {e}")
106
+ return "μžλ§‰μ„ μΆ”μΆœν•˜λŠ” 데 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."
107
 
108
  async def get_video_comments(video_id):
109
  try:
 
136
  return response_json.get("generated_text", "").strip()
137
  except json.JSONDecodeError:
138
  logging.error(f"Failed to decode JSON: {response}")
139
+ return response
140
  except Exception as e:
141
  logging.error(f"Error generating reply: {e}")
142
  return "닡변을 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
 
147
  embed = discord.Embed(description=f"**λŒ“κΈ€**: {comment[0]}\n**λ‹΅κΈ€**: {reply}")
148
  await thread.send(embed=embed)
149
  webhook_data = {"video_id": video_id, "replies": [{"comment": comment[0], "reply": reply, "comment_id": comment[1]}]}
150
+ await send_webhook_data(webhook_data)
151
  except discord.HTTPException as e:
152
  if e.code == 160004:
153
  logging.error("A thread has already been created for this message")
154
  else:
155
  logging.error(f"Error in thread creation and reply sending: {e}")
156
 
157
+ async def send_webhook_data(data):
158
+ async with aiohttp.ClientSession() as session:
159
+ data_json = json.dumps(data)
160
+ for i in range(0, len(data_json), MAX_CHUNK_SIZE):
161
+ chunk = data_json[i:i+MAX_CHUNK_SIZE]
162
+ try:
163
+ async with session.post(WEBHOOK_URL, json=json.loads(chunk)) as response:
164
+ if response.status == 200:
165
+ logging.info("Webhook data sent successfully.")
166
+ else:
167
+ logging.error(f"Failed to send webhook data: HTTP {response.status}")
168
+ except aiohttp.ClientError as e:
169
+ logging.error(f"HTTP error occurred while sending webhook data: {e}")
170
 
171
  if __name__ == "__main__":
172
  discord_client = MyClient(intents=intents)