import subprocess import os def download_fb_content(url, cookies_path): """ Downloads content from a direct Facebook URL using gallery-dl. Args: url (str): The direct URL to the Facebook content. cookies_path (str): The path to the cookies.txt file. """ # First, check if the cookies file exists before attempting to download. if not os.path.exists(cookies_path): print(f"Error: Cookies file not found at '{cookies_path}'") print("Please ensure the file exists and the path is correct.") return # Stop the function if cookies are missing # --- Direct download logic --- print("-" * 20) print(f"Running gallery-dl for: {url}") print("-" * 20) cmd = [ "gallery-dl", "--cookies", cookies_path, url ] try: # Run the command and check for any errors during execution. subprocess.run(cmd, check=True) print("\nDownload completed successfully.") except FileNotFoundError: print("\n--- ERROR ---") print("Command 'gallery-dl' not found.") print("Please make sure gallery-dl is installed and accessible in your system's PATH.") except subprocess.CalledProcessError as e: print("\n--- ERROR ---") print(f"gallery-dl returned an error (Exit Code: {e.returncode}).") print("This could be due to an invalid URL, private content, or expired cookies.") print("Try running the command directly in your terminal for more detailed error messages.") # --- EXAMPLE --- # Set the path to your cookies file. # Make sure it's in the same directory as this script or provide the full path. cookies_file = "cookies.txt" print("--- Downloading Facebook Story Content ---") story_url = "https://www.facebook.com/stories/108639588358960/UzpfSVNDOjE1NjIwNDMzMjg0ODk0ODA=/?view_single=false" download_fb_content(story_url, cookies_file)