Skip to content

Video editing with Python

Add tests for 'download_webfile' (#1595)

Álvaro Mondéjarc9c781b5ecfb
diff --git a/moviepy/video/io/downloader.py b/moviepy/video/io/downloader.py
index 68b14a1f..293c86fe 100644
--- a/moviepy/video/io/downloader.py
+++ b/moviepy/video/io/downloader.py
@@ -8,13 +8,34 @@ from moviepy.tools import subprocess_call
def download_webfile(url, filename, overwrite=False):
- """Small utility to download the file at 'url' under name 'filename'.
+ """Small utility to download the file at ``url`` under name ``filename``.
- - If url is a youtube video ID like z410eauCnH it will download the video
+ Parameters
+ ----------
+
+ url : str
+ If url is a youtube video ID like z410eauCnH it will download the video
using youtube-dl. Requires youtube-dl (pip install youtube-dl).
- - If the filename already exists and overwrite=False, nothing will happen.
+
+ filename : str
+ Path to the new downloaded file location.
+
+ overwrite : bool, optional
+ If the filename already exists and overwrite=False, nothing will happen.
+ Use it to force destination file overwriting.
+
+ Examples
+ --------
+
+ >>> from moviepy.io.downloader import download_website
+ >>>
+ >>> download_website(
+ ... "http://localhost:8000/media/chaplin.mp4",
+ ... "media/chaplin-copy.mp4",
+ ... )
+ >>>
"""
- if os.path.exists(filename) and not overwrite:
+ if os.path.exists(filename) and not overwrite: # pragma: no cover
return
if "." in url:
@@ -26,7 +47,10 @@ def download_webfile(url, filename, overwrite=False):
subprocess_call(["youtube-dl", url, "-o", filename])
except OSError as e:
raise OSError(
- e.message + "\n A possible reason is that youtube-dl"
- " is not installed on your computer. Install it with "
- ' "pip install youtube_dl"'
+ (
+ "Error running youtube-dl.\n%sA possible reason is that"
+ " youtube-dl is not installed on your computer. Install it "
+ " with 'pip install youtube_dl'."
+ )
+ % ((e.message + "\n" if hasattr(e, "message") else ""))
)
diff --git a/tests/test_helper.py b/tests/test_helper.py
index deec503b..ecfb34e2 100644
--- a/tests/test_helper.py
+++ b/tests/test_helper.py
@@ -1,8 +1,12 @@
"""Define general test helper attributes and utilities."""
+import contextlib
import functools
+import http.server
+import socketserver
import sys
import tempfile
+import threading
import numpy as np
@@ -44,3 +48,14 @@ def get_mono_wave(freq=440):
return np.sin(freq * 2 * np.pi * t)
return make_mono_frame
+
+
+def static_files_server(port=8000):
+ class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
+ pass
+
+ my_server = socketserver.TCPServer(("", port), MyHttpRequestHandler)
+ thread = threading.Thread(target=my_server.serve_forever, daemon=True)
+ thread.start()
+ yield thread
diff --git a/tests/test_tools.py b/tests/test_tools.py
index 028e112d..b23ca229 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -1,5 +1,6 @@
"""Tool tests meant to be run with pytest. Taken from PR #121 (grimley517)."""
+import filecmp
import importlib
import os
import shutil
@@ -7,6 +8,9 @@ import shutil
import pytest
import moviepy.tools as tools
+from moviepy.video.io.downloader import download_webfile
+
+from tests.test_helper import TMP_DIR, static_files_server
@pytest.mark.parametrize(
@@ -91,5 +95,44 @@ def test_deprecated_version_of(old_name):
assert record[0].message.args[0] == expected_warning_message
+ ("url", "expected_result"),
+ (
+ (
+ "http://localhost:8000/media/chaplin.mp4",
+ os.path.join("media", "chaplin.mp4"),
+ ),
+ ("foobarbazimpossiblecode", OSError),
+ ),
+)
+def test_download_webfile(url, expected_result):
+ filename = os.path.join(TMP_DIR, "moviepy_downloader_test.mp4")
+ if os.path.isfile(filename):
+ os.remove(filename)
+
+ if hasattr(expected_result, "__traceback__") or len(url) == 11:
+ if not shutil.which("youtube-dl"):
+ with pytest.raises(expected_result):
+ download_webfile(url, filename)
+ assert not os.path.isfile(filename)
+ elif len(url) != 11:
+ with pytest.raises(OSError) as exc:
+ download_webfile(url, filename)
+ assert "Error running youtube-dl." in str(exc.value)
+ assert not os.path.isfile(filename)
+ else:
+ download_webfile(url, filename)
+ assert os.path.isfile(filename)
+ else:
+ # network files
+ with static_files_server():
+ download_webfile(url, filename)
+
+ assert filecmp.cmp(filename, expected_result)
+
+ if os.path.isfile(filename):
+ os.remove(filename)
+
+
if __name__ == "__main__":
pytest.main()