Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from googleapiclient.discovery import build
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 6 |
+
youtube = build("youtube", "v3", developerKey=API_KEY)
|
| 7 |
+
|
| 8 |
+
def search_videos(keyword, max_results=10):
|
| 9 |
+
request = youtube.search().list(
|
| 10 |
+
q=keyword,
|
| 11 |
+
part="snippet",
|
| 12 |
+
type="video",
|
| 13 |
+
order="viewCount", # 조회수 순
|
| 14 |
+
maxResults=max_results
|
| 15 |
+
)
|
| 16 |
+
response = request.execute()
|
| 17 |
+
|
| 18 |
+
results = []
|
| 19 |
+
for item in response["items"]:
|
| 20 |
+
video_id = item["id"]["videoId"]
|
| 21 |
+
title = item["snippet"]["title"]
|
| 22 |
+
channel = item["snippet"]["channelTitle"]
|
| 23 |
+
url = f"https://youtube.com/watch?v={video_id}"
|
| 24 |
+
results.append(f"**{title}**\n채널: {channel}\n{url}\n")
|
| 25 |
+
|
| 26 |
+
return "\n".join(results)
|
| 27 |
+
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("## YouTube 인기 영상 검색")
|
| 30 |
+
keyword = gr.Textbox(label="검색어")
|
| 31 |
+
count = gr.Slider(5, 50, value=10, step=5, label="결과 수")
|
| 32 |
+
btn = gr.Button("검색")
|
| 33 |
+
output = gr.Markdown()
|
| 34 |
+
|
| 35 |
+
btn.click(search_videos, [keyword, count], output)
|
| 36 |
+
|
| 37 |
+
demo.launch()
|