youtube2 / app.py
Juna190825's picture
Update app.py
b1c6e0c verified
Raw
History Blame Contribute Delete
3 kB
# import gradio as gr
# from youtubesearchpython import VideosSearch
# import sys
# from io import StringIO
# # PATCH: Remove the problematic proxies parameter
# def search_youtube(query, max_results=15):
# try:
# print(f"πŸ” Searching for: {query}")
# # Try with limit first
# try:
# results = VideosSearch(query, limit=max_results).result()['result']
# except TypeError as e:
# if "proxies" in str(e):
# # If proxies error, try without limit
# print("⚠️ Version compatibility fix applied...")
# search = VideosSearch(query)
# results = search.result()['result'][:max_results]
# else:
# raise e
# videos = []
# for video in results:
# videos.append({
# 'title': video.get('title', 'No Title'),
# 'url': video.get('url_suffix', ''),
# 'channel': video.get('channel', 'Unknown'),
# 'duration': video.get('duration', 'N/A'),
# 'views': video.get('views', 'N/A')
# })
# print(f"βœ… Found {len(videos)} videos")
# return videos
# except Exception as e:
# print(f"❌ Error: {e}")
# return []
# # This works in: Colab, HF Spaces, Local, ANY Python environment
# def search_and_display(query, max_results):
# log_capture = StringIO()
# sys.stdout = log_capture
# try:
# videos = search_youtube(query, int(max_results))
# if not videos:
# display_text = "❌ No videos found"
# else:
# display_text = f"πŸ“Ί **Found {len(videos)} videos**\n\n"
# for i, video in enumerate(videos, 1):
# display_text += f"**{i}. {video['title']}**\n"
# display_text += f" πŸ“Ή Channel: {video['channel']}\n"
# display_text += f" ⏱️ Duration: {video['duration']}\n"
# display_text += f" πŸ‘οΈ Views: {video['views']}\n"
# display_text += f" πŸ”— https://youtube.com{video['url']}\n\n"
# logs = log_capture.getvalue()
# return display_text, logs if logs else "βœ… Done!"
# finally:
# sys.stdout = sys.__stdout__
# # Create Gradio interface
# with gr.Blocks() as demo:
# gr.Markdown("# πŸ” YouTube Search")
# with gr.Row():
# query = gr.Textbox(label="Search", placeholder="Enter search...", scale=3)
# max_res = gr.Slider(label="Results", minimum=5, maximum=30, value=15, step=5, scale=1)
# btn = gr.Button("Search")
# with gr.Row():
# results = gr.Markdown(label="Results")
# logs = gr.Textbox(label="Logs", lines=15, interactive=False)
# btn.click(search_and_display, [query, max_res], [results, logs])
# demo.launch()
import requests
requests.get("https://api.ipify.org?format=json").json()