Skip to content

Latest commit

 

History

History
115 lines (82 loc) · 2.4 KB

phase-09.md

File metadata and controls

115 lines (82 loc) · 2.4 KB

Phase 09: Make the downloader safer (lab extension)

Goal

Improve the downloader so one failed video does not stop the whole program.

In real data projects, downloads can fail because:

  • a URL is broken
  • the network is slow
  • YouTube blocks or delays a request
  • the video is unavailable
  • the request takes too long

Your app should handle these problems and keep going.

Steps

  1. Add simple error handling to the download function in solutions/library.py.
  2. Add a timeout to the yt-dlp options.
  3. Return a result dictionary for each download.
  4. Record whether each download succeeded or failed.
  5. Update the Markdown report with any failed URLs.

Add safer yt-dlp options

You can add a socket timeout to the yt-dlp options:

ydl_options = {
    "outtmpl": "videos/%(title)s.%(ext)s",
    "socket_timeout": 30,
}

This means yt-dlp should not wait forever if the connection is too slow.

Use try and except

Wrap the download code so the program can recover from an error:

def download_video(url):
    ydl_options = {
        "outtmpl": "videos/%(title)s.%(ext)s",
        "socket_timeout": 30,
    }

    try:
        with yt_dlp.YoutubeDL(ydl_options) as ydl:
            ydl.download([url])

        return {
            "url": url,
            "status": "success",
            "error": "",
        }

    except Exception as error:
        return {
            "url": url,
            "status": "failed",
            "error": str(error),
        }

Keep downloading after an error

When you download multiple videos, store each result:

results = []

for url in urls:
    result = download_video(url)
    results.append(result)

Then print or report the failed downloads:

for result in results:
    if result["status"] == "failed":
        print("Failed:", result["url"])
        print("Error:", result["error"])

Update the report

Add a new section to reports/sequential_report.md:

## Download status

Successful downloads:
Failed downloads:

If any videos failed, include the URL and the error message.

Checkpoint

Run:

python solutions/main.py

Confirm that:

  • The app does not crash if one video fails.
  • The app keeps trying the remaining videos.
  • Each download returns a success or failed status.
  • Failed URLs are printed in the terminal or added to the report.
  • The report includes a download status section.