seawolf2357 commited on
Commit
b1c9665
β€’
1 Parent(s): 8a03a11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -21,7 +21,7 @@ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("
21
 
22
  # Pexels API ν‚€ μ„€μ •
23
  PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
24
- PEXELS_API_URL = "https://api.pexels.com/v1/search"
25
 
26
  # νŠΉμ • 채널 ID
27
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -42,7 +42,7 @@ class MyClient(discord.Client):
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:
@@ -56,13 +56,13 @@ class MyClient(discord.Client):
56
  # 의미 λΆ„μ„ν•˜μ—¬ 영문 ν‚€μ›Œλ“œ μΆ”μΆœ
57
  keywords = await extract_keywords(message)
58
  if keywords:
59
- # Pexels API둜 고해상도 이미지 검색
60
- image_urls = await search_images(keywords)
61
- if image_urls:
62
- # μš”μ²­μžμ™€μ˜ μ“°λ ˆλ“œ 생성 및 고해상도 이미지 전솑
63
- await create_thread_and_send_images(message, keywords, image_urls)
64
  else:
65
- await message.channel.send(f"**{keywords}**에 λŒ€ν•œ 고해상도 이미지λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
66
  else:
67
  await message.channel.send("ν‚€μ›Œλ“œλ₯Ό μΆ”μΆœν•  수 μ—†μŠ΅λ‹ˆλ‹€.")
68
  finally:
@@ -95,26 +95,26 @@ async def extract_keywords(message):
95
  logging.debug(f'Extracted keywords: {keywords}')
96
  return keywords
97
 
98
- async def search_images(keywords):
99
  headers = {
100
  "Authorization": PEXELS_API_KEY
101
  }
102
  params = {
103
  "query": keywords,
104
- "per_page": 80 # μ΅œλŒ€ 20개 이미지λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€
105
  }
106
- response = requests.get(PEXELS_API_URL, headers=headers, params=params)
107
  if response.status_code == 200:
108
  data = response.json()
109
- return [photo['src']['large2x'] for photo in data['photos']]
110
  return None
111
 
112
- async def create_thread_and_send_images(message, keywords, image_urls):
113
  # μ“°λ ˆλ“œ 생성
114
  thread = await message.channel.create_thread(name=f"{message.author.name}의 검색 κ²°κ³Ό", message=message)
115
- message_content = f"**{keywords}**에 λŒ€ν•œ 고해상도 이미지 {len(image_urls)}μž₯을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€:"
116
  await thread.send(message_content)
117
- for url in image_urls:
118
  await thread.send(url)
119
 
120
  if __name__ == "__main__":
 
21
 
22
  # Pexels API ν‚€ μ„€μ •
23
  PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
24
+ PEXELS_API_VIDEO_URL = "https://api.pexels.com/videos/search"
25
 
26
  # νŠΉμ • 채널 ID
27
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
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:
 
56
  # 의미 λΆ„μ„ν•˜μ—¬ 영문 ν‚€μ›Œλ“œ μΆ”μΆœ
57
  keywords = await extract_keywords(message)
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:
 
95
  logging.debug(f'Extracted keywords: {keywords}')
96
  return keywords
97
 
98
+ async def search_videos(keywords):
99
  headers = {
100
  "Authorization": PEXELS_API_KEY
101
  }
102
  params = {
103
  "query": keywords,
104
+ "per_page": 80 # μ΅œλŒ€ 20개 λ™μ˜μƒμ„ κ°€μ Έμ˜΅λ‹ˆλ‹€
105
  }
106
+ response = requests.get(PEXELS_API_VIDEO_URL, headers=headers, params=params)
107
  if response.status_code == 200:
108
  data = response.json()
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__":