Skip to content

Commit ab45460

Browse files
committed
feat: infer upload source
1 parent 5416008 commit ab45460

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ You can directly upload from `youtube`, `any public url`, `S3 bucket` or a `loca
7878

7979
```python
8080
# Upload a video by url
81-
video = conn.upload(url="https://www.youtube.com/watch?v=WDv4AWk0J3U")
81+
video = conn.upload("https://www.youtube.com/watch?v=WDv4AWk0J3U")
8282

8383
# Upload a video from file system
84-
video_f = conn.upload(file_path="./my_video.mp4")
84+
video_f = conn.upload("./my_video.mp4")
8585

8686
```
8787

@@ -147,9 +147,9 @@ In the future you'll be able to index videos using:
147147
coll = conn.get_collection()
148148

149149
# Upload Videos to a collection
150-
coll.upload(url="https://www.youtube.com/watch?v=lsODSDmY4CY")
151-
coll.upload(url="https://www.youtube.com/watch?v=vZ4kOr38JhY")
152-
coll.upload(url="https://www.youtube.com/watch?v=uak_dXHh6s4")
150+
coll.upload("https://www.youtube.com/watch?v=lsODSDmY4CY")
151+
coll.upload("https://www.youtube.com/watch?v=vZ4kOr38JhY")
152+
coll.upload("https://www.youtube.com/watch?v=uak_dXHh6s4")
153153
```
154154

155155
- `conn.get_collection()` : Returns a Collection object; the default collection.

tests/test_upload.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from videodb._upload import upload
2+
3+
4+
class DummyConn:
5+
def __init__(self):
6+
self.collection_id = "default"
7+
self.data = None
8+
9+
def get(self, path, params=None, show_progress=False):
10+
# return fake upload url
11+
return {"upload_url": "http://upload"}
12+
13+
def post(self, path, data=None, show_progress=False):
14+
self.data = data
15+
# mimic api returning id and url
16+
return {"id": "m-1", **(data or {})}
17+
18+
19+
def test_upload_infers_url():
20+
conn = DummyConn()
21+
upload(conn, file_path="https://example.com/video.mp4")
22+
assert conn.data["url"] == "https://example.com/video.mp4"

videodb/__about__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
""" About information for videodb sdk"""
1+
"""About information for videodb sdk"""
22

3-
4-
5-
__version__ = "0.2.15"
3+
__version__ = "0.2.16"
64
__title__ = "videodb"
75
__author__ = "videodb"
86
__email__ = "[email protected]"

videodb/_upload.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import requests
22

33
from typing import Optional
4+
from urllib.parse import urlparse
45
from requests import HTTPError
6+
import os
57

68

79
from videodb._constants import (
@@ -13,6 +15,11 @@
1315
)
1416

1517

18+
def _is_url(path: str) -> bool:
19+
parsed = urlparse(path)
20+
return all([parsed.scheme in ("http", "https"), parsed.netloc])
21+
22+
1623
def upload(
1724
_connection,
1825
file_path: str = None,
@@ -22,6 +29,14 @@ def upload(
2229
description: Optional[str] = None,
2330
callback_url: Optional[str] = None,
2431
) -> dict:
32+
if file_path and not url and _is_url(file_path):
33+
url = file_path
34+
file_path = None
35+
36+
if not file_path and url and not _is_url(url) and os.path.exists(url):
37+
file_path = url
38+
url = None
39+
2540
if not file_path and not url:
2641
raise VideodbError("Either file_path or url is required")
2742
if file_path and url:

videodb/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def upload(
265265
) -> Union[Video, Audio, Image, None]:
266266
"""Upload a file.
267267
268+
The method automatically detects if ``file_path`` is a URL or a local
269+
path when only one of ``file_path`` or ``url`` is provided.
270+
268271
:param str file_path: Path to the file to upload (optional)
269272
:param str url: URL of the file to upload (optional)
270273
:param MediaType media_type: MediaType object (optional)

videodb/collection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ def upload(
437437
) -> Union[Video, Audio, Image, None]:
438438
"""Upload a file to the collection.
439439
440+
The method automatically detects if ``file_path`` is a URL or a local
441+
path when only one of ``file_path`` or ``url`` is provided.
442+
440443
:param str file_path: Path to the file to be uploaded
441444
:param str url: URL of the file to be uploaded
442445
:param MediaType media_type: MediaType object (optional)

0 commit comments

Comments
 (0)