Fix instagram download

This commit is contained in:
2025-12-04 18:35:25 +03:00
parent 4752183ed9
commit 97c85572e9

View File

@@ -312,6 +312,11 @@ async def download_media(
'writesubtitles': False,
'writeautomaticsub': False,
'ignoreerrors': False,
# Network settings for better reliability
'socket_timeout': 60, # Increase socket timeout to 60 seconds
'retries': 3, # Retry failed downloads up to 3 times
'fragment_retries': 3, # Retry failed fragments
'file_access_retries': 3, # Retry file access errors
}
# Check if Node.js is available for JS extraction (required for Instagram, TikTok, YouTube, etc.)
@@ -319,27 +324,31 @@ async def download_media(
nodejs_path = shutil.which('node')
if nodejs_path:
logger.info(f"Node.js found at: {nodejs_path}. JS extraction will be available.")
# yt-dlp will automatically use Node.js if available
# For YouTube, we can explicitly set extractor args to use Node.js
# This helps avoid warnings about missing JS runtime
if 'youtube.com' in url or 'youtu.be' in url:
ydl_opts['extractor_args'] = {
'youtube': {
'player_client': ['android', 'web'], # Use clients that don't require JS
}
}
else:
logger.warning(
"Node.js not found. Some sites (Instagram, TikTok, YouTube, etc.) may require JS extraction. "
"Install Node.js for full functionality."
)
# For YouTube without Node.js, use extractor args to avoid warnings
if 'youtube.com' in url or 'youtu.be' in url:
ydl_opts['extractor_args'] = {
'youtube': {
'player_client': ['android', 'web'], # Use clients that don't require JS
}
}
# Configure extractor args for specific sites
ydl_opts['extractor_args'] = {}
# YouTube settings
if 'youtube.com' in url or 'youtu.be' in url:
ydl_opts['extractor_args']['youtube'] = {
'player_client': ['android', 'web'], # Use clients that don't require JS
}
# Instagram settings - improve reliability
if 'instagram.com' in url:
ydl_opts['extractor_args']['instagram'] = {
# Use mobile API for better reliability
'api': ['mobile'],
}
# Increase timeout specifically for Instagram
ydl_opts['socket_timeout'] = 120 # 2 minutes for Instagram
ydl_opts['retries'] = 5 # More retries for Instagram
logger.info("Instagram URL detected, using optimized settings with increased timeouts and retries")
# Add cookies if specified (for Instagram and other sites)
if cookies_file:
@@ -405,17 +414,51 @@ async def download_media(
# but the video file is still downloaded correctly
try:
ydl.download([url])
except Exception as postprocess_error:
except Exception as download_error:
error_msg = str(download_error)
error_lower = error_msg.lower()
# Check if it's just a postprocessing error (video is already downloaded)
error_msg = str(postprocess_error)
if "Postprocessing" in error_msg or "aspect ratio" in error_msg.lower():
if "Postprocessing" in error_msg or "aspect ratio" in error_lower:
logger.warning(
f"Postprocessing error (non-critical): {error_msg}. "
f"Video file should still be available. Will check file existence."
)
# Don't raise - video is likely already downloaded
# Check for Instagram-specific errors
elif 'instagram.com' in url:
if 'timeout' in error_lower or 'timed out' in error_lower:
logger.error(f"Instagram download timeout: {error_msg}")
raise Exception(
f"Instagram download timeout. This may be due to:\n"
f"- Network issues\n"
f"- Instagram rate limiting\n"
f"- Missing or expired cookies\n"
f"Please try again later or check your cookies file."
)
elif 'incompleteread' in error_lower or 'incomplete read' in error_lower:
logger.error(f"Instagram incomplete read: {error_msg}")
raise Exception(
f"Instagram download incomplete. This may be due to:\n"
f"- Network instability\n"
f"- Instagram server issues\n"
f"Please try again."
)
elif 'unable to download webpage' in error_lower:
logger.error(f"Instagram webpage download failed: {error_msg}")
raise Exception(
f"Failed to access Instagram content. This may be due to:\n"
f"- Private or deleted post\n"
f"- Missing or expired cookies\n"
f"- Instagram blocking requests\n"
f"Please check the URL and cookies file."
)
else:
# Other Instagram errors
logger.error(f"Instagram download error: {error_msg}")
raise Exception(f"Instagram download failed: {error_msg}")
else:
# Real error, re-raise
# Real error for other sites, re-raise
raise
return info