Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import requests
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import logging
|
|
|
5 |
|
6 |
# Configure logging for debugging purposes
|
7 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -23,26 +24,47 @@ def youtube_search(query, max_results=10):
|
|
23 |
# Parse the HTML response using BeautifulSoup
|
24 |
soup = BeautifulSoup(response.text, "html.parser")
|
25 |
|
26 |
-
#
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
gallery_items.append((thumbnail_url, video_title, video_url))
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
except requests.exceptions.RequestException as e:
|
43 |
error_message = f"Request error occurred: {e}"
|
44 |
logging.error(error_message)
|
45 |
return [], error_message
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
error_message = f"An unexpected error occurred: {e}"
|
48 |
logging.error(error_message)
|
@@ -59,18 +81,4 @@ def display_videos(query):
|
|
59 |
return formatted_gallery, ""
|
60 |
|
61 |
# Gradio interface
|
62 |
-
with gr
|
63 |
-
gr.Markdown("## YouTube Video Search (Without API)")
|
64 |
-
with gr.Row():
|
65 |
-
with gr.Column(scale=2):
|
66 |
-
search_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
|
67 |
-
search_button = gr.Button("Search")
|
68 |
-
with gr.Column(scale=4):
|
69 |
-
search_output = gr.Gallery(label="Search Results", columns=2)
|
70 |
-
error_output = gr.Textbox(label="Error Message", interactive=False, visible=False)
|
71 |
-
|
72 |
-
# Define search button behavior
|
73 |
-
search_button.click(display_videos, inputs=search_input, outputs=[search_output, error_output])
|
74 |
-
|
75 |
-
# Launch the Gradio interface with public sharing enabled
|
76 |
-
demo.launch(share=True)
|
|
|
1 |
import requests
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
import logging
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
|
7 |
# Configure logging for debugging purposes
|
8 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
24 |
# Parse the HTML response using BeautifulSoup
|
25 |
soup = BeautifulSoup(response.text, "html.parser")
|
26 |
|
27 |
+
# Look for the initial JSON data block within the script tags
|
28 |
+
scripts = soup.find_all("script")
|
29 |
+
for script in scripts:
|
30 |
+
if 'var ytInitialData = ' in script.text:
|
31 |
+
json_text = script.text.split('var ytInitialData = ')[1].split("};")[0] + "}"
|
32 |
+
data = json.loads(json_text)
|
33 |
|
34 |
+
# Traverse through the JSON to find video entries
|
35 |
+
video_items = []
|
36 |
+
contents = data['contents']['twoColumnSearchResultsRenderer']['primaryContents']['sectionListRenderer']['contents']
|
37 |
+
for content in contents:
|
38 |
+
video_entries = content.get('itemSectionRenderer', {}).get('contents', [])
|
39 |
+
for entry in video_entries:
|
40 |
+
video_renderer = entry.get('videoRenderer')
|
41 |
+
if video_renderer:
|
42 |
+
video_id = video_renderer.get('videoId', 'N/A')
|
43 |
+
video_title = video_renderer.get('title', {}).get('runs', [{}])[0].get('text', 'N/A')
|
44 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
45 |
+
thumbnail_url = video_renderer.get('thumbnail', {}).get('thumbnails', [{}])[0].get('url', 'N/A')
|
46 |
|
47 |
+
video_items.append((thumbnail_url, video_title, video_url))
|
|
|
48 |
|
49 |
+
if len(video_items) >= max_results:
|
50 |
+
break
|
51 |
+
|
52 |
+
if video_items:
|
53 |
+
return video_items, "" # Return the list of video items and no error message
|
54 |
+
else:
|
55 |
+
logging.warning("No video entries found.")
|
56 |
+
return [], "No video entries found."
|
57 |
+
logging.warning("JSON data block not found in the page.")
|
58 |
+
return [], "Unable to find video data."
|
59 |
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
error_message = f"Request error occurred: {e}"
|
62 |
logging.error(error_message)
|
63 |
return [], error_message
|
64 |
+
except json.JSONDecodeError as e:
|
65 |
+
error_message = f"JSON decoding error occurred: {e}"
|
66 |
+
logging.error(error_message)
|
67 |
+
return [], error_message
|
68 |
except Exception as e:
|
69 |
error_message = f"An unexpected error occurred: {e}"
|
70 |
logging.error(error_message)
|
|
|
81 |
return formatted_gallery, ""
|
82 |
|
83 |
# Gradio interface
|
84 |
+
with gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|