seawolf2357 commited on
Commit
1899abe
โ€ข
1 Parent(s): 829fd20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import re
5
  import asyncio
6
  import subprocess
 
7
  from huggingface_hub import InferenceClient
8
  from googleapiclient.discovery import build
9
  from youtube_transcript_api import YouTubeTranscriptApi
@@ -33,6 +34,9 @@ youtube_service = build('youtube', 'v3', developerKey=API_KEY)
33
  # ํŠน์ • ์ฑ„๋„ ID
34
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
35
 
 
 
 
36
  class MyClient(discord.Client):
37
  def __init__(self, *args, **kwargs):
38
  super().__init__(*args, **kwargs)
@@ -43,7 +47,7 @@ class MyClient(discord.Client):
43
 
44
  # web.py ํŒŒ์ผ ์‹คํ–‰
45
  subprocess.Popen(["python", "web.py"])
46
- logging.info("Web.py server has been started.")
47
 
48
  # ๋ด‡์ด ์‹œ์ž‘๋  ๋•Œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก
49
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
@@ -88,28 +92,28 @@ def extract_video_id(url):
88
  match = re.match(youtube_regex, url)
89
  if match:
90
  video_id = match.group(6)
91
- logging.debug(f'Extracted video ID: {video_id}')
92
  return video_id
93
 
94
  async def get_best_available_transcript(video_id):
95
  try:
96
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
97
  except Exception as e:
98
- logging.warning(f'Error fetching Korean transcript: {e}')
99
  try:
100
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
101
  except Exception as e:
102
- logging.warning(f'Error fetching English transcript: {e}')
103
  try:
104
  transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
105
  transcript = transcripts.find_manually_created_transcript().fetch()
106
  except Exception as e:
107
- logging.error(f'Error fetching alternative transcript: {e}')
108
  return None
109
 
110
  formatter = TextFormatter()
111
  transcript_text = formatter.format_transcript(transcript)
112
- logging.debug(f'Fetched transcript: {transcript_text}')
113
  return transcript_text
114
 
115
  async def get_video_comments(video_id):
@@ -125,7 +129,7 @@ async def get_video_comments(video_id):
125
  comment_id = item['snippet']['topLevelComment']['id']
126
  comments.append((comment, comment_id))
127
 
128
- logging.debug(f'Fetched comments: {comments}')
129
  return comments
130
 
131
  async def generate_replies(comments, transcript):
@@ -145,15 +149,28 @@ async def generate_replies(comments, transcript):
145
  reply = "๋‹ต๊ธ€์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
146
  replies.append(reply)
147
 
148
- logging.debug(f'Generated replies: {replies}')
149
  return replies
150
 
151
  async def create_thread_and_send_replies(message, video_id, comments, replies):
152
  thread = await message.channel.create_thread(name=f"{message.author.name}์˜ ๋Œ“๊ธ€ ๋‹ต๊ธ€", message=message)
 
 
153
  for (comment, _), reply in zip(comments, replies):
154
  embed = discord.Embed(description=f"**๋Œ“๊ธ€**: {comment}\n**๋‹ต๊ธ€**: {reply}")
155
  await thread.send(embed=embed)
156
 
 
 
 
 
 
 
 
 
 
 
 
157
  if __name__ == "__main__":
158
  discord_client = MyClient(intents=intents)
159
  discord_client.run(os.getenv('DISCORD_TOKEN'))
 
4
  import re
5
  import asyncio
6
  import subprocess
7
+ import aiohttp
8
  from huggingface_hub import InferenceClient
9
  from googleapiclient.discovery import build
10
  from youtube_transcript_api import YouTubeTranscriptApi
 
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
  class MyClient(discord.Client):
41
  def __init__(self, *args, **kwargs):
42
  super().__init__(*args, **kwargs)
 
47
 
48
  # web.py ํŒŒ์ผ ์‹คํ–‰
49
  subprocess.Popen(["python", "web.py"])
50
+ logging.info("Web.py ์„œ๋ฒ„๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
51
 
52
  # ๋ด‡์ด ์‹œ์ž‘๋  ๋•Œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก
53
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
 
92
  match = re.match(youtube_regex, url)
93
  if match:
94
  video_id = match.group(6)
95
+ logging.debug(f'์ถ”์ถœ๋œ ๋น„๋””์˜ค ID: {video_id}')
96
  return video_id
97
 
98
  async def get_best_available_transcript(video_id):
99
  try:
100
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
101
  except Exception as e:
102
+ logging.warning(f'ํ•œ๊ตญ์–ด ์ž๋ง‰ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜: {e}')
103
  try:
104
  transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
105
  except Exception as e:
106
+ logging.warning(f'์˜์–ด ์ž๋ง‰ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜: {e}')
107
  try:
108
  transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
109
  transcript = transcripts.find_manually_created_transcript().fetch()
110
  except Exception as e:
111
+ logging.error(f'๋Œ€์ฒด ์ž๋ง‰ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜: {e}')
112
  return None
113
 
114
  formatter = TextFormatter()
115
  transcript_text = formatter.format_transcript(transcript)
116
+ logging.debug(f'๊ฐ€์ ธ์˜จ ์ž๋ง‰: {transcript_text}')
117
  return transcript_text
118
 
119
  async def get_video_comments(video_id):
 
129
  comment_id = item['snippet']['topLevelComment']['id']
130
  comments.append((comment, comment_id))
131
 
132
+ logging.debug(f'๊ฐ€์ ธ์˜จ ๋Œ“๊ธ€: {comments}')
133
  return comments
134
 
135
  async def generate_replies(comments, transcript):
 
149
  reply = "๋‹ต๊ธ€์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
150
  replies.append(reply)
151
 
152
+ logging.debug(f'์ƒ์„ฑ๋œ ๋‹ต๊ธ€: {replies}')
153
  return replies
154
 
155
  async def create_thread_and_send_replies(message, video_id, comments, replies):
156
  thread = await message.channel.create_thread(name=f"{message.author.name}์˜ ๋Œ“๊ธ€ ๋‹ต๊ธ€", message=message)
157
+ webhook_data = {"video_id": video_id, "replies": []}
158
+
159
  for (comment, _), reply in zip(comments, replies):
160
  embed = discord.Embed(description=f"**๋Œ“๊ธ€**: {comment}\n**๋‹ต๊ธ€**: {reply}")
161
  await thread.send(embed=embed)
162
 
163
+ # ์›นํ›… ๋ฐ์ดํ„ฐ ์ค€๋น„
164
+ webhook_data["replies"].append({"comment": comment, "reply": reply})
165
+
166
+ # ์›นํ›…์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก
167
+ async with aiohttp.ClientSession() as session:
168
+ async with session.post(WEBHOOK_URL, json=webhook_data) as resp:
169
+ if resp.status == 200:
170
+ logging.info("์›นํ›…์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก ์„ฑ๊ณต")
171
+ else:
172
+ logging.error(f"์›นํ›…์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก ์‹คํŒจ: {resp.status}")
173
+
174
  if __name__ == "__main__":
175
  discord_client = MyClient(intents=intents)
176
  discord_client.run(os.getenv('DISCORD_TOKEN'))