seawolf2357
commited on
Commit
โข
1899abe
1
Parent(s):
829fd20
Update app.py
Browse files
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
|
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'
|
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'
|
99 |
try:
|
100 |
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
101 |
except Exception as e:
|
102 |
-
logging.warning(f'
|
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'
|
108 |
return None
|
109 |
|
110 |
formatter = TextFormatter()
|
111 |
transcript_text = formatter.format_transcript(transcript)
|
112 |
-
logging.debug(f'
|
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'
|
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'
|
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'))
|