Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,56 +2,92 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Ensure npm dependencies are installed before starting (just in case)
|
| 6 |
-
subprocess.run(["npm", "install"], check=True)
|
| 7 |
-
|
| 8 |
def search_pinterest(query):
|
| 9 |
if not query:
|
| 10 |
-
return None, "Please enter a search
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
print(f"
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
# Run the
|
| 19 |
-
# We
|
| 20 |
result = subprocess.run(
|
| 21 |
-
["node", "scraper.js",
|
| 22 |
capture_output=True,
|
| 23 |
text=True,
|
| 24 |
-
timeout=
|
| 25 |
)
|
| 26 |
|
| 27 |
-
output
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
if
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return None, output
|
| 36 |
else:
|
| 37 |
-
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("# 📌 Pinterest Video
|
| 45 |
-
gr.Markdown("
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def search_pinterest(query):
|
| 6 |
if not query:
|
| 7 |
+
return None, "⚠️ Please enter a search keyword."
|
| 8 |
|
| 9 |
+
# Auto-add 'video' to help Pinterest search results
|
| 10 |
+
search_query = query if "video" in query.lower() else f"{query} video"
|
| 11 |
+
|
| 12 |
+
print(f"--- Starting Scraper for: {search_query} ---")
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# Run the Node.js scraper
|
| 16 |
+
# We set a 90-second timeout because Puppeteer can be slow on cloud servers
|
| 17 |
result = subprocess.run(
|
| 18 |
+
["node", "scraper.js", search_query],
|
| 19 |
capture_output=True,
|
| 20 |
text=True,
|
| 21 |
+
timeout=90
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Capture all output from Node.js
|
| 25 |
+
stdout_content = result.stdout.strip()
|
| 26 |
+
stderr_content = result.stderr.strip()
|
| 27 |
|
| 28 |
+
# Log to Hugging Face console for debugging
|
| 29 |
+
if stderr_content:
|
| 30 |
+
print(f"Debug Logs:\n{stderr_content}")
|
| 31 |
+
|
| 32 |
+
# Extract the URL from stdout
|
| 33 |
+
# We split by lines and look for the one starting with 'http'
|
| 34 |
+
lines = stdout_content.split('\n')
|
| 35 |
+
video_url = None
|
| 36 |
+
for line in lines:
|
| 37 |
+
clean_line = line.strip()
|
| 38 |
+
if clean_line.startswith("http"):
|
| 39 |
+
video_url = clean_line
|
| 40 |
+
break
|
| 41 |
|
| 42 |
+
if video_url:
|
| 43 |
+
print(f"✅ Success! URL Found: {video_url}")
|
| 44 |
+
return video_url, f"Found video for: {query}"
|
|
|
|
| 45 |
else:
|
| 46 |
+
print("❌ No URL found in Node output.")
|
| 47 |
+
return None, f"Could not find a video. Output received: {stdout_content[:100]}..."
|
| 48 |
|
| 49 |
+
except subprocess.TimeoutExpired:
|
| 50 |
+
print("❌ Scraper timed out.")
|
| 51 |
+
return None, "The search took too long. Please try again with a simpler keyword."
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"❌ System Error: {str(e)}")
|
| 54 |
+
return None, f"System Error: {str(e)}"
|
| 55 |
|
| 56 |
+
# Define the Gradio UI Theme and Layout
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 58 |
+
gr.Markdown("# 📌 Pinterest Video Downloader")
|
| 59 |
+
gr.Markdown("Enter a topic below. The system will search Pinterest and return the best quality video found.")
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
+
with gr.Column(scale=4):
|
| 63 |
+
input_text = gr.Textbox(
|
| 64 |
+
label="What are you looking for?",
|
| 65 |
+
placeholder="e.g. Luxury Car, Funny Cat, Nature Aesthetic...",
|
| 66 |
+
lines=1
|
| 67 |
+
)
|
| 68 |
+
with gr.Column(scale=1):
|
| 69 |
+
submit_btn = gr.Button("🔍 Search Video", variant="primary")
|
| 70 |
|
| 71 |
+
with gr.Row():
|
| 72 |
+
# The Video component handles the .mp4 URL directly
|
| 73 |
+
video_display = gr.Video(label="Result Video")
|
| 74 |
+
|
| 75 |
+
status_msg = gr.Textbox(label="Status Log", interactive=False)
|
| 76 |
|
| 77 |
+
# Link the button to the function
|
| 78 |
+
submit_btn.click(
|
| 79 |
+
fn=search_pinterest,
|
| 80 |
+
inputs=input_text,
|
| 81 |
+
outputs=[video_display, status_msg]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Allow pressing "Enter" to search
|
| 85 |
+
input_text.submit(
|
| 86 |
+
fn=search_pinterest,
|
| 87 |
+
inputs=input_text,
|
| 88 |
+
outputs=[video_display, status_msg]
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# Launch the app
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.launch()
|