Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,7 @@
|
|
1 |
-
import subprocess
|
2 |
-
import sys
|
3 |
-
|
4 |
-
# Ensure compatible versions of httpx and httpcore are installed
|
5 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
|
6 |
-
|
7 |
-
import gradio as gr
|
8 |
-
import requests
|
9 |
-
import re
|
10 |
import yt_dlp
|
11 |
import logging
|
|
|
|
|
12 |
|
13 |
# Configure logging for debugging purposes
|
14 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -30,6 +23,7 @@ def youtube_search(query, max_results=50):
|
|
30 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
31 |
result = ydl.extract_info(search_url, download=False)
|
32 |
gallery_items = []
|
|
|
33 |
|
34 |
if 'entries' in result:
|
35 |
logging.debug(f"Number of entries found: {len(result['entries'])}")
|
@@ -39,10 +33,39 @@ def youtube_search(query, max_results=50):
|
|
39 |
video_title = entry.get('title', "Unknown Title")
|
40 |
|
41 |
if video_id:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
gallery_items.append((thumbnail_url, video_id, video_title))
|
43 |
-
|
44 |
-
else:
|
45 |
-
logging.debug(f"Missing video ID for entry: {entry}")
|
46 |
else:
|
47 |
logging.warning("No entries found in search result.")
|
48 |
return gallery_items, ""
|
@@ -53,7 +76,6 @@ def youtube_search(query, max_results=50):
|
|
53 |
|
54 |
# Function to display the video using the video URL
|
55 |
def show_video(video_url):
|
56 |
-
# Regular expression to extract the YouTube video ID from the URL
|
57 |
video_id = None
|
58 |
patterns = [
|
59 |
r"youtube\.com/watch\?v=([^&?\/]+)",
|
@@ -69,16 +91,13 @@ def show_video(video_url):
|
|
69 |
logging.debug(f"Extracted video ID: {video_id}")
|
70 |
break
|
71 |
|
72 |
-
# If no video ID is found, return an error message
|
73 |
if not video_id:
|
74 |
logging.error("Invalid YouTube URL. Please enter a valid YouTube video link.")
|
75 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
76 |
|
77 |
-
# Create the embed URL
|
78 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
79 |
logging.debug(f"Embed URL generated: {embed_url}")
|
80 |
|
81 |
-
# Return an iframe with the video
|
82 |
html_code = f'''
|
83 |
<iframe width="560" height="315" src="{embed_url}"
|
84 |
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
@@ -107,13 +126,11 @@ with gr.Blocks() as demo:
|
|
107 |
gallery_items, error_message = youtube_search(query)
|
108 |
if error_message:
|
109 |
return [], error_message, gr.update(visible=True)
|
110 |
-
|
111 |
-
gallery_items_display = [(item[0], item[1]) for item in gallery_items] # Show thumbnail and video ID only
|
112 |
return gallery_items_display, "", gr.update(visible=False)
|
113 |
|
114 |
# Update the selected video link field when a video is clicked in the gallery
|
115 |
def on_video_select(evt: gr.SelectData):
|
116 |
-
# Extract the video ID from the event value, which is a dictionary containing details of the selected item
|
117 |
selected_video_id = evt.value["caption"]
|
118 |
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
119 |
logging.debug(f"Video selected: {video_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import yt_dlp
|
2 |
import logging
|
3 |
+
import gradio as gr
|
4 |
+
import re
|
5 |
|
6 |
# Configure logging for debugging purposes
|
7 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
23 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
24 |
result = ydl.extract_info(search_url, download=False)
|
25 |
gallery_items = []
|
26 |
+
detailed_entries = []
|
27 |
|
28 |
if 'entries' in result:
|
29 |
logging.debug(f"Number of entries found: {len(result['entries'])}")
|
|
|
33 |
video_title = entry.get('title', "Unknown Title")
|
34 |
|
35 |
if video_id:
|
36 |
+
# Collect and display details
|
37 |
+
print("\n==============================")
|
38 |
+
print("Video Details:")
|
39 |
+
print("==============================")
|
40 |
+
print(f"ID: {entry.get('id', 'N/A')}")
|
41 |
+
print(f"Title: {entry.get('title', 'N/A')}")
|
42 |
+
print(f"Uploader: {entry.get('uploader', 'N/A')}")
|
43 |
+
print(f"Uploader ID: {entry.get('uploader_id', 'N/A')}")
|
44 |
+
print(f"Upload Date: {entry.get('upload_date', 'N/A')}")
|
45 |
+
print(f"Description: {entry.get('description', 'N/A')}")
|
46 |
+
print(f"Duration (seconds): {entry.get('duration', 'N/A')}")
|
47 |
+
print(f"View Count: {entry.get('view_count', 'N/A')}")
|
48 |
+
print(f"Like Count: {entry.get('like_count', 'N/A')}")
|
49 |
+
print(f"Dislike Count: {entry.get('dislike_count', 'N/A')}")
|
50 |
+
print(f"Comment Count: {entry.get('comment_count', 'N/A')}")
|
51 |
+
print(f"Average Rating: {entry.get('average_rating', 'N/A')}")
|
52 |
+
print(f"Thumbnail URL: {entry.get('thumbnail', 'N/A')}")
|
53 |
+
print(f"Webpage URL: {entry.get('webpage_url', 'N/A')}")
|
54 |
+
print(f"Channel URL: {entry.get('channel_url', 'N/A')}")
|
55 |
+
print(f"Categories: {entry.get('categories', 'N/A')}")
|
56 |
+
print(f"Tags: {entry.get('tags', 'N/A')}")
|
57 |
+
print(f"Channel ID: {entry.get('channel_id', 'N/A')}")
|
58 |
+
print(f"Age Restriction: {entry.get('age_limit', 'N/A')}")
|
59 |
+
print(f"Is Live: {entry.get('is_live', 'N/A')}")
|
60 |
+
print(f"FPS: {entry.get('fps', 'N/A')}")
|
61 |
+
print(f"Resolution: {entry.get('resolution', 'N/A')}")
|
62 |
+
print(f"Aspect Ratio: {entry.get('aspect_ratio', 'N/A')}")
|
63 |
+
print(f"Video Codec: {entry.get('vcodec', 'N/A')}")
|
64 |
+
print(f"Audio Codec: {entry.get('acodec', 'N/A')}")
|
65 |
+
print("==============================\n")
|
66 |
+
|
67 |
gallery_items.append((thumbnail_url, video_id, video_title))
|
68 |
+
detailed_entries.append(entry)
|
|
|
|
|
69 |
else:
|
70 |
logging.warning("No entries found in search result.")
|
71 |
return gallery_items, ""
|
|
|
76 |
|
77 |
# Function to display the video using the video URL
|
78 |
def show_video(video_url):
|
|
|
79 |
video_id = None
|
80 |
patterns = [
|
81 |
r"youtube\.com/watch\?v=([^&?\/]+)",
|
|
|
91 |
logging.debug(f"Extracted video ID: {video_id}")
|
92 |
break
|
93 |
|
|
|
94 |
if not video_id:
|
95 |
logging.error("Invalid YouTube URL. Please enter a valid YouTube video link.")
|
96 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
97 |
|
|
|
98 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
99 |
logging.debug(f"Embed URL generated: {embed_url}")
|
100 |
|
|
|
101 |
html_code = f'''
|
102 |
<iframe width="560" height="315" src="{embed_url}"
|
103 |
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
|
|
126 |
gallery_items, error_message = youtube_search(query)
|
127 |
if error_message:
|
128 |
return [], error_message, gr.update(visible=True)
|
129 |
+
gallery_items_display = [(item[0], item[1]) for item in gallery_items]
|
|
|
130 |
return gallery_items_display, "", gr.update(visible=False)
|
131 |
|
132 |
# Update the selected video link field when a video is clicked in the gallery
|
133 |
def on_video_select(evt: gr.SelectData):
|
|
|
134 |
selected_video_id = evt.value["caption"]
|
135 |
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
136 |
logging.debug(f"Video selected: {video_url}")
|