seawolf2357
commited on
Commit
β’
4636b5a
1
Parent(s):
2332fb8
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import requests
|
6 |
import asyncio
|
7 |
-
import
|
8 |
|
9 |
# λ‘κΉ
μ€μ
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
@@ -23,6 +23,10 @@ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("
|
|
23 |
PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
|
24 |
PEXELS_VIDEO_API_URL = "https://api.pexels.com/videos/search"
|
25 |
|
|
|
|
|
|
|
|
|
26 |
# νΉμ μ±λ ID
|
27 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
28 |
|
@@ -36,13 +40,11 @@ class MyClient(discord.Client):
|
|
36 |
|
37 |
async def on_ready(self):
|
38 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
39 |
-
subprocess.Popen(["python", "web.py"])
|
40 |
-
logging.info("Web.py server has been started.")
|
41 |
|
42 |
# λ΄μ΄ μμλ λ μλ΄ λ©μμ§λ₯Ό μ μ‘
|
43 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
44 |
if channel:
|
45 |
-
await channel.send("μ°Ύκ³ μΆμ
|
46 |
|
47 |
async def on_message(self, message):
|
48 |
if message.author == self.user:
|
@@ -58,11 +60,13 @@ class MyClient(discord.Client):
|
|
58 |
if keywords:
|
59 |
# Pexels APIλ‘ κ³ ν΄μλ λΉλμ€ κ²μ
|
60 |
video_urls = await search_videos(keywords)
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
else:
|
65 |
-
await message.channel.send(f"**{keywords}**μ λν
|
66 |
else:
|
67 |
await message.channel.send("ν€μλλ₯Ό μΆμΆν μ μμ΅λλ€.")
|
68 |
finally:
|
@@ -101,7 +105,7 @@ async def search_videos(keywords):
|
|
101 |
}
|
102 |
params = {
|
103 |
"query": keywords,
|
104 |
-
"per_page":
|
105 |
}
|
106 |
response = requests.get(PEXELS_VIDEO_API_URL, headers=headers, params=params)
|
107 |
if response.status_code == 200:
|
@@ -109,13 +113,32 @@ async def search_videos(keywords):
|
|
109 |
return [video['video_files'][0]['link'] for video in data['videos']]
|
110 |
return None
|
111 |
|
112 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# μ°λ λ μμ±
|
114 |
thread = await message.channel.create_thread(name=f"{message.author.name}μ κ²μ κ²°κ³Ό", message=message)
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
discord_client = MyClient(intents=intents)
|
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import requests
|
6 |
import asyncio
|
7 |
+
from googleapiclient.discovery import build
|
8 |
|
9 |
# λ‘κΉ
μ€μ
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
|
|
23 |
PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
|
24 |
PEXELS_VIDEO_API_URL = "https://api.pexels.com/videos/search"
|
25 |
|
26 |
+
# YouTube API ν€ μ€μ
|
27 |
+
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
28 |
+
youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
29 |
+
|
30 |
# νΉμ μ±λ ID
|
31 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
32 |
|
|
|
40 |
|
41 |
async def on_ready(self):
|
42 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
|
|
|
|
43 |
|
44 |
# λ΄μ΄ μμλ λ μλ΄ λ©μμ§λ₯Ό μ μ‘
|
45 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
46 |
if channel:
|
47 |
+
await channel.send("μ°Ύκ³ μΆμ λΉλμ€λ μμ
μ λν μ€λͺ
μ ν λ¬Έμ₯ λ¨μλ‘ μ
λ ₯νμΈμ. μ) λ°λ€μμ μννλ μ¬λ λλ ν₯κ²¨μ΄ μ¬λ¦ μμ
")
|
48 |
|
49 |
async def on_message(self, message):
|
50 |
if message.author == self.user:
|
|
|
60 |
if keywords:
|
61 |
# Pexels APIλ‘ κ³ ν΄μλ λΉλμ€ κ²μ
|
62 |
video_urls = await search_videos(keywords)
|
63 |
+
# YouTube APIλ‘ μμ
κ²μ
|
64 |
+
music_urls = await search_music_on_youtube(keywords)
|
65 |
+
if video_urls or music_urls:
|
66 |
+
# μμ²μμμ μ°λ λ μμ± λ° κ²°κ³Ό μ μ‘
|
67 |
+
await create_thread_and_send_results(message, keywords, video_urls, music_urls)
|
68 |
else:
|
69 |
+
await message.channel.send(f"**{keywords}**μ λν λΉλμ€λ μμ
μ μ°Ύμ μ μμ΅λλ€.")
|
70 |
else:
|
71 |
await message.channel.send("ν€μλλ₯Ό μΆμΆν μ μμ΅λλ€.")
|
72 |
finally:
|
|
|
105 |
}
|
106 |
params = {
|
107 |
"query": keywords,
|
108 |
+
"per_page": 10 # μ΅λ 10κ° λΉλμ€λ₯Ό κ°μ Έμ΅λλ€
|
109 |
}
|
110 |
response = requests.get(PEXELS_VIDEO_API_URL, headers=headers, params=params)
|
111 |
if response.status_code == 200:
|
|
|
113 |
return [video['video_files'][0]['link'] for video in data['videos']]
|
114 |
return None
|
115 |
|
116 |
+
async def search_music_on_youtube(keywords):
|
117 |
+
response = youtube_service.search().list(
|
118 |
+
q=keywords,
|
119 |
+
part='snippet',
|
120 |
+
type='video',
|
121 |
+
maxResults=5,
|
122 |
+
videoCategoryId='10' # μμ
μΉ΄ν
κ³ λ¦¬ ID
|
123 |
+
).execute()
|
124 |
+
music_urls = []
|
125 |
+
for item in response.get('items', []):
|
126 |
+
music_urls.append(f"https://www.youtube.com/watch?v={item['id']['videoId']}")
|
127 |
+
return music_urls
|
128 |
+
|
129 |
+
async def create_thread_and_send_results(message, keywords, video_urls, music_urls):
|
130 |
# μ°λ λ μμ±
|
131 |
thread = await message.channel.create_thread(name=f"{message.author.name}μ κ²μ κ²°κ³Ό", message=message)
|
132 |
+
if video_urls:
|
133 |
+
message_content = f"**{keywords}**μ λν κ³ ν΄μλ λΉλμ€ {len(video_urls)}κ°λ₯Ό μ°Ύμμ΅λλ€:"
|
134 |
+
await thread.send(message_content)
|
135 |
+
for url in video_urls:
|
136 |
+
await thread.send(url)
|
137 |
+
if music_urls:
|
138 |
+
message_content = f"**{keywords}**μ λν μμ
{len(music_urls)}κ°λ₯Ό μ°Ύμμ΅λλ€:"
|
139 |
+
await thread.send(message_content)
|
140 |
+
for url in music_urls:
|
141 |
+
await thread.send(url)
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
discord_client = MyClient(intents=intents)
|