seawolf2357 commited on
Commit
2db254d
β€’
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