From 8292c7ad680c3d7093e15c6133fddda55baf9cf5 Mon Sep 17 00:00:00 2001 From: Sarah <37188711+sagarrat7@users.noreply.github.com> Date: Tue, 23 Aug 2022 21:19:52 -0700 Subject: [PATCH] feature/support-undownloadable-vimeo-links (#205) * updated vimeo downloader * linter * removed unneeded files * throw error when invalid uri detected --- cdp_backend/tests/conftest.py | 3 +++ cdp_backend/tests/resources/example_video.mp4 | Bin 8378175 -> 8378175 bytes cdp_backend/tests/utils/test_file_utils.py | 12 ++++++++++++ cdp_backend/utils/file_utils.py | 10 +++++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cdp_backend/tests/conftest.py b/cdp_backend/tests/conftest.py index e1a96e8c..7e2e3100 100644 --- a/cdp_backend/tests/conftest.py +++ b/cdp_backend/tests/conftest.py @@ -40,6 +40,9 @@ def resources_dir() -> Path: # City of Versailles, Kentucky EXAMPLE_VIMEO = "https://vimeo.com/503166067" +# City of Chicago, Illinois +EXAMPLE_VIMEO_SHOWCASE = "https://vimeo.com/showcase/6277394/video/722690793" + @pytest.fixture def example_video(resources_dir: Path) -> Path: diff --git a/cdp_backend/tests/resources/example_video.mp4 b/cdp_backend/tests/resources/example_video.mp4 index 5dd374c1bc42491658519be6542ef747539874f2..884d1e6e31c23bd31dd11cbd7a96ca70d4c1ff11 100644 GIT binary patch delta 798 zcmW;IT})DO7{~GHNqaHV5G~&VOA9mb@FtQb7L@^}MSxaVh(H$DTwATHr>#xrWUafrvI}pzao%{7&CNUeJzRV)&i8rF+5dl*KJN)j@TM)tpzPC& zW~0*I84MakF&AudaW~A#)ep<$E$71n0}40!~wK{>;KVj=2pgsD!hW+fX&!%5i$K3eM5=r25|v4l$Po zszIcfOItV3ad}q@I?&VduA#A>qhK^b4VXwU)-G0aTnW^I8P3y6pl0pR=9s^PIEuKlAT5gn4+;oL8yo ztu$Fo8gnpUX>Yg0LxwEzbNKdFLUup9QkUML{{WZ}Sr>Q1YO2rVO-jkUD1$&A#HOsMA3?941AbexQlt_^2L^(D|SlD`Zo-e>wU6TE81S;Du(=`dhLpc%U0=fveC;y0L5e z%y)adKFQ;4gX?gEJYIjSfVWc-KeR)DQjv}iYaB-cLFj}KjRbC5zH>~9VYmgip^K7Y zx3Q21>&|Yt19xfN8PUDrSh5mQQ(u0!%Ku+vQcB$q129M_^^odwwrH99 IvozHB4;kk;D*ylh diff --git a/cdp_backend/tests/utils/test_file_utils.py b/cdp_backend/tests/utils/test_file_utils.py index 2f3c17a0..fe4f80ab 100644 --- a/cdp_backend/tests/utils/test_file_utils.py +++ b/cdp_backend/tests/utils/test_file_utils.py @@ -26,6 +26,7 @@ EXAMPLE_VIDEO_FILENAME, EXAMPLE_VIDEO_HD_FILENAME, EXAMPLE_VIMEO, + EXAMPLE_VIMEO_SHOWCASE, EXAMPLE_YOUTUBE_VIDEO_EMBEDDED, EXAMPLE_YOUTUBE_VIDEO_PARAMETER, EXAMPLE_YOUTUBE_VIDEO_SHORT, @@ -247,6 +248,7 @@ def test_convert_video_to_mp4( (EXAMPLE_YOUTUBE_VIDEO_PARAMETER, "XALBGkjkUPQ.mp4"), (EXAMPLE_YOUTUBE_VIDEO_SHORT, "XALBGkjkUPQ.mp4"), (EXAMPLE_VIMEO, Path("503166067") / "503166067.mp4"), + (EXAMPLE_VIMEO_SHOWCASE, Path("722690793") / "722690793.mp4"), (EXAMPLE_M3U8_PLAYLIST_URI, None), ], ) @@ -264,3 +266,13 @@ def test_remote_resource_copy( assert Path(actual_uri).is_file() os.remove(actual_uri) + + +def test_invalid_uri() -> None: + with pytest.raises(Exception) as e: + file_utils.resource_copy("https://vimeo.com/fakeuri") + assert e.type == ValueError + assert ( + str(e.value) + == "Could not extract video id from uri: 'https://vimeo.com/fakeuri'" + ) diff --git a/cdp_backend/utils/file_utils.py b/cdp_backend/utils/file_utils.py index cf4d18eb..69a081e3 100644 --- a/cdp_backend/utils/file_utils.py +++ b/cdp_backend/utils/file_utils.py @@ -4,6 +4,7 @@ import logging import math import random +import re from hashlib import sha256 from pathlib import Path from typing import Optional, Tuple, Union @@ -226,7 +227,14 @@ def vimeo_copy(uri: str, dst: Path, overwrite: bool = False) -> str: if dst.is_file() and not overwrite: raise FileExistsError(dst) - v = Vimeo(uri) + # 9 is the length of the video id; no other + # string of numbers in the URL has length 9 (thankfully) + match = re.findall(r"\d{9}", uri) + if len(match) == 0: + raise ValueError(f"Could not extract video id from uri: '{uri}'") + + vid = match[0] + v = Vimeo.from_video_id(video_id=vid) if len(v.streams) == 0: raise ValueError("File {} contains no downloadable streams", uri)