Skip to content

models

Extraction

Bases: BaseModel

Represents the extracted data from a source.

Source code in src/torah_dl/core/models.py
 7
 8
 9
10
11
12
13
class Extraction(BaseModel):
    """Represents the extracted data from a source."""

    title: str | None = None
    download_url: str
    file_format: str | None = None
    file_name: str | None = None

Extractor

Bases: ABC

Abstract base class for all extractors.

Source code in src/torah_dl/core/models.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Extractor(ABC):
    """Abstract base class for all extractors."""

    @property
    @abstractmethod
    def url_patterns(self) -> Pattern | list[Pattern]:
        """
        Returns the regex pattern(s) that match URLs this extractor can handle.
        Can return either a single compiled regex pattern or a list of patterns.
        """
        pass

    def can_handle(self, url: str) -> bool:
        """
        Checks if this extractor can handle the given URL.

        Args:
            url: The URL to check

        Returns:
            bool: True if this extractor can handle the URL, False otherwise
        """
        patterns = self.url_patterns
        if isinstance(patterns, Pattern):
            patterns = [patterns]

        return any(pattern.match(url) for pattern in patterns)

    @abstractmethod
    def extract(self, url: str) -> Extraction:
        """
        Extracts data from the given URL.

        Args:
            url: The URL to extract from

        Returns:
            Extraction: The extracted data

        Raises:
            ValueError: If the URL is not supported by this extractor
        """
        pass

url_patterns abstractmethod property

url_patterns: Pattern | list[Pattern]

Returns the regex pattern(s) that match URLs this extractor can handle. Can return either a single compiled regex pattern or a list of patterns.

can_handle

can_handle(url: str) -> bool

Checks if this extractor can handle the given URL.

Parameters:

Name Type Description Default
url str

The URL to check

required

Returns:

Name Type Description
bool bool

True if this extractor can handle the URL, False otherwise

Source code in src/torah_dl/core/models.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def can_handle(self, url: str) -> bool:
    """
    Checks if this extractor can handle the given URL.

    Args:
        url: The URL to check

    Returns:
        bool: True if this extractor can handle the URL, False otherwise
    """
    patterns = self.url_patterns
    if isinstance(patterns, Pattern):
        patterns = [patterns]

    return any(pattern.match(url) for pattern in patterns)

extract abstractmethod

extract(url: str) -> Extraction

Extracts data from the given URL.

Parameters:

Name Type Description Default
url str

The URL to extract from

required

Returns:

Name Type Description
Extraction Extraction

The extracted data

Raises:

Type Description
ValueError

If the URL is not supported by this extractor

Source code in src/torah_dl/core/models.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@abstractmethod
def extract(self, url: str) -> Extraction:
    """
    Extracts data from the given URL.

    Args:
        url: The URL to extract from

    Returns:
        Extraction: The extracted data

    Raises:
        ValueError: If the URL is not supported by this extractor
    """
    pass