Skip to content

Commit 2ab473e

Browse files
committed
feat: add image and ImageAsset
1 parent 6646cd8 commit 2ab473e

File tree

6 files changed

+79
-6
lines changed

6 files changed

+79
-6
lines changed

videodb/_constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class MediaType:
88
video = "video"
99
audio = "audio"
10+
image = "image"
1011

1112

1213
class SearchType:
@@ -31,6 +32,7 @@ class ApiPath:
3132
upload = "upload"
3233
video = "video"
3334
audio = "audio"
35+
image = "image"
3436
stream = "stream"
3537
thumbnail = "thumbnail"
3638
upload_url = "upload_url"

videodb/asset.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ def __repr__(self) -> str:
8585
f"fade_in_duration={self.fade_in_duration}, "
8686
f"fade_out_duration={self.fade_out_duration})"
8787
)
88+
89+
90+
class ImageAsset(MediaAsset):
91+
def __init__(
92+
self,
93+
asset_id: str,
94+
width: Optional[int] = 100,
95+
height: Optional[int] = 100,
96+
position_x: Optional[int] = 80,
97+
position_y: Optional[int] = 20,
98+
end: Optional[Union[int, None]] = None,
99+
) -> None:
100+
super().__init__(asset_id)
101+
self.width = width
102+
self.height = height
103+
self.position_x = position_x
104+
self.position_y = position_y
105+
self.end = end
106+
107+
def to_json(self) -> dict:
108+
return copy.deepcopy(self.__dict__)
109+
110+
def __repr__(self) -> str:
111+
return (
112+
f"ImageAsset("
113+
f"asset_id={self.asset_id}, "
114+
f"width={self.width}, "
115+
f"height={self.height}, "
116+
f"position_x={self.position_x}, "
117+
f"position_y={self.position_y}, "
118+
f"end={self.end})"
119+
)

videodb/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from videodb._utils._http_client import HttpClient
1414
from videodb.video import Video
1515
from videodb.audio import Audio
16+
from videodb.image import Image
1617

1718
from videodb._upload import (
1819
upload,
@@ -46,7 +47,7 @@ def upload(
4647
name: Optional[str] = None,
4748
description: Optional[str] = None,
4849
callback_url: Optional[str] = None,
49-
) -> Union[Video, Audio, None]:
50+
) -> Union[Video, Audio, Image, None]:
5051
upload_data = upload(
5152
self,
5253
file_path,
@@ -60,3 +61,5 @@ def upload(
6061
return Video(self, **upload_data) if upload_data else None
6162
elif upload_data.get("id").startswith("a-"):
6263
return Audio(self, **upload_data) if upload_data else None
64+
elif upload_data.get("id").startswith("i-"):
65+
return Image(self, **upload_data) if upload_data else None

videodb/collection.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from videodb.video import Video
1515
from videodb.audio import Audio
16+
from videodb.image import Image
1617
from videodb.search import SearchFactory, SearchResult
1718

1819
logger = logging.getLogger(__name__)
@@ -54,6 +55,17 @@ def get_audio(self, audio_id: str) -> Audio:
5455
def delete_audio(self, audio_id: str) -> None:
5556
return self._connection.delete(path=f"{ApiPath.audio}/{audio_id}")
5657

58+
def get_images(self) -> list[Image]:
59+
images_data = self._connection.get(path=f"{ApiPath.image}")
60+
return [Image(self._connection, **image) for image in images_data.get("images")]
61+
62+
def get_image(self, image_id: str) -> Image:
63+
image_data = self._connection.get(path=f"{ApiPath.image}/{image_id}")
64+
return Image(self._connection, **image_data)
65+
66+
def delete_image(self, image_id: str) -> None:
67+
return self._connection.delete(path=f"{ApiPath.image}/{image_id}")
68+
5769
def search(
5870
self,
5971
query: str,
@@ -79,7 +91,7 @@ def upload(
7991
name: Optional[str] = None,
8092
description: Optional[str] = None,
8193
callback_url: Optional[str] = None,
82-
) -> Union[Video, Audio, None]:
94+
) -> Union[Video, Audio, Image, None]:
8395
upload_data = upload(
8496
self._connection,
8597
file_path,
@@ -93,3 +105,5 @@ def upload(
93105
return Video(self._connection, **upload_data) if upload_data else None
94106
elif upload_data.get("id").startswith("a-"):
95107
return Audio(self._connection, **upload_data) if upload_data else None
108+
elif upload_data.get("id").startswith("i-"):
109+
return Image(self._connection, **upload_data) if upload_data else None

videodb/image.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from videodb._constants import (
2+
ApiPath,
3+
)
4+
5+
6+
class Image:
7+
def __init__(self, _connection, id: str, collection_id: str, **kwargs) -> None:
8+
self._connection = _connection
9+
self.id = id
10+
self.collection_id = collection_id
11+
self.name = kwargs.get("name", None)
12+
13+
def __repr__(self) -> str:
14+
return (
15+
f"Image("
16+
f"id={self.id}, "
17+
f"collection_id={self.collection_id}, "
18+
f"name={self.name})"
19+
)
20+
21+
def delete(self) -> None:
22+
self._connection.delete(f"{ApiPath.image}/{self.id}")

videodb/timeline.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Union
22

33
from videodb._constants import ApiPath
4-
from videodb.asset import VideoAsset, AudioAsset
4+
from videodb.asset import VideoAsset, AudioAsset, ImageAsset
55

66

77
class Timeline(object):
@@ -28,9 +28,9 @@ def add_inline(self, asset: Union[VideoAsset]) -> None:
2828
raise ValueError("asset must be of type VideoAsset")
2929
self._timeline.append(asset)
3030

31-
def add_overlay(self, start: int, asset: Union[AudioAsset]) -> None:
32-
if not isinstance(asset, AudioAsset):
33-
raise ValueError("asset must be of type AudioAsset")
31+
def add_overlay(self, start: int, asset: Union[AudioAsset, ImageAsset]) -> None:
32+
if not isinstance(asset, AudioAsset) and not isinstance(asset, ImageAsset):
33+
raise ValueError("asset must be of type AudioAsset or ImageAsset")
3434
self._timeline.append((start, asset))
3535

3636
def generate_stream(self) -> str:

0 commit comments

Comments
 (0)