seawolf2357 commited on
Commit
b4bdc7a
β€’
1 Parent(s): 4f7204d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -48
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import discord
2
  import logging
3
  import os
4
- from huggingface_hub import InferenceClient
5
- import requests
6
  import asyncio
 
7
  from googleapiclient.discovery import build
8
  from dotenv import load_dotenv
9
 
@@ -23,17 +22,12 @@ intents.guild_messages = True
23
  # μΆ”λ‘  API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
24
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
25
 
26
- # Pexels API ν‚€ μ„€μ •
27
- PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
28
- PEXELS_VIDEO_API_URL = "https://api.pexels.com/videos/search"
29
-
30
  # YouTube API ν‚€ μ„€μ •
31
  YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
32
  youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
33
 
34
  # νŠΉμ • 채널 ID
35
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
36
- YOUTUBE_MUSIC_CHANNEL_ID = os.getenv("YOUTUBE_MUSIC_CHANNEL_ID")
37
 
38
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  μ „μ—­ λ³€μˆ˜
39
  conversation_history = []
@@ -49,7 +43,7 @@ class MyClient(discord.Client):
49
  # 봇이 μ‹œμž‘λ  λ•Œ μ•ˆλ‚΄ λ©”μ‹œμ§€λ₯Ό 전솑
50
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
51
  if channel:
52
- await channel.send("찾고싢은 λΉ„λ””μ˜€λ‚˜ μŒμ•…μ— λŒ€ν•œ μ„€λͺ…을 ν•œ λ¬Έμž₯ λ‹¨μœ„λ‘œ μž…λ ₯ν•˜μ„Έμš”. 예) λ°”λ‹€μ—μ„œ μ„œν•‘ν•˜λŠ” μ‚¬λžŒ λ˜λŠ” ν₯겨운 여름 μŒμ•…")
53
 
54
  async def on_message(self, message):
55
  if message.author == self.user:
@@ -60,18 +54,16 @@ class MyClient(discord.Client):
60
  return
61
  self.is_processing = True
62
  try:
63
- # 의미 λΆ„μ„ν•˜μ—¬ 영문 ν‚€μ›Œλ“œ μΆ”μΆœ
64
  keywords = await extract_keywords(message)
65
  if keywords:
66
- # Pexels API둜 고해상도 λΉ„λ””μ˜€ 검색
67
- video_urls = await search_videos(keywords)
68
- # YouTube API둜 μŒμ•… 검색
69
- music_urls = await search_music_on_youtube(keywords)
70
- if video_urls or music_urls:
71
  # μš”μ²­μžμ™€μ˜ μ“°λ ˆλ“œ 생성 및 κ²°κ³Ό 전솑
72
- await create_thread_and_send_results(message, keywords, video_urls, music_urls)
73
  else:
74
- await message.channel.send(f"**{keywords}**에 λŒ€ν•œ λΉ„λ””μ˜€λ‚˜ μŒμ•…μ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
75
  else:
76
  await message.channel.send("ν‚€μ›Œλ“œλ₯Ό μΆ”μΆœν•  수 μ—†μŠ΅λ‹ˆλ‹€.")
77
  finally:
@@ -104,46 +96,35 @@ async def extract_keywords(message):
104
  logging.debug(f'Extracted keywords: {keywords}')
105
  return keywords
106
 
107
- async def search_videos(keywords):
108
- headers = {
109
- "Authorization": PEXELS_API_KEY
110
- }
111
- params = {
112
- "query": keywords,
113
- "per_page": 10 # μ΅œλŒ€ 10개 λΉ„λ””μ˜€λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€
114
- }
115
- response = requests.get(PEXELS_VIDEO_API_URL, headers=headers, params=params)
116
- if response.status_code == 200:
117
- data = response.json()
118
- return [video['video_files'][0]['link'] for video in data['videos']]
119
- return None
120
-
121
- async def search_music_on_youtube(keywords):
122
  response = youtube_service.search().list(
123
  q=keywords,
124
  part='snippet',
125
  type='video',
126
- maxResults=5,
127
- channelId=YOUTUBE_MUSIC_CHANNEL_ID # νŠΉμ • 채널 ID둜 검색
128
  ).execute()
129
- music_urls = []
 
130
  for item in response.get('items', []):
131
- music_urls.append(f"https://www.youtube.com/watch?v={item['id']['videoId']}")
132
- return music_urls
133
-
134
- async def create_thread_and_send_results(message, keywords, video_urls, music_urls):
 
 
 
 
 
135
  # μ“°λ ˆλ“œ 생성
136
- thread = await message.channel.create_thread(name=f"{message.author.name}의 검색 κ²°κ³Ό", message=message)
137
- if video_urls:
138
- message_content = f"**{keywords}**에 λŒ€ν•œ 고해상도 λΉ„λ””μ˜€ {len(video_urls)}개λ₯Ό μ°Ύμ•˜μŠ΅λ‹ˆλ‹€:"
139
- await thread.send(message_content)
140
- for url in video_urls:
141
- await thread.send(url)
142
- if music_urls:
143
- message_content = f"**{keywords}**에 λŒ€ν•œ μŒμ•… {len(music_urls)}개λ₯Ό μ°Ύμ•˜μŠ΅λ‹ˆλ‹€:"
144
  await thread.send(message_content)
145
- for url in music_urls:
146
- await thread.send(url)
 
 
147
 
148
  if __name__ == "__main__":
149
  discord_client = MyClient(intents=intents)
 
1
  import discord
2
  import logging
3
  import os
 
 
4
  import asyncio
5
+ from huggingface_hub import InferenceClient
6
  from googleapiclient.discovery import build
7
  from dotenv import load_dotenv
8
 
 
22
  # μΆ”λ‘  API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
23
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
24
 
 
 
 
 
25
  # YouTube API ν‚€ μ„€μ •
26
  YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
27
  youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
28
 
29
  # νŠΉμ • 채널 ID
30
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
31
 
32
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  μ „μ—­ λ³€μˆ˜
33
  conversation_history = []
 
43
  # 봇이 μ‹œμž‘λ  λ•Œ μ•ˆλ‚΄ λ©”μ‹œμ§€λ₯Ό 전솑
44
  channel = self.get_channel(SPECIFIC_CHANNEL_ID)
45
  if channel:
46
+ await channel.send("νŠΈλ Œλ“œ 보고 싢은 주제λ₯Ό μž…λ ₯ν•˜μ„Έμš”. 예) μ΅œμ‹  ν…Œν¬ λ‰΄μŠ€")
47
 
48
  async def on_message(self, message):
49
  if message.author == self.user:
 
54
  return
55
  self.is_processing = True
56
  try:
57
+ # 주제 ν‚€μ›Œλ“œ μΆ”μΆœ
58
  keywords = await extract_keywords(message)
59
  if keywords:
60
+ # YouTube API둜 μ΅œμ‹  νŠΈλ Œλ“œ 및 인기 λΉ„λ””μ˜€ 검색
61
+ video_details = await search_trending_videos(keywords)
62
+ if video_details:
 
 
63
  # μš”μ²­μžμ™€μ˜ μ“°λ ˆλ“œ 생성 및 κ²°κ³Ό 전솑
64
+ await create_thread_and_send_results(message, keywords, video_details)
65
  else:
66
+ await message.channel.send(f"**{keywords}**에 λŒ€ν•œ μ΅œμ‹  νŠΈλ Œλ“œ λΉ„λ””μ˜€λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
67
  else:
68
  await message.channel.send("ν‚€μ›Œλ“œλ₯Ό μΆ”μΆœν•  수 μ—†μŠ΅λ‹ˆλ‹€.")
69
  finally:
 
96
  logging.debug(f'Extracted keywords: {keywords}')
97
  return keywords
98
 
99
+ async def search_trending_videos(keywords):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  response = youtube_service.search().list(
101
  q=keywords,
102
  part='snippet',
103
  type='video',
104
+ order='viewCount', # 인기 μžˆλŠ” λΉ„λ””μ˜€ κΈ°μ€€μœΌλ‘œ μ •λ ¬
105
+ maxResults=5 # μ›ν•˜λŠ” κ²°κ³Ό 수
106
  ).execute()
107
+
108
+ video_details = []
109
  for item in response.get('items', []):
110
+ video_details.append({
111
+ 'title': item['snippet']['title'],
112
+ 'url': f"https://www.youtube.com/watch?v={item['id']['videoId']}",
113
+ 'description': item['snippet']['description'],
114
+ 'thumbnail': item['snippet']['thumbnails']['high']['url']
115
+ })
116
+ return video_details
117
+
118
+ async def create_thread_and_send_results(message, keywords, video_details):
119
  # μ“°λ ˆλ“œ 생성
120
+ thread = await message.channel.create_thread(name=f"{message.author.name}의 νŠΈλ Œλ“œ 검색 κ²°κ³Ό", message=message)
121
+ if video_details:
122
+ message_content = f"**{keywords}**에 λŒ€ν•œ μ΅œμ‹  νŠΈλ Œλ“œ λΉ„λ””μ˜€ {len(video_details)}개λ₯Ό μ°Ύμ•˜μŠ΅λ‹ˆλ‹€:"
 
 
 
 
 
123
  await thread.send(message_content)
124
+ for video in video_details:
125
+ embed = discord.Embed(title=video['title'], description=video['description'], url=video['url'])
126
+ embed.set_thumbnail(url=video['thumbnail'])
127
+ await thread.send(embed=embed)
128
 
129
  if __name__ == "__main__":
130
  discord_client = MyClient(intents=intents)