-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add recent list support for videos/playlists
- Loading branch information
Showing
21 changed files
with
439 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from pathlib import Path | ||
from typing import Generic, Iterable, List, Optional, TypeVar, Union | ||
|
||
from pydantic import parse_obj_as | ||
|
||
from gridplayer.models.video_uri import VideoURI | ||
|
||
T = TypeVar("T") | ||
|
||
IN_URI = Union[str, VideoURI] | ||
IN_PATH = Union[str, Path] | ||
|
||
|
||
class RecentList(Generic[T]): | ||
def __init__(self): | ||
self._list: List[T] = [] | ||
|
||
def __bool__(self) -> bool: | ||
return bool(self._list) | ||
|
||
def __iter__(self) -> Iterable[T]: | ||
return iter(self._list) | ||
|
||
def __len__(self): | ||
return len(self._list) | ||
|
||
def add(self, items: List[T]) -> None: | ||
for item in reversed(items): | ||
if item in self._list: | ||
self._list.remove(item) | ||
self._list.insert(0, item) | ||
|
||
def truncate(self, limit: int) -> None: | ||
if len(self._list) > limit: | ||
self._list = self._list[:limit] | ||
|
||
|
||
class RecentListVideos(RecentList[VideoURI]): | ||
def __init__(self, uris: Optional[List[IN_URI]] = None): | ||
super().__init__() | ||
|
||
if uris is None: | ||
return | ||
|
||
for uri in uris: | ||
if isinstance(uri, str): | ||
try: | ||
uri = parse_obj_as(VideoURI, uri) | ||
except ValueError: | ||
continue | ||
|
||
self._list.append(uri) | ||
|
||
|
||
class RecentListPlaylists(RecentList[Path]): | ||
def __init__(self, paths: Optional[List[IN_PATH]] = None): | ||
super().__init__() | ||
|
||
if paths is None: | ||
return | ||
|
||
for path in paths: | ||
self._list.append(Path(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from pydantic import AnyUrl, FilePath, PydanticValueError | ||
|
||
from gridplayer.params.extensions import SUPPORTED_MEDIA_EXT | ||
|
||
|
||
class VideoURL(AnyUrl): | ||
allowed_schemes = {"http", "https", "rtp", "rtsp", "udp", "mms", "mmsh"} | ||
max_length = 2083 | ||
|
||
|
||
class PathNotAbsoluteError(PydanticValueError): | ||
code = "path.not_absolute" | ||
msg_template = 'path "{path}" is not absolute' | ||
|
||
|
||
class PathExtensionNotSupportedError(PydanticValueError): | ||
code = "path.ext_not_supported" | ||
msg_template = 'path extension "{path}" is not supported' | ||
|
||
|
||
class AbsoluteFilePath(FilePath): | ||
@classmethod | ||
def validate(cls, path: Path) -> Path: | ||
super().validate(path) | ||
|
||
if not path.is_absolute(): | ||
raise PathNotAbsoluteError(path=path) | ||
|
||
if path.suffix[1:].lower() not in SUPPORTED_MEDIA_EXT: | ||
raise PathExtensionNotSupportedError(path=path) | ||
|
||
return path | ||
|
||
|
||
VideoURI = Union[VideoURL, AbsoluteFilePath] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.