seawolf2357 commited on
Commit
2db254d
·
verified ·
1 Parent(s): 8892907

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -11,33 +11,33 @@ from youtube_transcript_api import YouTubeTranscriptApi
11
  from youtube_transcript_api.formatters import TextFormatter
12
  from dotenv import load_dotenv
13
 
14
- # 환경 변수 로드
15
  load_dotenv()
16
 
17
- # 로깅 설정
18
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
19
 
20
- # 인텐트 설정
21
  intents = discord.Intents.default()
22
  intents.message_content = True
23
  intents.messages = True
24
  intents.guilds = True
25
  intents.guild_messages = True
26
 
27
- # 추론 API 클라이언트 설정
28
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
29
 
30
- # YouTube API 설정
31
  API_KEY = os.getenv("YOUTUBE_API_KEY")
32
  youtube_service = build('youtube', 'v3', developerKey=API_KEY)
33
 
34
- # 특정 채널 ID
35
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
36
 
37
- # 웹훅 URL 설정
38
  WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY1MDYzMjA0MzA1MjY4NTUzMDUxMzUi_pc"
39
 
40
- # 전송 실패 재시도 횟수
41
  MAX_RETRIES = 3
42
 
43
  class MyClient(discord.Client):
@@ -49,14 +49,14 @@ class MyClient(discord.Client):
49
  async def on_ready(self):
50
  logging.info(f'{self.user}로 로그인되었습니다!')
51
 
52
- # web.py 파일 실행
53
  subprocess.Popen(["python", "web.py"])
54
  logging.info("Web.py 서버가 시작되었습니다.")
55
 
56
- # aiohttp 클라이언트 세션 생성
57
  self.session = aiohttp.ClientSession()
58
 
59
- # 봇이 시작될 안내 메시지를 전송
60
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
61
  if channel:
62
  await channel.send("유튜브 비디오 URL을 입력하면, 자막과 댓글을 기반으로 답글을 작성합니다.")
@@ -76,7 +76,7 @@ class MyClient(discord.Client):
76
  comments = await get_video_comments(video_id)
77
  if comments and transcript:
78
  replies = await generate_replies(comments, transcript)
79
- await create_thread_and_send_replies(message, video_id, comments, replies)
80
  else:
81
  await message.channel.send("자막이나 댓글을 가져올 수 없습니다.")
82
  else:
@@ -90,7 +90,7 @@ class MyClient(discord.Client):
90
  )
91
 
92
  async def close(self):
93
- # aiohttp 클라이언트 세션 종료
94
  if self.session:
95
  await self.session.close()
96
  await super().close()
@@ -180,7 +180,7 @@ async def send_webhook_data(session, chunk_data, chunk_number):
180
 
181
  return False # 재시도 횟수 초과 시 실패로 간주
182
 
183
- async def create_thread_and_send_replies(message, video_id, comments, replies):
184
  thread = await message.channel.create_thread(name=f"{message.author.name}의 댓글 답글", message=message)
185
  webhook_data = {"video_id": video_id, "replies": []}
186
 
@@ -197,7 +197,7 @@ async def create_thread_and_send_replies(message, video_id, comments, replies):
197
  chunk = webhook_data["replies"][i:i+chunk_size]
198
  chunk_data = {"video_id": video_id, "replies": chunk}
199
 
200
- success = await send_webhook_data(self.session, chunk_data, i // chunk_size + 1)
201
  if not success:
202
  logging.error(f"데이터 전송 실패: {i // chunk_size + 1} 번째 청크")
203
 
 
11
  from youtube_transcript_api.formatters import TextFormatter
12
  from dotenv import load_dotenv
13
 
14
+ # Load environment variables
15
  load_dotenv()
16
 
17
+ # Logging configuration
18
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
19
 
20
+ # Intents configuration
21
  intents = discord.Intents.default()
22
  intents.message_content = True
23
  intents.messages = True
24
  intents.guilds = True
25
  intents.guild_messages = True
26
 
27
+ # Inference API client configuration
28
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
29
 
30
+ # YouTube API configuration
31
  API_KEY = os.getenv("YOUTUBE_API_KEY")
32
  youtube_service = build('youtube', 'v3', developerKey=API_KEY)
33
 
34
+ # Specific channel ID
35
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
36
 
37
+ # Webhook URL configuration
38
  WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY1MDYzMjA0MzA1MjY4NTUzMDUxMzUi_pc"
39
 
40
+ # Maximum retries for sending data
41
  MAX_RETRIES = 3
42
 
43
  class MyClient(discord.Client):
 
49
  async def on_ready(self):
50
  logging.info(f'{self.user}로 로그인되었습니다!')
51
 
52
+ # Start web.py
53
  subprocess.Popen(["python", "web.py"])
54
  logging.info("Web.py 서버가 시작되었습니다.")
55
 
56
+ # Create aiohttp client session
57
  self.session = aiohttp.ClientSession()
58
 
59
+ # Send initial message when bot starts
60
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
61
  if channel:
62
  await channel.send("유튜브 비디오 URL을 입력하면, 자막과 댓글을 기반으로 답글을 작성합니다.")
 
76
  comments = await get_video_comments(video_id)
77
  if comments and transcript:
78
  replies = await generate_replies(comments, transcript)
79
+ await create_thread_and_send_replies(message, video_id, comments, replies, self.session)
80
  else:
81
  await message.channel.send("자막이나 댓글을 가져올 수 없습니다.")
82
  else:
 
90
  )
91
 
92
  async def close(self):
93
+ # Close aiohttp client session
94
  if self.session:
95
  await self.session.close()
96
  await super().close()
 
180
 
181
  return False # 재시도 횟수 초과 시 실패로 간주
182
 
183
+ async def create_thread_and_send_replies(message, video_id, comments, replies, session):
184
  thread = await message.channel.create_thread(name=f"{message.author.name}의 댓글 답글", message=message)
185
  webhook_data = {"video_id": video_id, "replies": []}
186
 
 
197
  chunk = webhook_data["replies"][i:i+chunk_size]
198
  chunk_data = {"video_id": video_id, "replies": chunk}
199
 
200
+ success = await send_webhook_data(session, chunk_data, i // chunk_size + 1)
201
  if not success:
202
  logging.error(f"데이터 전송 실패: {i // chunk_size + 1} 번째 청크")
203