Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,12 +22,19 @@ mobile_emulation = {
|
|
| 22 |
|
| 23 |
def get_video_stream(target_url):
|
| 24 |
chrome_options = Options()
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
chrome_options.add_argument('--no-sandbox')
|
| 27 |
chrome_options.add_argument('--disable-dev-shm-usage')
|
| 28 |
chrome_options.add_argument('--disable-gpu')
|
| 29 |
chrome_options.add_argument('--window-size=360,640')
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Critical for Docker: Write user data to a writable temporary location
|
| 32 |
user_data_dir = f'/tmp/chrome-user-data-{int(time.time())}'
|
| 33 |
chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
|
|
@@ -40,23 +47,26 @@ def get_video_stream(target_url):
|
|
| 40 |
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 41 |
|
| 42 |
try:
|
| 43 |
-
# Go directly to the target URL
|
|
|
|
| 44 |
driver.get(target_url)
|
| 45 |
|
| 46 |
-
# Wait for video element
|
| 47 |
wait = WebDriverWait(driver, 15)
|
| 48 |
video_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "video")))
|
| 49 |
|
| 50 |
video_src = video_element.get_attribute('src')
|
| 51 |
if not video_src:
|
| 52 |
raise Exception("No video src found")
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Download video content into memory
|
| 55 |
response = requests.get(video_src)
|
| 56 |
if response.status_code == 200:
|
| 57 |
return io.BytesIO(response.content)
|
| 58 |
else:
|
| 59 |
-
raise Exception("Failed to download video file")
|
| 60 |
|
| 61 |
finally:
|
| 62 |
driver.quit()
|
|
@@ -81,6 +91,7 @@ def download_route(link):
|
|
| 81 |
download_name=f'instagram_video_{int(time.time())}.mp4'
|
| 82 |
)
|
| 83 |
except Exception as e:
|
|
|
|
| 84 |
return jsonify({"error": str(e)}), 500
|
| 85 |
|
| 86 |
@app.route('/')
|
|
|
|
| 22 |
|
| 23 |
def get_video_stream(target_url):
|
| 24 |
chrome_options = Options()
|
| 25 |
+
|
| 26 |
+
# Core Headless Settings
|
| 27 |
+
chrome_options.add_argument('--headless=new') # Updated headless syntax
|
| 28 |
chrome_options.add_argument('--no-sandbox')
|
| 29 |
chrome_options.add_argument('--disable-dev-shm-usage')
|
| 30 |
chrome_options.add_argument('--disable-gpu')
|
| 31 |
chrome_options.add_argument('--window-size=360,640')
|
| 32 |
|
| 33 |
+
# Network & DNS fixes for Docker/Hugging Face
|
| 34 |
+
chrome_options.add_argument('--dns-prefetch-disable')
|
| 35 |
+
chrome_options.add_argument('--disable-features=NetworkService')
|
| 36 |
+
chrome_options.add_argument('--disable-features=VizDisplayCompositor')
|
| 37 |
+
|
| 38 |
# Critical for Docker: Write user data to a writable temporary location
|
| 39 |
user_data_dir = f'/tmp/chrome-user-data-{int(time.time())}'
|
| 40 |
chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
|
|
|
|
| 47 |
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
# Go directly to the target URL
|
| 51 |
+
print(f"Navigating to: {target_url}")
|
| 52 |
driver.get(target_url)
|
| 53 |
|
| 54 |
+
# Wait for video element
|
| 55 |
wait = WebDriverWait(driver, 15)
|
| 56 |
video_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "video")))
|
| 57 |
|
| 58 |
video_src = video_element.get_attribute('src')
|
| 59 |
if not video_src:
|
| 60 |
raise Exception("No video src found")
|
| 61 |
+
|
| 62 |
+
print(f"Found video SRC: {video_src}")
|
| 63 |
|
| 64 |
# Download video content into memory
|
| 65 |
response = requests.get(video_src)
|
| 66 |
if response.status_code == 200:
|
| 67 |
return io.BytesIO(response.content)
|
| 68 |
else:
|
| 69 |
+
raise Exception(f"Failed to download video file: {response.status_code}")
|
| 70 |
|
| 71 |
finally:
|
| 72 |
driver.quit()
|
|
|
|
| 91 |
download_name=f'instagram_video_{int(time.time())}.mp4'
|
| 92 |
)
|
| 93 |
except Exception as e:
|
| 94 |
+
print(f"Error: {e}")
|
| 95 |
return jsonify({"error": str(e)}), 500
|
| 96 |
|
| 97 |
@app.route('/')
|