Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,11 +7,14 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %
|
|
7 |
# Function to search YouTube videos using yt-dlp for better reliability
|
8 |
def youtube_search(query, max_results=50):
|
9 |
ydl_opts = {
|
10 |
-
'quiet': False,
|
11 |
-
'extract_flat':
|
12 |
'logger': logging.getLogger(), # Use the logging module to capture yt-dlp logs
|
13 |
'simulate': True,
|
14 |
-
'noplaylist': True, #
|
|
|
|
|
|
|
15 |
}
|
16 |
search_url = f"ytsearch{max_results}:{query}"
|
17 |
|
@@ -20,51 +23,60 @@ def youtube_search(query, max_results=50):
|
|
20 |
try:
|
21 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
22 |
result = ydl.extract_info(search_url, download=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
detailed_entries = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
for entry in result['entries']:
|
28 |
-
# Collect and print each field separately
|
29 |
-
print("\n==============================")
|
30 |
-
print("Video Details:")
|
31 |
-
print("==============================")
|
32 |
-
print(f"ID: {entry.get('id', 'N/A')}")
|
33 |
-
print(f"Title: {entry.get('title', 'N/A')}")
|
34 |
-
print(f"Uploader: {entry.get('uploader', 'N/A')}")
|
35 |
-
print(f"Uploader ID: {entry.get('uploader_id', 'N/A')}")
|
36 |
-
print(f"Upload Date: {entry.get('upload_date', 'N/A')}")
|
37 |
-
print(f"Description: {entry.get('description', 'N/A')}")
|
38 |
-
print(f"Duration (seconds): {entry.get('duration', 'N/A')}")
|
39 |
-
print(f"View Count: {entry.get('view_count', 'N/A')}")
|
40 |
-
print(f"Like Count: {entry.get('like_count', 'N/A')}")
|
41 |
-
print(f"Dislike Count: {entry.get('dislike_count', 'N/A')}")
|
42 |
-
print(f"Comment Count: {entry.get('comment_count', 'N/A')}")
|
43 |
-
print(f"Average Rating: {entry.get('average_rating', 'N/A')}")
|
44 |
-
print(f"Thumbnail URL: {entry.get('thumbnail', 'N/A')}")
|
45 |
-
print(f"Webpage URL: {entry.get('webpage_url', 'N/A')}")
|
46 |
-
print(f"Channel URL: {entry.get('channel_url', 'N/A')}")
|
47 |
-
print(f"Categories: {entry.get('categories', 'N/A')}")
|
48 |
-
print(f"Tags: {entry.get('tags', 'N/A')}")
|
49 |
-
print(f"Channel ID: {entry.get('channel_id', 'N/A')}")
|
50 |
-
print(f"Age Restriction: {entry.get('age_limit', 'N/A')}")
|
51 |
-
print(f"Is Live: {entry.get('is_live', 'N/A')}")
|
52 |
-
print(f"FPS: {entry.get('fps', 'N/A')}")
|
53 |
-
print(f"Resolution: {entry.get('resolution', 'N/A')}")
|
54 |
-
print(f"Aspect Ratio: {entry.get('aspect_ratio', 'N/A')}")
|
55 |
-
print(f"Video Codec: {entry.get('vcodec', 'N/A')}")
|
56 |
-
print(f"Audio Codec: {entry.get('acodec', 'N/A')}")
|
57 |
-
print("==============================\n")
|
58 |
|
59 |
-
# Add the entry details to the list if needed
|
60 |
-
detailed_entries.append(entry)
|
61 |
-
else:
|
62 |
-
logging.warning("No entries found in search result.")
|
63 |
return detailed_entries
|
|
|
|
|
|
|
|
|
|
|
64 |
except Exception as e:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
|
69 |
# Test the function with a sample query
|
70 |
youtube_search("test video")
|
|
|
7 |
# Function to search YouTube videos using yt-dlp for better reliability
|
8 |
def youtube_search(query, max_results=50):
|
9 |
ydl_opts = {
|
10 |
+
'quiet': False,
|
11 |
+
'extract_flat': False, # Set to False to retrieve full details of each video
|
12 |
'logger': logging.getLogger(), # Use the logging module to capture yt-dlp logs
|
13 |
'simulate': True,
|
14 |
+
'noplaylist': True, # Avoid playlist entries
|
15 |
+
'ignoreerrors': True, # Continue on errors without stopping
|
16 |
+
'force_generic_extractor': False, # Use the best extractor available
|
17 |
+
'default_search': 'ytsearch', # Default search method
|
18 |
}
|
19 |
search_url = f"ytsearch{max_results}:{query}"
|
20 |
|
|
|
23 |
try:
|
24 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
25 |
result = ydl.extract_info(search_url, download=False)
|
26 |
+
|
27 |
+
if not result or 'entries' not in result:
|
28 |
+
logging.warning("No entries found in search result.")
|
29 |
+
return []
|
30 |
+
|
31 |
+
entries = result['entries']
|
32 |
+
logging.debug(f"Number of entries found: {len(entries)}")
|
33 |
+
|
34 |
detailed_entries = []
|
35 |
+
for entry in entries:
|
36 |
+
# Collect and print each field separately
|
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 |
+
# Add the entry details to the list if needed
|
68 |
+
detailed_entries.append(entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
|
|
|
|
|
|
70 |
return detailed_entries
|
71 |
+
|
72 |
+
except yt_dlp.utils.DownloadError as e:
|
73 |
+
logging.error(f"DownloadError occurred: {e}")
|
74 |
+
except yt_dlp.utils.ExtractorError as e:
|
75 |
+
logging.error(f"ExtractorError occurred: {e}")
|
76 |
except Exception as e:
|
77 |
+
logging.error(f"An unexpected error occurred: {e}")
|
78 |
+
|
79 |
+
return []
|
80 |
|
81 |
# Test the function with a sample query
|
82 |
youtube_search("test video")
|