Skip to content

Commit a5d433a

Browse files
committed
Add source parameter for upload
1 parent ab45460 commit a5d433a

File tree

6 files changed

+58
-25
lines changed

6 files changed

+58
-25
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ conn = videodb.connect(api_key="YOUR_API_KEY")
7474
Now that you have established a connection to VideoDB, you can upload your videos using `conn.upload()`.
7575
You can directly upload from `youtube`, `any public url`, `S3 bucket` or a `local file path`. A default collection is created when you create your first connection.
7676

77-
`upload` method returns a `Video` object.
77+
`upload` method returns a `Video` object. You can simply pass a single string
78+
representing either a local file path or a URL. For explicit calls, use the
79+
``source`` parameter.
7880

7981
```python
8082
# Upload a video by url

tests/test_upload.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from videodb._upload import upload
1+
from videodb._upload import upload, VideodbError
22

33

44
class DummyConn:
@@ -16,7 +16,17 @@ def post(self, path, data=None, show_progress=False):
1616
return {"id": "m-1", **(data or {})}
1717

1818

19-
def test_upload_infers_url():
19+
def test_upload_infers_url_from_source():
2020
conn = DummyConn()
21-
upload(conn, file_path="https://example.com/video.mp4")
21+
upload(conn, source="https://example.com/video.mp4")
2222
assert conn.data["url"] == "https://example.com/video.mp4"
23+
24+
25+
def test_upload_source_conflict():
26+
conn = DummyConn()
27+
try:
28+
upload(conn, source="/tmp/file.mp4", file_path="/tmp/file.mp4")
29+
except VideodbError:
30+
assert True
31+
else:
32+
assert False, "Expected VideodbError"

videodb/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""About information for videodb sdk"""
22

3-
__version__ = "0.2.16"
3+
__version__ = "0.2.17"
44
__title__ = "videodb"
55
__author__ = "videodb"
66
__email__ = "[email protected]"

videodb/_upload.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ def _is_url(path: str) -> bool:
2222

2323
def upload(
2424
_connection,
25-
file_path: str = None,
26-
url: str = None,
25+
source: str | None = None,
26+
file_path: str | None = None,
27+
url: str | None = None,
2728
media_type: Optional[str] = None,
2829
name: Optional[str] = None,
2930
description: Optional[str] = None,
3031
callback_url: Optional[str] = None,
3132
) -> dict:
33+
"""Upload a file or URL.
34+
35+
``source`` can be used as a generic argument which accepts either a local
36+
file path or a URL. ``file_path`` and ``url`` remain for backward
37+
compatibility and should not be used together with ``source``.
38+
"""
39+
if source and (file_path or url):
40+
raise VideodbError("source cannot be used with file_path or url")
41+
42+
if source and not file_path and not url:
43+
if _is_url(source):
44+
url = source
45+
else:
46+
file_path = source
3247
if file_path and not url and _is_url(file_path):
3348
url = file_path
3449
file_path = None

videodb/client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,20 @@ def get_transcode_details(self, job_id: str) -> dict:
256256

257257
def upload(
258258
self,
259-
file_path: str = None,
260-
url: str = None,
259+
source: str | None = None,
260+
file_path: str | None = None,
261+
url: str | None = None,
261262
media_type: Optional[str] = None,
262263
name: Optional[str] = None,
263264
description: Optional[str] = None,
264265
callback_url: Optional[str] = None,
265266
) -> Union[Video, Audio, Image, None]:
266267
"""Upload a file.
267268
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.
269+
The method automatically detects if ``source``/``file_path`` is a URL
270+
or a local path when only one of ``file_path`` or ``url`` is provided.
270271
272+
:param str source: Local path or URL of the file to upload (optional)
271273
:param str file_path: Path to the file to upload (optional)
272274
:param str url: URL of the file to upload (optional)
273275
:param MediaType media_type: MediaType object (optional)
@@ -279,12 +281,13 @@ def upload(
279281
"""
280282
upload_data = upload(
281283
self,
282-
file_path,
283-
url,
284-
media_type,
285-
name,
286-
description,
287-
callback_url,
284+
source,
285+
file_path=file_path,
286+
url=url,
287+
media_type=media_type,
288+
name=name,
289+
description=description,
290+
callback_url=callback_url,
288291
)
289292
media_id = upload_data.get("id", "")
290293
if media_id.startswith("m-"):

videodb/collection.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ def search_title(self, query) -> List[Video]:
428428

429429
def upload(
430430
self,
431+
source: str | None = None,
431432
file_path: str = None,
432433
url: Optional[str] = None,
433434
media_type: Optional[str] = None,
@@ -437,9 +438,10 @@ def upload(
437438
) -> Union[Video, Audio, Image, None]:
438439
"""Upload a file to the collection.
439440
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.
441+
The method automatically detects if ``source``/``file_path`` is a URL
442+
or a local path when only one of ``file_path`` or ``url`` is provided.
442443
444+
:param str source: Local path or URL of the file to be uploaded
443445
:param str file_path: Path to the file to be uploaded
444446
:param str url: URL of the file to be uploaded
445447
:param MediaType media_type: MediaType object (optional)
@@ -451,12 +453,13 @@ def upload(
451453
"""
452454
upload_data = upload(
453455
self._connection,
454-
file_path,
455-
url,
456-
media_type,
457-
name,
458-
description,
459-
callback_url,
456+
source,
457+
file_path=file_path,
458+
url=url,
459+
media_type=media_type,
460+
name=name,
461+
description=description,
462+
callback_url=callback_url,
460463
)
461464
media_id = upload_data.get("id", "")
462465
if media_id.startswith("m-"):

0 commit comments

Comments
 (0)