Skip to content

Commit

Permalink
feat(Track): Make ID optional, Automatically compute one if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaphoenix committed Mar 1, 2024
1 parent d16ace8 commit d3292b9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions devine/core/tracks/track.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import base64
import re
import subprocess
from copy import copy
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Iterable, Optional, Union
from uuid import UUID
from zlib import crc32

import m3u8
import requests
Expand All @@ -24,18 +26,16 @@ class Descriptor(Enum):

def __init__(
self,
id_: str,
url: Union[str, list[str]],
language: Union[Language, str],
is_original_lang: bool = False,
descriptor: Descriptor = Descriptor.URL,
needs_repack: bool = False,
drm: Optional[Iterable[DRM_T]] = None,
edition: Optional[str] = None,
extra: Optional[Any] = None
extra: Optional[Any] = None,
id_: Optional[str] = None,
) -> None:
if not isinstance(id_, str):
raise TypeError(f"Expected id to be a {str}, not {type(id_)}")
if not isinstance(url, (str, list)):
raise TypeError(f"Expected url to be a {str}, or list of {str}, not {type(url)}")
if not isinstance(language, (Language, str)):
Expand All @@ -46,6 +46,8 @@ def __init__(
raise TypeError(f"Expected descriptor to be a {Track.Descriptor}, not {type(descriptor)}")
if not isinstance(needs_repack, bool):
raise TypeError(f"Expected needs_repack to be a {bool}, not {type(needs_repack)}")
if not isinstance(id_, (str, type(None))):
raise TypeError(f"Expected id_ to be a {str}, not {type(id_)}")
if not isinstance(edition, (str, type(None))):
raise TypeError(f"Expected edition to be a {str}, not {type(edition)}")

Expand All @@ -59,7 +61,6 @@ def __init__(
except TypeError:
raise TypeError(f"Expected drm to be an iterable, not {type(drm)}")

self.id = id_
self.url = url
# required basic metadata
self.language = Language.get(language)
Expand All @@ -73,6 +74,14 @@ def __init__(
self.edition: str = edition
self.extra: Any = extra or {} # allow anything for extra, but default to a dict

if not id_:
this = copy(self)
this.url = self.url.rsplit("?", maxsplit=1)[0]
checksum = crc32(repr(self).encode("utf8"))
id_ = hex(checksum)[2:]

self.id = id_

# TODO: Currently using OnFoo event naming, change to just segment_filter
self.OnSegmentFilter: Optional[Callable] = None

Expand Down

0 comments on commit d3292b9

Please sign in to comment.