seawolf2357
commited on
Commit
β’
2db254d
1
Parent(s):
8892907
Update app.py
Browse files
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 |
-
#
|
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 |
-
#
|
35 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
36 |
|
37 |
-
#
|
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(
|
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 |
|