seawolf2357 commited on
Commit
4636b5a
β€’
1 Parent(s): 2332fb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  from huggingface_hub import InferenceClient
5
  import requests
6
  import asyncio
7
- import subprocess
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
- if video_urls:
62
- # μš”μ²­μžμ™€μ˜ μ“°λ ˆλ“œ 생성 및 고해상도 λΉ„λ””μ˜€ 전솑
63
- await create_thread_and_send_videos(message, keywords, video_urls)
 
 
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": 40 # μ΅œλŒ€ 10개 λΉ„λ””μ˜€λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€
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 create_thread_and_send_videos(message, keywords, video_urls):
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  # μ“°λ ˆλ“œ 생성
114
  thread = await message.channel.create_thread(name=f"{message.author.name}의 검색 κ²°κ³Ό", message=message)
115
- message_content = f"**{keywords}**에 λŒ€ν•œ 고해상도 λΉ„λ””μ˜€ {len(video_urls)}개λ₯Ό μ°Ύμ•˜μŠ΅λ‹ˆλ‹€:"
116
- await thread.send(message_content)
117
- for url in video_urls:
118
- await thread.send(url)
 
 
 
 
 
 
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)