Skip to content

Commit 6ee9d6f

Browse files
committed
Improve upload source detection in Conn.upload() and coll.upload()
1 parent 5416008 commit 6ee9d6f

File tree

5 files changed

+66
-32
lines changed

5 files changed

+66
-32
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ 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.
7879

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

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

8687
```
8788

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

149150
# 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")
151+
coll.upload("https://www.youtube.com/watch?v=lsODSDmY4CY")
152+
coll.upload("https://www.youtube.com/watch?v=vZ4kOr38JhY")
153+
coll.upload("https://www.youtube.com/watch?v=uak_dXHh6s4")
153154
```
154155

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

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: 31 additions & 2 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,15 +15,42 @@
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,
18-
file_path: str = None,
19-
url: str = None,
25+
source: Optional[str] = None,
2026
media_type: Optional[str] = None,
2127
name: Optional[str] = None,
2228
description: Optional[str] = None,
2329
callback_url: Optional[str] = None,
30+
file_path: Optional[str] = None,
31+
url: Optional[str] = None,
2432
) -> 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.
37+
"""
38+
if source and (file_path or url):
39+
raise VideodbError("source cannot be used with file_path or url")
40+
41+
if source and not file_path and not url:
42+
if _is_url(source):
43+
url = source
44+
else:
45+
file_path = source
46+
if file_path and not url and _is_url(file_path):
47+
url = file_path
48+
file_path = None
49+
50+
if not file_path and url and not _is_url(url) and os.path.exists(url):
51+
file_path = url
52+
url = None
53+
2554
if not file_path and not url:
2655
raise VideodbError("Either file_path or url is required")
2756
if file_path and url:

videodb/client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,32 +256,35 @@ 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: Optional[str] = None,
261260
media_type: Optional[str] = None,
262261
name: Optional[str] = None,
263262
description: Optional[str] = None,
264263
callback_url: Optional[str] = None,
264+
file_path: Optional[str] = None,
265+
url: Optional[str] = None,
265266
) -> Union[Video, Audio, Image, None]:
266267
"""Upload a file.
267268
268-
:param str file_path: Path to the file to upload (optional)
269-
:param str url: URL of the file to upload (optional)
269+
:param str source: Local path or URL of the file to upload (optional)
270270
:param MediaType media_type: MediaType object (optional)
271271
:param str name: Name of the file (optional)
272272
:param str description: Description of the file (optional)
273273
:param str callback_url: URL to receive the callback (optional)
274+
:param str file_path: Path to the file to upload (optional)
275+
:param str url: URL of the file to upload (optional)
274276
:return: :class:`Video <Video>`, or :class:`Audio <Audio>`, or :class:`Image <Image>` object
275277
:rtype: Union[ :class:`videodb.video.Video`, :class:`videodb.audio.Audio`, :class:`videodb.image.Image`]
276278
"""
277279
upload_data = upload(
278280
self,
279-
file_path,
280-
url,
281-
media_type,
282-
name,
283-
description,
284-
callback_url,
281+
source,
282+
media_type=media_type,
283+
name=name,
284+
description=description,
285+
callback_url=callback_url,
286+
file_path=file_path,
287+
url=url,
285288
)
286289
media_id = upload_data.get("id", "")
287290
if media_id.startswith("m-"):

videodb/collection.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,32 +428,35 @@ def search_title(self, query) -> List[Video]:
428428

429429
def upload(
430430
self,
431-
file_path: str = None,
432-
url: Optional[str] = None,
431+
source: Optional[str] = None,
433432
media_type: Optional[str] = None,
434433
name: Optional[str] = None,
435434
description: Optional[str] = None,
436435
callback_url: Optional[str] = None,
436+
file_path: Optional[str] = None,
437+
url: Optional[str] = None,
437438
) -> Union[Video, Audio, Image, None]:
438439
"""Upload a file to the collection.
439440
440-
:param str file_path: Path to the file to be uploaded
441-
:param str url: URL of the file to be uploaded
441+
:param str source: Local path or URL of the file to be uploaded
442442
:param MediaType media_type: MediaType object (optional)
443443
:param str name: Name of the file (optional)
444444
:param str description: Description of the file (optional)
445445
:param str callback_url: URL to receive the callback (optional)
446+
:param str file_path: Path to the file to be uploaded
447+
:param str url: URL of the file to be uploaded
446448
:return: :class:`Video <Video>`, or :class:`Audio <Audio>`, or :class:`Image <Image>` object
447449
:rtype: Union[ :class:`videodb.video.Video`, :class:`videodb.audio.Audio`, :class:`videodb.image.Image`]
448450
"""
449451
upload_data = upload(
450452
self._connection,
451-
file_path,
452-
url,
453-
media_type,
454-
name,
455-
description,
456-
callback_url,
453+
source,
454+
media_type=media_type,
455+
name=name,
456+
description=description,
457+
callback_url=callback_url,
458+
file_path=file_path,
459+
url=url,
457460
)
458461
media_id = upload_data.get("id", "")
459462
if media_id.startswith("m-"):

0 commit comments

Comments
 (0)