Skip to content

Commit

Permalink
feature/support-undownloadable-vimeo-links (#205)
Browse files Browse the repository at this point in the history
* updated vimeo downloader

* linter

* removed unneeded files

* throw error when invalid uri detected
  • Loading branch information
sagarrat7 committed Aug 24, 2022
1 parent 1e5b807 commit 8292c7a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cdp_backend/tests/conftest.py
Expand Up @@ -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:
Expand Down
Binary file modified cdp_backend/tests/resources/example_video.mp4
Binary file not shown.
12 changes: 12 additions & 0 deletions cdp_backend/tests/utils/test_file_utils.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
],
)
Expand All @@ -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'"
)
10 changes: 9 additions & 1 deletion cdp_backend/utils/file_utils.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8292c7a

Please sign in to comment.